There are two main WiFi configuration tools used under Linux system:
- wireless tools: it mainly configures WEP encrypted WiFi. Now iwconfig and other commands have been replaced by iw. iw is also partially used for WiFi configuration in this article. If you want to know more relevant functions, you can enter iw help in the shell window to view it. (dependent on libnl Library)
- wpa_supplicant: it mainly configures WAP and WPA2 encrypted WiFi. It is designed as a daemon running in the background. It can be seen as two main executable tools:
wpa_supplicant: running in the background, equivalent to the server.
wpa_cli: the front-end to search, set up and connect to the network, which is equivalent to the client.
Next, we will use two configuration tools to configure WiFi for Linux through the command line. We choose the appropriate configuration method according to the encryption mode of the network we want to connect to.
iw
Get Root permission
$ su
Find available network cards
$ iw dev
phy#0 Interface wlan1 ifindex 7 wdev 0x2 addr 8e:f7:10:78:49:a2 ssid COZY_49a2 type AP txpower 31.00 dBm Interface wlan0 ifindex 6 wdev 0x1 addr 8c:f7:10:78:49:a2 type managed txpower 31.00 dBm
Get the above output and write down the name of the network card after the Interface. I have two network cards, WLAN 1 and WLAN 0. If you want to connect to the wireless network, you can't just choose one of the network cards. Depending on the type of network card, we can see that the type of WLAN 1 is AP, which is used as a Wireless access point, which is equivalent to a network card for wireless signal transmission, while the type of WLAN 0 is managed, which can be used as a client to access the main AP. Here we need to use the board to connect the network, so we should use WLAN 0.
View active network card
First, check the status of the network card and observe whether it is activated.
$ ip link show wlan1 //Here wlan1 is the name of the network card
//Activated status, < > contains the UP keyword. 7: wlan1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 8e:f7:10:78:49:a2 brd ff:ff:ff:ff:ff:ff //Inactive status, no UP keyword in < >. 7: wlan1: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 8e:f7:10:78:49:a2 brd ff:ff:ff:ff:ff:ff
You will get the above two possible outputs.
Then, if it is not activated, we can use one of the following two statements to activate the network card.
$ ip link set wlan1 up //Here wlan1 is the name of the network card $ ifconfig wlan1 up //Here wlan1 is the name of the network card
wlan1: INTERFACE-ENABLED
The output and print results are the same. We can continue to execute the above command to check the status of the network card again to see if it has been activated successfully.
Search nearby networks
To search for available wireless networks nearby, choose one of the following two statements.
The first method is:
$ iw wlan1 scan | grep SSID
SSID: TP-LINK_0B8B SSID: TP-LINK_2458 SSID: ChinaNet_1688 SSID: CMW-AP SSID: wifitest
iw scan can display all the specific information of the wireless network. We add grep statement to grab the key string SSID (network name) and print it.
Enter account password
$ iw wlan0 connect <SSID> keys d:1:<PSW> //SSID is the WiFi name and PSW is the password
command failed: Operation not supported (-95)
The result reported an error. I found no specific error on the Internet, but I guess the connected wireless network is not WEP encrypted. So there is no way to configure the connection through iw command. However, if it is WEP encryption, the network connection should be successful at this step.
The last is to test whether the network is connected. There is no way to test here. The remaining test steps are left to WPA_ After the supplicant configuration is successful.
wpa supplicant
Start background operation
Different from iw command, if you want to run WPA_ If supplicant is used, start it to run in the background. In this step, I feel that the network card has been activated. If not, run the $ifconfig wlan1 up command again
$ wpa_supplicant -D nl80211 -i wlan0 -c /etc/wpa_supplicant.conf -B
- -D <driver name> Driver Name (nl80211 or west)
- -I < interface name > network card name
- -C < conf file dir > configuration file
- -B background operation
Search nearby networks
The first command performs the search function, and the second command has only the display function, but no search function.
$ wpa_cli -i wlan1 scan $ wpa_cli -i wlan1 scan_result
bssid / frequency / signal level / flags / ssid 48:7d:2e:be:24:58 2462 -72 [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS] TP-LINK_2458 74:05:a5:0d:0b:8b 2462 -76 [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS] TP-LINK_0B8B 34:ce:00:0a:c6:c0 2472 -76 [WPA2-PSK-CCMP][ESS] ABC 74:da:da:8f:7f:d8 2447 -80 [WPA-PSK-CCMP+TKIP][WPA2-PSK-CCMP+TKIP][ESS] D-Link_DIR_cyan 80:ea:07:d8:97:45 2437 -80 [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS] TP-LINK_9547 dc:fe:18:ba:5c:31 2437 -81 [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS] test_vmx 88:25:93:31:8e:a2 2412 -83 [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS] TP_LINK_8EA2 80:ea:07:d8:c8:6e 2437 -84 [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS] TP-LINK_C86E 50:bd:5f:04:d9:04 2437 -86 [WPA2-PSK-CCMP][ESS] 20200401 8c:a6:df:f7:31:d5 2412 -87 [WPA-PSK-CCMP][WPA2-PSK-CCMP][ESS] TP-LINK_YC ac:f2:c5:96:82:e6 2437 -93 [WPA2-PSK-CCMP][ESS] ott123
From here, we can see the specific information of each WiFi, and their encryption methods include WPA, WPA2 and ESS, but there is no WEP, so we can't connect just now (laugh). The last column is the SSID we need
Enter account password
In WPA_ There are two ways to enter account and password in supplicant:
- Command line input: This is a temporary input, which should be re entered after the next system restart.
- Modify configuration file: directly configure the account and password in the configuration file. The system will automatically connect after the next restart.
Command line input
First enter interactive mode:
$ wpa_cli -i wlan1
Then determine the connection mode according to the encrypted information of the WiFi network to be connected.
// $ > add_network > set_network 0 ssid "SSID" > set_network 0 psk "Password" > enable_network 0
<3>CTRL-EVENT-SCAN-STARTED <3>CTRL-EVENT-SCAN-RESULTS <3>CTRL-EVENT-NETWORK-NOT-FOUND <3>CTRL-EVENT-SCAN-STARTED <3>CTRL-EVENT-SCAN-RESULTS <3>Trying to associate with c2:b8:b6:a7:8a:00 (SSID='Jiangnan' freq=2412 MHz) > [ 382.799309@1] Connecting with c2:b8:b6:a7:8a:00 ssid "Jiangnan", len (8) channel=1 [ 382.799309@1] [ 382.878924@1] wl_iw_event: Link UP with BSSID=c2:b8:b6:a7:8a:00 [ 382.879278@1] wl_bss_connect_done succeeded with c2:b8:b6:a7:8a:00 <3>Associated with c2:b8:b6:a7:8a:00 <3>CTRL-EVENT-SUBNET-STATUS-UPDATE status=0 <3>WPA: Key negotiation completed with c2:b8:b6:a7:8a:00 [PTK=CCMP GTK=CCMP] <3>CTRL-EVENT-CONNECTED - Connection to c2:b8:b6:a7:8a:00 completed [id=0 id_str=] > [ 382.982776@1] wl_bss_connect_done succeeded with c2:b8:b6:a7:8a:00
In this way, the network connection is successful. We can also add a command to save the configuration of this successful connection to / etc / WPA_ supplicant. In the conf file.
$ wpa_cli -i wlan0 save_config
In this way, when we use the cat command to view the file, we will find that the network name and password have been saved in it.
Profile connection
By directly modifying the configuration file / etc/wpa_supplicant.conf, write the network name and password directly into the configuration file for connection.
First enter the directory of / etc, and then open the configuration file with vi, WPA_ The configuration file of supplicant is in this directory
$ cd ./etc $ vi wpa_supplicant.conf
At this time, we have entered the configuration file, but to edit the file, press i to enter the editing mode and add the configuration file to the network item:
ctrl_interface=/var/run/wpa_supplicant update_config=1 network={ ssid="SSID" psk="Password" }
When the modification is completed, type Esc to exit editing mode, and then type: and x to save the modified file.
Get IP address
$ dhclient wlan0
I read many articles that use the dhclient command to obtain the IP address, but I don't know why I can't find this command on Linux. There should be something missing.
If there is no gateway, an error will occur if you ping the Internet.
So we use the route command to view the kernel routing table
$ route
Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 127.0.0.0 * 255.0.0.0 U 0 0 0 lo 172.20.10.0 * 255.255.255.240 U 306 0 0 wlan0 192.168.2.0 * 255.255.255.0 U 0 0 0 wlan1 192.168.5.0 * 255.255.255.0 U 0 0 0 usb0
We will find that the gateway is nothing, so we need to assign a default gateway.
route add default gw 10.0.0.253 //This ip is found through ifconfig or ip addr
In this way, the gateway is assigned.
Another way is to directly restart the system ireboot, and then the system will automatically assign you an IP address next time, so that you can connect successfully directly.
Detect network connection
Here we use ping Baidu to detect the connection.
$ ping -c10 www.baidu.com
-c10 means sending 10 packets.
PING www.baidu.com (183.232.231.172): 56 data bytes 64 bytes from 183.232.231.172: seq=0 ttl=55 time=68.751 ms 64 bytes from 183.232.231.172: seq=1 ttl=55 time=173.151 ms 64 bytes from 183.232.231.172: seq=2 ttl=55 time=120.435 ms 64 bytes from 183.232.231.172: seq=3 ttl=55 time=63.139 ms 64 bytes from 183.232.231.172: seq=4 ttl=55 time=58.868 ms 64 bytes from 183.232.231.172: seq=5 ttl=55 time=197.981 ms 64 bytes from 183.232.231.172: seq=6 ttl=55 time=57.563 ms 64 bytes from 183.232.231.172: seq=7 ttl=55 time=56.769 ms 64 bytes from 183.232.231.172: seq=8 ttl=55 time=89.183 ms 64 bytes from 183.232.231.172: seq=9 ttl=55 time=75.906 ms --- www.baidu.com ping statistics --- 10 packets transmitted, 10 packets received, 0% packet loss round-trip min/avg/max = 56.769/96.174/197.981 ms
Done!