The graphical interface of the Ubuntu Linux system provides intuitive WiFi connection, but in some scenarios (such as the server without a desktop environment, the graphical interface crashes, or remote terminal operation), connecting to WiFi through the command line is a must. This article will introduce the complete process of connecting to WiFi from the terminal in detail, including tool use, configuration file modification, encrypted network processing and troubleshooting, to help users achieve efficient network management without interface.
Preparations: Check the hardware and drivers
Before connecting to the WiFi, ensure that the wireless network card is identified by the system and the driver is properly loaded. Open the terminal (Ctrl+Alt+T) and enter the following command to view the network port information:
lshw C network
Find the Wireless interface section in the output result, and confirm that the wireless network card is claimed and the driver has been correctly loaded (common drivers such as iwlwifi for Intel network cards and ath9k for Atheros network cards). If the wireless card is not enabled, run the rfkill command to unlock the hardware switch:
rfkill list Displays wireless device status
rfkill unblock Indicates that the wifi software and hardware are blocked
If the network card is not recognized, you may need to install additional drivers. For example, a Broadcom NIC needs to be installed with bcmwlkernelsource:
sudo apt update && sudo apt install bcmwlkernelsource
Scan available WiFi networks
Scan for peripheral wireless signals using iwlist. First confirm the wireless interface name (usually wlan0 or wlp2s0), using the ip a command to see:
ip a
Assuming the interface name is wlan0, scan:
sudo iwlist wlan0 scan | grep ESSID
This command lists all available WiFi names (ESSID). If you need a detailed signal strength and encryption method, remove the grep filter:
sudo iwlist wlan0 scan
Connect to Open network (no password)
For unencrypted WiFi, you can connect directly via iwconfig. The following command connects interface wlan0 to a network whose ESSID is Free_WiFi:
sudo ip link set wlan0 up Enables the interface
sudo iwconfig wlan0 essid "Free_WiFi" Specifies the network name
sudo dhclient wlan0 Obtains the IP address
After the connection is successful, run ip a show wlan0 to check the assigned IP address, or run ping 8.8.8.8 to test the network connectivity.
Connect to the WPA/WPA2 encrypted network
Most WiFi uses WPA/WPA2 encryption, using the wpa_supplicant tool. First install the necessary packages:
sudo apt install wpasupplicant
Step 4.1: Generate the configuration file
Create a temporary profile to which you write your WiFi name (SSID) and password. Use wpa_passphrase to automatically generate encryption keys:
wpa_passphrase "My_WiFi" "password123" | sudo tee /etc/wpa_supplicant.conf
This command encrypts SSID My_WiFi and password123 and saves them to the system configuration directory. View the generated content:
cat /etc/wpa_supplicant.conf
The output should contain the ssid and the encrypted psk, not the plaintext password.
Step 4.2: Start the connection
Associate encrypted networks with wpa_supplicant and run in the background:
sudo wpa_supplicant B i wlan0 c /etc/wpa_supplicant.conf
Parameter description:
B: Running in the background
i wlan0: indicates a wireless interface
c: Specify a configuration file
Step 4.3: Obtain the IP address
Start the DHCP client to obtain an IP address:
sudo dhclient wlan0
Using NetworkManager Tool (nmcli)
If NetworkManager has been installed on the system, you can use nmcli to quickly connect to the system without manually editing files.
Step 5.1: Scan and list the network
nmcli device wifi list
Step 5.2: Connect to encrypted WiFi
nmcli device wifi connect "My_WiFi" password "password123"
Step 5.3: Check the connection status
nmcli connection show
(Optional) Configuring a Static IP Address
If a fixed IP address is required, you can manually configure it. Edit the YAML file in the /etc/netplan/ directory (for example, 01netcfg.yaml) :
yaml
network:
version: 2
renderer: networkd
wifis:
wlan0:
dhcp4: no
addresses: [192.168.1.100/24]
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
accesspoints:
"My_WiFi":
password: "password123"
Application configuration:
sudo netplan apply
Troubleshooting and frequently asked questions
Problem 1: The WiFi network cannot be scanned
Check NIC status: rfkill list Indicates that the network adapter is not blocked.
Driver problem: dmesg | grep iwlwifi View driver logs.
Replace the scanning tool: try iw dev wlan0 scan.
Fault 2: The IP address cannot be obtained after the connection
Release old leases: sudo dhclient r wlan0.
Run the sudo systemctl restart NetworkManager command to restart the network service.
Problem 3: Repeated disconnections
Optimize the power management: edit/etc/NetworkManager/conf. D/defaultwifipowersaveon. Conf, wifi. Powersave set to 2 (to disable the power saving mode).
Issue 4: WPA3 encryption support
Update wpa_supplicant to version 2.9 or later and add proto=RSN key_mgmt=SAE to the configuration file.
8. Persistent configuration (automatic connection after restart)
The configuration based on netplan or NetworkManager is automatically saved. If wpa_supplicant is used manually, you need to create the systemd service:
sudo systemctl enable wpa_supplicant@wlan0
Using terminals to connect to a network is one of the core Linux user skills and is critical in server maintenance, fault recovery, and automated scripting. You can learn more about the combination of iwconfig, wpa_supplicant, nmcli and netplan tools, which can be flexibly applied in different scenarios.