Connecting to Wi-Fi via a terminal in Ubuntu It may seem like a daunting task for beginners, but in practice, it's one of the most reliable ways to manage network connections. The graphical interface NetworkManager It sometimes crashes, especially when working with non-standard router configurations or after system updates. The terminal, however, allows for flexible connection configuration, troubleshooting, and even automation through scripts.
In this article you will find step-by-step instructions Connecting to Wi-Fi via the command line, including scanning available networks, saving passwords, managing profiles, and troubleshooting common errors. We'll cover standard tools like nmcli And nmtui, as well as low-level utilities like wpa_supplicant For experienced users. If you've ever encountered connection issues after a sleep or reboot, or simply want to understand how Linux networking works at a deeper level, this guide is for you.
Before you begin, make sure your Wi-Fi adapter is recognized by the systemTo do this, enter the command lspci | grep -i network (for PCI devices) or lsusb (for USB adapters). If the adapter isn't detected, you may need to install drivers—we'll also briefly cover this topic.
1. Checking the network adapter and drivers
Before connecting to Wi-Fi, you need to make sure that your network adapter correctly determined by the system and drivers are installed for it. Ubuntu Most modern adapters are supported out of the box, but some models (especially from Broadcom or Realtek) may require additional action.
To check if the adapter is present, run:
lspci -knn | grep -iA3 net
For USB adapters use:
lsusb
If in the output you see a line with Network controller, but it is indicated nearby Kernel driver in use: none, this means that the driver is not loaded. In this case:
- 🔧 Install proprietary drivers via
Additional drivers(software-properties-gtk) or manually: - 📦 For Broadcom:
sudo apt install firmware-b43-installer - 🔄 For Realtek (e.g. RTL8821CE): Clone the repository from GitHub and compile the driver.
⚠️ Attention: If the adapter is no longer detected after installing the drivers, try rolling back the changes or using an older kernel version. Some drivers conflict with the latest updates. Ubuntu.
After installing the drivers, reboot the system or manually load the kernel module:
sudo modprobe -r {module_name} && sudo modprobe {module_name}
2. Scanning available Wi-Fi networks
To see a list of available networks, you can use several tools. The easiest way is the utility nmcli (Part NetworkManager), which provides a convenient interface for managing connections.
Run the command to scan:
nmcli device wifi list
If NetworkManager disabled, activate it:
sudo systemctl start NetworkManager
sudo systemctl enable NetworkManager
An alternative method is to use iwlist (outdated, but works on all distributions):
sudo iwlist wlan0 scan | grep ESSID
Here wlan0 — the name of your Wi-Fi interface. You can find it using the command ip a or iwconfig.
| Team | Description | Output example |
|---|---|---|
nmcli d wifi |
Shows Wi-Fi status (on/off) | enabled or disabled |
nmcli d wifi rescan |
Force rescan | Successfully requested a Wi-Fi scan. |
iw dev wlan0 scan | grep"SSID:" |
Scanning with output of network names only | SSID: MyWiFi_2.4G |
⚠️ Attention: If the teamnmcli device wifi listreturns an empty list, check if Wi-Fi is blocked by a hardware switch (on some laptops) or software viarfkillYou can unlock it with the commandsudo rfkill unblock wifi.
3. Connecting to Wi-Fi via nmcli
Utility nmcli — the easiest way to connect to Wi-Fi from a terminal. It allows you to save passwords, manage profiles, and even set up static IP addresses. Let's look at a basic scenario for connecting to a network with a password.
Step 1. Connect to the network (the password will be saved in NetworkManager):
nmcli device wifi connect "NETWORK_NAME" password "PASSWORD"
Step 2. If the network is hidden (does not broadcast SSID), add the flag hidden yes:
nmcli device wifi connect "NETWORK_NAME" password "PASSWORD" hidden yes
Step 3. To connect to a network without saving a password (one-time connection), use:
nmcli connection add type wifi con-name"TempWiFi" ifname wlan0 ssid"NETWORK_NAME"nmcli connection modify TempWiFi wifi-sec.key-mgmt wpa-psk
nmcli connection modify TempWiFi wifi-sec.psk"PASSWORD"
nmcli connection up TempWiFi
To manage existing connections:
- 📋 View all saved networks:
nmcli connection show - 🔄 Reconnect to the network:
nmcli connection up"CONNECTION_NAME" - 🗑️ Delete a saved network:
nmcli connection delete"CONNECTION_NAME"
Ping to the router (ping 192.168.1.1)
Ping to Google DNS (ping 8.8.8.8)
DNS Check (nslookup ubuntu.com)
Speed test (speedtest-cli)
-->
4. Connecting via wpa_supplicant (for advanced users)
If NetworkManager is not available or you prefer manual configuration, you can use wpa_supplicant — a low-level utility for managing Wi-Fi connections. This method requires superuser privileges and basic knowledge of working with configuration files.
Step 1. Edit the configuration file:
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
Add a block for your network (example for WPA2-PSK):
network={ssid="NETWORK_NAME"
psk="PASSWORD"
key_mgmt=WPA-PSK
}
Step 2. Connect to the network:
sudo wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf
sudo dhclient wlan0
Here dhclient Requests an IP address from the router. If you need to specify a static IP, use ifconfig or ip addr.
Step 3. To automatically connect on boot, add wpa_supplicant to startup:
sudo systemctl enable wpa_supplicant
⚠️ Attention: Filewpa_supplicant.confcontains cleartext passwords. Make sure the file's permissions are restricted:sudo chmod 600 /etc/wpa_supplicant/wpa_supplicant.confOtherwise, any user of the system will be able to see your network passwords.
What to do if wpa_supplicant does not connect?
If after launch wpa_supplicant The network does not connect, check:
1. The specified SSID and password are correct (case sensitive!).
2. Support of the encryption type by your adapter (for example, some older adapters do not work with WPA3).
3. Error logs: journalctl -u wpa_supplicant -f.
4. Possible interference on the channel (try connecting to a network on a different 2.4/5 GHz channel).
5. Eliminating typical errors
Even if you enter the commands correctly, connecting to Wi-Fi via the terminal may still result in errors. Let's look at the most common issues and their solutions.
Error: "Device not managed" or "unmanaged"
This means that NetworkManager does not control your Wi-Fi interface. Solution:
sudo nmcli device set wlan0 managed yes
sudo systemctl restart NetworkManager
Error: "Activation of network connection failed"
Most often caused by an incorrect password or incompatible encryption type. Check:
- 🔐 The password is correct (enter it in quotation marks if it contains special characters).
- 📡 Network security type (e.g.
WPA2-PSKvsWPA3). - 🔄 Reboot your router (this sometimes helps clear temporary blocks).
Error: "No secrets were provided"
This means that wpa_supplicant or nmcli Didn't receive your password? Make sure:
- 🔑 The password is transmitted in the correct format (for example, for
nmcliuse the flagpassword). - 📄 Configuration file
wpa_supplicant.confedited without syntax errors.
| Error | Possible cause | Solution |
|---|---|---|
Connection activated, but no IP address |
The DHCP server is not issuing IP addresses. | Check your router settings or assign IP manually |
SSID not found |
The network is hidden or out of range | Add a flag hidden yes or check the antenna |
Authentication timeout |
Incompatibility of encryption types | Try changing the security type on your router. |
6. Automating connections via scripts
If you frequently connect to the same network, you can automate the process with a bash script. This is especially convenient for servers or systems without a graphical interface.
Example script for connecting via nmcli:
#!/bin/bashSSID="MyWiFi"
PASSWORD="mysecurepassword"
Let's check if we're already connected.
if nmcli connection show --active | grep -q"$SSID"; then
echo "Already connected to $SSID"
exit 0
fi
Let's connect
nmcli device wifi connect"$SSID" password"$PASSWORD"
We check the result
if ip a show wlan0 | grep -q"inet"; then
echo "Successfully connected to $SSID"
echo "IP address: $(ip a show wlan0 | grep"inet" | awk'{print $2}')"
else
echo "Connection error"
exit 1
fi
Save the script to a file (eg. connect_wifi.sh), make it executable and run it when needed:
chmod +x connect_wifi.sh
./connect_wifi.sh
To automatically run on boot, add the script to crontab:
crontab -e
Add the line:
@reboot /path/to/script/connect_wifi.sh
Important: Storing cleartext passwords in a script is not secure. For production systems, use wpa_supplicant with encrypted configuration files or tools like pass to manage passwords.
7. Alternative tools: nmtui and iwd
Besides nmcli And wpa_supplicant, V Ubuntu Other Wi-Fi management tools are available.
nmtui (Text User Interface for NetworkManager)
It is a text-based interface, convenient for users who don't want to remember commands. nmcliRun it with the command:
nmtui
In the menu, select Activate a connection, then find your network and enter the password.
iwd (Intel Wireless Daemon)
A lightweight Wi-Fi management daemon developed by IntelIt consumes fewer resources than wpa_supplicant, and supports modern standards such as WPA3. Install it:
sudo apt install iwd
Turn it off wpa_supplicant and activate iwd:
sudo systemctl disable wpa_supplicantsudo systemctl enable iwd
sudo systemctl start iwd
Connection via iwd:
sudo iwctlstation wlan0 scan
station wlan0 get-networks
station wlan0 connect"NETWORK_NAME"
Comparison of tools:
| Tool | Pros | Cons |
|---|---|---|
nmcli |
Simplicity, integration with NetworkManager |
Requires running NetworkManager |
wpa_supplicant |
Flexible, works without NetworkManager |
Complex configuration, manual IP control |
iwd |
Lightweight, supports WPA3 |
Less documentation, not all adapters are supported |
8. Security: How to protect your connection
Connecting to Wi-Fi via a terminal requires special attention to security, as passwords can be stored in cleartext in configuration files. Here are some recommendations:
1. Encryption of configuration files
If you use wpa_supplicant, encrypt the file with passwords:
gpg -c /etc/wpa_supplicant/wpa_supplicant.conf
This will create an encrypted copy that can only be decrypted using your GPG key.
2. Restricting access rights
Make sure that configuration files are accessible only to root:
sudo chmod 600 /etc/wpa_supplicant/wpa_supplicant.conf
sudo chown root:root /etc/wpa_supplicant/wpa_supplicant.conf
3. Using a VPN
If you connect to public networks, always use a VPN to encrypt your traffic. Install OpenVPN or WireGuard:
sudo apt install openvpn
sudo openvpn --config client.ovpn
4. Disabling unnecessary networks
Regularly clear your list of saved networks to avoid automatically connecting to unsafe access points:
nmcli connection delete "UNNECESSARY_NETWORK"
Additional measures:
- 🔒 Use complex passwords (at least 12 characters with letters, numbers, and special characters).
- 🛡️ Turn it off
WPSon the router - this protocol is vulnerable to brute-force attacks. - 🔄 Update your router firmware and packages regularly
NetworkManager/wpa_supplicant.
⚠️ Attention: Never connect to networks with suspicious names (eg.Free_WiFiorStarbucks_Free) without additional protection. Attackers often create fake access points to intercept traffic.
FAQ: Frequently Asked Questions
Is it possible to connect to Wi-Fi without a password (open network)?
Yes, for open networks use the command:
nmcli device wifi connect"NETWORK_NAME" --ask
When prompted for a password, simply click Enter. For wpa_supplicant Specify in the config:
network={ssid="NETWORK_NAME"
key_mgmt=NONE
}
Please note that open networks are not secure - your traffic can be intercepted.
How do I connect to a network with Enterprise authentication (WPA2-Enterprise)?
For networks with 802.1X (for example, in universities or offices) use:
nmcli connection add type wifi con-name"EnterpriseWiFi" ifname wlan0 ssid"NETWORK_NAME"nmcli connection modify EnterpriseWiFi wifi-sec.key-mgmt wpa-eap
nmcli connection modify EnterpriseWiFi 802-1x.eap peap
nmcli connection modify EnterpriseWiFi 802-1x.phase2-auth mschapv2
nmcli connection modify EnterpriseWiFi 802-1x.identity"YOUR_LOGIN"
nmcli connection modify EnterpriseWiFi 802-1x.password"YOUR_PASSWORD"
nmcli connection up EnterpriseWiFi
Replace peap on ttls, if another authentication method is required.
Why doesn't Wi-Fi connect automatically after sleep/hibernation?
This is a common power management issue. Solutions:
- Disable Wi-Fi auto-off:
sudo sed -i's/wifi.powersave = 3/wifi.powersave = 2/' /etc/NetworkManager/conf.d/default-wifi-powersave-on.confsudo systemctl restart NetworkManager - Add the script to
pm-utilsto reconnect:sudo nano /etc/pm/sleep.d/10_wifiInsert:
#!/bin/shcase"$1" in
resume|thaw)
nmcli radio wifi on
nmcli connection up"CONNECTION_NAME"
esacMake executable:
sudo chmod +x /etc/pm/sleep.d/10_wifi
How do I connect to a 5GHz network if it's not visible in the list?
Some adapters only scan for 2.4 GHz by default. To enable 5 GHz support:
- Check if your adapter supports 5GHz:
iw list | grep"5 GHz" - Force the country to be set (eg.
USfor the maximum number of channels):sudo iw reg set USsudo systemctl restart NetworkManager - Manually specify the channel when scanning:
sudo iw dev wlan0 scan freq 5180(5180 is an example channel for 5 GHz)
If your adapter does not support 5GHz, consider purchasing a USB adapter that does. 802.11ac.
Is it possible to manage Wi-Fi connections via SSH?
Yes, all the commands described (nmcli, wpa_supplicant) work via SSH. However:
- Make sure the SSH server is running on the remote machine (
sudo apt install openssh-server). - For
wpa_supplicantIt may be necessary to pass environment variables (eg.DBUS_SESSION_BUS_ADDRESS). - Use
screenortmuxto prevent the session from being interrupted when SSH is disconnected:sudo apt install tmuxtmux new -s wifi_session
Run commands inside tmux
To disable: Ctrl+B, then D
To return: tmux attach -t wifi_session