How to connect to WiFi on Linux via the terminal

Working with Linux-based operating systems often requires interacting with the command line, especially when the graphical interface is unavailable, malfunctioning, or you're managing a server remotely. Connecting to a wireless network is one of the basic tasks an administrator should be able to perform without using a mouse or fancy settings windows. This knowledge is essential not only for data center system administrators but also for enthusiasts setting up systems. Raspberry Pi or Odroid in "headless" mode (without a monitor).

There are several ways to establish an internet connection, and the method you choose depends on the distribution, the installed kernel version, and the network manager you are using. In modern distributions, such as Ubuntu, Fedora or Debian, is most commonly used NetworkManager, which has a powerful console client. However, minimalist builds or older machines may require manual configuration via wpa_supplicant or utilities ip And iw.

In this article, we'll detail the steps to take for various scenarios, focusing on security and troubleshooting. You'll learn how to scan the airwaves, select the optimal channel, enter passwords, and check signal quality using only the keyboard. Understanding these processes gives you complete control over your system's network stack.

Diagnostics and search for available networks

Before attempting to connect, you need to make sure the wireless adapter is active and can see nearby access points. First, check the network interface status. Most modern distributions use the command ip link, which shows the status of all network devices. You need to find an interface with a name like wlan0, wlp2s0 or wi-fi.

If the interface is in the state DOWN, it needs to be raised. To do this, use the command sudo ip link set dev wlan0 up, Where wlan0 replaced with the name of your device. Once the interface is activated, you can begin scanning the airwaves. The utility iwlist or more modern iw.

Usage iwlist gives a detailed, albeit verbose, output. The command sudo iwlist wlan0 scan will return a list of all visible networks with their parameters. If you prefer a more readable format, the utility iw dev wlan0 scan will display the information in a structured manner. Note the SSID (network name) and signal strength.

When analyzing the list of networks, pay attention not only to the name but also to the frequency and channel width. Congestion in the 2.4 GHz band is often the cause of low speeds.

  • 📡 SSID — network ID, the name you see in the list of available connections.
  • 📶 Signal level — signal level measured in dBm (the closer to 0, the better, for example -40 dBm is excellent, -90 dBm is bad).
  • 🔒 Encryption — the encryption type, usually WPA2 or WPA3, which is critical to choosing a connection method.
  • 📡 Frequency — channel frequency, 2412 MHz corresponds to channel 1, 5180 MHz — to channel 36.
📊 Which Linux distribution do you use most often?
Ubuntu/Debian
CentOS/Fedora
Arch Linux
Gentoo/Slackware
Another

Connecting via NetworkManager (nmcli)

The most convenient and widespread tool for network management in Linux is NetworkManagerIts console client nmcli Allows you to perform all necessary actions without manually editing configuration files. This is the preferred method for desktop environments and servers where the appropriate package is installed.

First, make sure the service is running. Enter systemctl status NetworkManagerIf the service is active, you can proceed directly to connecting. The command to connect to a known network is simple: nmcli dev wifi connect "Network_Name" password "Your_Password"The system will automatically determine the encryption type and attempt to obtain an IP address via DHCP.

If you've connected to the network before, you'll reconnect automatically when a signal appears. To view your saved connections, use nmcli connection showThis allows you to quickly switch between known access points.

☑️ Check before connection

Completed: 0 / 4

Some corporate networks may require hiding the SSID or using a static IP. In such cases, the command is supplemented with flags. For example, for a hidden network: nmcli dev wifi connect"SSID" password"pass" hidden yesFor a static address, you will need to create a new connection with the parameters ipv4.addresses And ipv4.gateway.

⚠️ Attention: When entering a password on the command line, it may appear in clear text in the bash command history. For security, use environment variables or enter the password interactively if your terminal allows for input masking.

Manual configuration via wpa_supplicant

In environments where NetworkManager is not present (for example, minimalist server builds or embedded systems), a bundle of utilities is used wpa_supplicant And dhcpcd (or dhclient). This method provides deeper control, but requires precision in the syntax of the configuration files.

The first step is to create or edit a configuration file /etc/wpa_supplicant/wpa_supplicant.confThis file describes the network settings. To generate a hashed password (to avoid storing it in plaintext), use the command wpa_passphrase"SSID""password"Copy the result into the config.

network={

ssid="MyHomeWiFi"

psk="password_hash_or_plaintext"

key_mgmt=WPA-PSK

}

After preparing the config, you need to start the daemon. The command sudo wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf initiates a connection. Flag -B means running in the background, -i specifies the interface, and -c — path to the config.

By myself wpa_supplicant It only authenticates but does not assign an IP address. To obtain an IP address and DNS settings, you need to run a DHCP client. Depending on the distribution, this may be sudo dhclient wlan0 or sudo dhcpcd wlan0Without this step, you will be "connected" to the access point, but will not be able to access the Internet.

What to do if wpa_supplicant doesn't see the network?

Make sure your wireless card driver supports scanning mode. Some proprietary drivers (such as Broadcom) require firmware modules to be loaded before they can work. Check the dmesg | grep firmware logs to identify driver loading errors.

Using the ip and iw utilities for advanced users

For experts who want to understand (low-level) processes or work in extremely constrained environments, there is a set of tools ip (from the iproute2 package) and iwd (Internet Wireless Daemon) or classic iwThese utilities allow you to manage the interface at the kernel level.

Modern replacement wpa_supplicant in many new distributions it becomes iwdIt's lighter, faster, and has a built-in DHCP client. To connect via iwctl (interactive mode) you need to complete the following sequence: station wlan0 scan, then station wlan0 get-networks and finally station wlan0 connect"SSID".

If you are using the classic stack, then after authentication via wpa_supplicant, IP address setting is performed via ip addr. For example: sudo ip addr add 192.168.1.50/24 dev wlan0. Then you need to specify the gateway: sudo ip route add default via 192.168.1.1This gives complete control, but requires manual entry of all parameters.

Working with these tools is useful when debugging drivers or creating automation scripts where graphical dependencies are unacceptable. However, for everyday use nmcli remains more convenient.

Setting up static IP and DNS

In a server environment, a fixed IP address is often required to ensure other devices can reliably find your Linux service. Setting up a static address depends on the chosen network management method. In the case of NetworkManager, this is done via the command nmcli con mod.

To manually assign an address via ip (If you are not using DHCP) you need to know the exact subnet mask and gateway. An error in the mask (for example, /24 instead of /16) will result in you not being able to communicate with devices on the other side of the network, even if there is a physical connection.

Parameter Example of meaning Description
IP Address 192.168.1.105 Unique address of the device on the local network
Netmask 255.255.255.0 (/24) Determines the size of the local network
Gateway 192.168.1.1 Router address, Internet access
DNS 8.8.8.8, 1.1.1.1 Domain name servers for URL to IP translation

Don't forget about DNS. Even with a properly configured IP, without DNS you won't be able to access websites by name, only by IP address. In the file /etc/resolv.conf Name servers are registered. For static configuration, add the lines nameserver 8.8.8.8.

Troubleshooting and signal analysis

Even if you enter the password correctly, the connection may fail. This could be due to a weak signal, an incorrect driver, or a blocking issue at the router level. The first diagnostic tool to use should be dmesg | tail immediately after attempting to connect. The Linux kernel logs driver errors in detail.

A common problem is a mismatch of security standards. If the router is configured only for WPA3, and your card or driver only supports WPA2, connection is impossible. This will appear in the logs as a deaf reason. In such cases, temporarily lowering the router's security level for testing can help.

To analyze signal quality in real time, use watch -n 1 iwconfig wlan0 (if wireless-tools is installed) or iw dev wlan0 link. Parameter Link Quality And Signal level will show whether it is worth moving closer to the router or changing the antenna.

⚠️ Attention: Interfaces and command names may vary depending on the Linux kernel version and specific distribution. Always check the documentation for your software version if standard commands return a "command not found" error.

It's also worth checking if power saving mode is enabled on your Wi-Fi adapter, which can turn off the card to save power. You can disable it with the command iw dev wlan0 set power_save offThis often solves the problem of unstable ping.

Why does WiFi disappear after a kernel update?

New versions of the Linux kernel often change the ABI (driver interface). If you're using proprietary drivers (for example, for older Broadcom or Realtek cards), you'll need to recompile them for the new kernel using the DKMS module or install the linux-headers package.

FAQ: Frequently Asked Questions

How do I find out the name of my wireless interface?

Use the command ip link or iw devThe name usually begins with wl (For example, wlp3s0) or wlan (For example, wlan0). In older systems, you may encounter eth1.

What to do if nmcli command is not found?

This means you don't have NetworkManager installed. On Debian/Ubuntu, install the package network-manager, on Arch Linux - networkmanager, in Fedora it is usually installed by default. Or use the method with wpa_supplicant.

How to save a WiFi password in plain text?

When using wpa_supplicant You don't have to hash the password, but just enter it in quotes in the field psk config. However, this is less secure, as any user with read access to the file will see the password.

Why can't Linux see 5GHz networks?

Your adapter may not support this band, or some 5 GHz channels may be blocked in the country specified in your regulatory settings (regdomain). Try setting your region: sudo iw reg set RU (or US, DE, etc.).