wpa_supplicant工具包括wpa_supplicant和wpa_cli两个程序,其中wpa_supplicant程序作为服务器运行在后台,服务于wpa_cli客户端的请求,从而实现WiFi的配置和连接。
使用 wpa_supplicant要运行 wpa_cli,您必须先运行 wpa_supplicant。wpa_supplicant作为wpa_cli的服务器,必须先启动wpa_supplicant才能被wpa_cli访问。使用命令:
使用 wpa_supplicant要运行 wpa_cli,您必须先运行 wpa_supplicant。wpa_supplicant作为wpa_cli的服务器,必须先启动wpa_supplicant才能被wpa_cli访问。使用命令:
wpa_supplicant -D nl80211 -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf -B
//-D nl80211: Indicates that the driver type name is nl80211
//-i wlan0: Indicates that the network interface name is wlan0
// -c /etc/wpa_supplicant/wpa_supplicant.conf:
// The path of the configuration file wpa_supplicant.conf,
// The path in BPI-M2-Zero Armbian_22.11.0 is this.
//-B: means hang the daemon process wpa_supplicant to run in the background
使用 wpa_cli
启动 wpa_cli 应用程序
$ wpa_cli -i wlan0 scan // Search for nearby wifi networks
$ wpa_cli -i wlan0 scan_result // Print search wifi network results
$ wpa_cli -i wlan0 list_network // List existing network connection ids
$ wpa_cli -i wlan0 add_network // Add a network connection and return an id number
如果连接加密方式为[WPA-psk-CCMP TKIP][WPA2-PSK-CCMP TKIP][ESS](wpa加密),wifi名称为name,wifi密码为:psk,网络连接id为0,添加倍数会递增。
$ wpa_cli -i wlan0 set_network 0 SSID '"name"'
$ wpa_cli -i wlan0 set_network 0 psk '"psk"'
$ wpa_cli -i wlan0 select_network 0
$ wpa_cli -i wlan0 enable_network 0
如果连接加密方式为[WEP][ESS](wep加密),则wifi名称为name,wifi密码为psk。
$ wpa_cli -i wlan0 set_network 0 ssid '"name"'
$ wpa_cli -i wlan0 set_network 0 key_mgmt NONE
$ wpa_cli -i wlan0 set_network 0 wep_key0 '"psk"'
$ wpa_cli -i wlan0 select_network 0
$ wpa_cli -i wlan0 enable_network 0
如果连接加密方式为[ESS](不加密),则wifi名称为name。
$ wpa_cli -i wlan0 set_network 0 ssid '"name"'
$ wpa_cli -i wlan0 set_network 0 key_mgmt NONE
$ wpa_cli -i wlan0 select_network 0
$ wpa_cli -i wlan0 enable_network 0
分配 ip/网络掩码/网关/dns
$ udhcpc -i wlan0 -q
如果command not found遇到,安装必要的工具udhcpc,用于分配ip/netmask/gateway/dns。
sudo apt update
sudo apt install udhcpc
如果你遇到:
Warning: /etc/resolv.conf is not a symbolic link to /run/resolvconf/resolv.conf`
这边走:
// Compare the two files
$ cat /run/resolvconf/resolv.conf
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
# 127.0.0.53 is the systemd-resolved stub resolver.
# run "systemd-resolve --status" to see details about the actual nameservers.
nameserver 192.168.223.150
nameserver 127.0.0.53
$ cat /etc/resolv.conf
// /etc/resolv.conf probably doesn't have the contents of /run/resolvconf/resolv.conf, delete it and ln again
$ sudo rm /etc/resolv.conf
$ sudo ln -s /run/resolvconf/resolv.conf /etc/resolv.conf
$ udhcpc -i wlan0 -q
udhcpc: started, v1.30.1
udhcpc: sending discover
udhcpc: sending select for 192.168.223.189
udhcpc: lease of 192.168.223.189 obtained, lease time 3599
执行完成后,就可以连接网络了。
// verify
$ifconfig
$ ping baidu.com
$ ping google.com
// save the connection
$ wpa_cli -i wlan0 save_config
// Disconnect
$ wpa_cli -i wlan0 disable_network 0
// connect to an existing connection
$ wpa_cli -i wlan0 list_network // list all saved connections
$ wpa_cli -i wlan0 select_network 0 // connect to the first saved connection
$ wpa_cli -i wlan0 enable_network 0 // Enable the first saved connection
// disconnect wifi
$ ifconfig wlan0 down
$ killall udhcpc
$ killall wpa_supplicant
编辑如何保存WiFi
ip link set wlan0 up
iw dev wlan0 scan | grep SSID
vim /etc/wpa_supplicant/wpa_supplicant.conf
network={
ssid="ssid"
psk="password"
priority=1
}
wpa_supplicant -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf -B
dhclient wlan0
编辑一次后,断电或复位后可自动连接。