如何设置WiFi连接

来自百问网嵌入式Linux wiki
Wiki讨论 | 贡献2020年6月23日 (二) 14:47的版本

如何设置WiFi连接

虽然该示例与 RTL8723BU 芯片有关(该芯片需要WiFi(最低802.11g/n)和Bluetooth 4.0 LE),但是本文中大多数命令还是通用命令,可以应用于任何平台。

安装相应软件包

buildroot下配置

yocto下配置安装

ifconfig

验证是否存在 wlan0 接口(如果该命令正常执行说明驱动程序已启动并且 WiFi 固件已加载):

检查 WLAN 接口

	Board $> ifconfig -a
	lo        Link encap:Local Loopback  
	         LOOPBACK  MTU:65536  Metric:1
	         RX packets:0 errors:0 dropped:0 overruns:0 frame:0
	         TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
	         collisions:0 txqueuelen:1000 
	         RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
	wlan0     Link encap:Ethernet  HWaddr 60:F1:89:3F:F6:0E  
	         BROADCAST MULTICAST  MTU:1500  Metric:1
	         RX packets:0 errors:0 dropped:0 overruns:0 frame:0
	         TX packets:1 errors:0 dropped:0 overruns:0 carrier:0
	         collisions:0 txqueuelen:1000 
	         RX bytes:0 (0.0 B)  TX bytes:10 (10.0 B)


初始化WLAN接口

	Board $> ifconfig wlan0 up
	wlan0     Link encap:Ethernet  HWaddr 60:F1:89:3F:F6:0E  
	         inet addr:192.168.43.135  Bcast:192.168.43.255  Mask:255.255.255.0
	         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1                   /* UP indicate that your interface is UP */
	         RX packets:19 errors:0 dropped:0 overruns:0 frame:0
	         TX packets:19 errors:0 dropped:0 overruns:0 carrier:0
	         collisions:0 txqueuelen:1000 
	         RX bytes:1774 (1.7 KiB)  TX bytes:2326 (2.2 KiB)

iw

本节是使用以下命令扫描任何无线接入点的过程:

扫描可用的 SSID(AP)

仅获取 SSID 名称
	Board $> iw dev wlan0 scan |grep SSID
	       SSID: NETWORK1
	       SSID: NETWORK2
列出完整的 SSID 信息
	Board $> iw dev wlan0 scan
	BSS 00:23:5e:4a:28:f9(on wlan0)
	       TSF: 0 usec (0d, 00:00:00)
	       freq: 2412
	       beacon interval: 100 TUs
	       capability: ESS ShortPreamble ShortSlotTime (0x0421)
	       signal: -72.00 dBm
	       last seen: 0 ms ago
	       SSID: NETWORK1
	       Supported rates: 1.0* 2.0* 5.5* 6.0 9.0 11.0* 12.0 18.0 
	       DS Parameter set: channel 1
	       TIM: DTIM Count 0 DTIM Period 1 Bitmap Control 0x0 Bitmap[0] 0x2
	       Country: FR     Environment: Indoor/Outdoor
	               Channels [1 - 13] @ 20 dBm
	       BSS Load:
	                * station count: 1
	                * channel utilisation: 30/255
	                * available admission capacity: 23437 [*32us]
	       ERP: <no flags>
	       Extended supported rates: 24.0 36.0 48.0 54.0 
	       WMM:     * Parameter version 1
	                * u-APSD
	                * BE: CW 15-1023, AIFSN 3
	                * BK: CW 15-1023, AIFSN 7
	                * VI: CW 7-15, AIFSN 2, TXOP 3008 usec
	                * VO: CW 3-7, AIFSN 2, TXOP 1504 usec
	BSS 00:23:5e:96:57:20(on wlan0)
	       TSF: 0 usec (0d, 00:00:00)
	       freq: 2412
	       beacon interval: 100 TUs
	       capability: ESS Privacy ShortPreamble ShortSlotTime (0x0431)
	       signal: -66.00 dBm
	       last seen: 0 ms ago
	       SSID: NETWORK2
	       Supported rates: 1.0* 2.0* 5.5* 6.0 9.0 11.0* 12.0 18.0 
	       DS Parameter set: channel 1
	       Country: FR     Environment: Indoor/Outdoor
	               Channels [1 - 13] @ 20 dBm
	       BSS Load:
	                * station count: 8
	                * channel utilisation: 48/255
	                * available admission capacity: 23437 [*32us]
	       ERP: <no flags>
	       RSN:     * Version: 1
	                * Group cipher: CCMP
	                * Pairwise ciphers: CCMP
	                * Authentication suites: IEEE 802.1X 00-40-96:0
	                * Capabilities: 4-PTKSA-RC 4-GTKSA-RC (0x0028)
	       Extended supported rates: 24.0 36.0 48.0 54.0 
	       WMM:     * Parameter version 1
	                * u-APSD
	                * BE: CW 15-1023, AIFSN 3
	                * BK: CW 15-1023, AIFSN 7
	                * VI: CW 7-15, AIFSN 2, TXOP 3008 usec
	                * VO: CW 3-7, AIFSN 2, TXOP 1504 usec

Wlan 网络附件(没有管理系统联网配置的系统)

配置 WiFi 连接

配置WiFi连接,使用的是 wpa_supplicant 工具
检查当前配置信息
	Board $> cat /etc/wpa_supplicant.conf
	ctrl_interface=/var/run/wpa_supplicant
	ctrl_interface_group=0
	update_config=1
	network={
	   key_mgmt=NONE
	}
设置 WiFi 的名称和密码
	Board $> wpa_passphrase <your_ssid_name> <your_ssid_key> >> /etc/wpa_supplicant.conf
检查新的配置信息
	Board $> cat /etc/wpa_supplicant.conf
	ctrl_interface=/var/run/wpa_supplicant
	ctrl_interface_group=0
	update_config=1
	network={
	       ssid="your_ssid_name"
	       psk="your_ssid_key"
	}
注意:如果没有 WPA 密钥,则将 psk=... 替换为 key_mgmt=NONE

连接到 SSID

	Board $> wpa_supplicant -B -iwlan0 -c /etc/wpa_supplicant.conf
	Successfully initialized wpa_supplicant

链接到 SSID

	Board $> iw wlan0 link
	       SSID: NETWORK1
	       freq: 2462
	       RX: 501 bytes (3 packets)
	       TX: 4056 bytes (22 packets)
	       signal: -75 dBm
	       tx bitrate: 12.0 MBit/s
	       bss flags:      short-preamble short-slot-time
	       dtim period:    1
	       beacon int:     100

为 WLAN 接口分配IP地址

使用 DHCP 客户端获取地址(假设无线网络(关联)具有 DHCP 服务器):
	Board $> dhclient wlan0
使用 ip 命令来验证 DHCP 分配的 IP 地址:
	Board $> ip addr show wlan0
	3: wlan0:  mtu 1500 qdisc mq state UP qlen 1000
	 link/ether 74:e5:43:a1:ce:65 brd ff:ff:ff:ff:ff:ff
	 inet 192.168.43.135/24 brd 192.168.1.255 scope global wlan0
	 inet6 fe80::76e5:43ff:fea1:ce65/64 scope link 
	    valid_lft forever preferred_lft forever

检查连接性

最基本的连通性测试是使用"ping”命令。在此示例中,无线路由器(关联到)的IP地址为192.168.43.1:
	Board $> ping 192.168.43.1
	PING 192.168.43.1 (192.168.43.1): 56 data bytes
	64 bytes from 192.168.43.1: seq=0 ttl=64 time=14.905 ms
	64 bytes from 192.168.43.1: seq=1 ttl=64 time=30.387 ms
	64 bytes from 192.168.43.1: seq=2 ttl=64 time=20.462 ms
注意:输入 <CTRL + C> 可终止 ping 会话。

启动时自动配置WiFi

networkctl

Systemd 为名为 systemd-networkd 的网络提供了一项特定的服务,该服务附带了networkctl 工具,该工具可以显示每个网络接口的状态。
	Board $> networkctl --no-pager
	 IDX LINK             TYPE               OPERATIONAL SETUP     
	 1 lo               loopback           carrier     unmanaged 
	 2 eth0             ether              routable    configured
	 3 sit0             sit                off         unmanaged 
	 4 ip6tnl0          tunnel6            off         unmanaged 
	 5 wlan0            wlan               off         unmanaged 
	 6 wlan1            wlan               off         unmanaged
列出 6 个链接。
我们可以看到 eth0 接口是通过配置的信息通过网络进行管理的,并且使用了该接口(可路由)。

如何通过网络设置进行无线配置

目标是通过 systemd-networkd 配置来配置 wlan 网络接口。
所有网络配置都存储在/ lib/systemd/network 或 /etc/systemd/network 中。创建专用于无线接口的文件:
	Board $> cat /lib/systemd/network/51-wireless.network
	         [Match]
	         Name=wlan0
	         
	         [Network]
	         DHCP=ipv4
要将此无线接口连接到特定网络的中,我们需要一些信息,例如网络的SSID和密码。
要查看当前范围内可用的无线网络列表:
	Board $> ifconfig wlan0 up
	Board $> iw dev wlan0 scan |grep SSID
	       SSID: NETWORK1
	       SSID: NETWORK2
将无线网络与无线网卡相连接,此处为 wlan0:
	Board $> mkdir -p /etc/wpa_supplicant/
	         echo "ctrl_interface=/var/run/wpa_supplicant" > /etc/wpa_supplicant/wpa_supplicant-wlan0.conf
	         echo "eapol_version=1" >> /etc/wpa_supplicant/wpa_supplicant-wlan0.conf
	         echo "ap_scan=1" >> /etc/wpa_supplicant/wpa_supplicant-wlan0.conf
	         echo "fast_reauth=1" >> /etc/wpa_supplicant/wpa_supplicant-wlan0.conf
	         echo "" >> /etc/wpa_supplicant/wpa_supplicant-wlan0.conf
	         wpa_passphrase SSID_OF_NETWORK PASSWORD_OF_NETWORK >> /etc/wpa_supplicant/wpa_supplicant-wlan0.conf
其中 SSID_OF_NETWORK PASSWORD_OF_NETWORK 对应于无线网络的SSID和密码。
注意:/etc/wpa_supplicant 上的文件名必须包含无线接口的名称: wpa_supplicant-wlan0.conf


启用并启动无线配置:
	Board $> systemctl enable wpa_supplicant@wlan0.service
	         systemctl restart systemd-networkd.service
	         systemctl restart wpa_supplicant@wlan0.service