树莓派wifi配置
2018年10月27日

概述

树莓派有多种联网的方式,通过有线网或者通过无线网。通过有线网连接是比较简单的,在开启dhcp的路由器下,直接插上网线就可以联网,本文介绍树莓派无线联网的方式。再没联网的情况下,如果没有屏幕等外设,通过串口连接是一种比较简单有效的方式。

扫描WIFI

使用如下命令可以扫描附近的无线网:

sudo iwlist wlan0 scan

扫描结果如下:

wlan0     Scan completed :
          Cell 01 - Address: 48:7D:2E:68:C7:81
                    ESSID:"TP-LINK_C781"
                    Protocol:IEEE 802.11bgn
                    Mode:Master
                    Frequency:2.412 GHz (Channel 1)
                    Encryption key:on
                    Bit Rates:300 Mb/s
                    Extra:wpa_ie=dd160050f20101000050f20401000050f20401000050f202
                    IE: WPA Version 1
                        Group Cipher : CCMP
                        Pairwise Ciphers (1) : CCMP
                        Authentication Suites (1) : PSK
                    Extra:rsn_ie=30140100000fac040100000fac040100000fac020000
                    IE: IEEE 802.11i/WPA2 Version 1
                        Group Cipher : CCMP
                        Pairwise Ciphers (1) : CCMP
                        Authentication Suites (1) : PSK
                    Quality=100/100  Signal level=47/100  
          Cell 02 - Address: 80:89:17:50:D0:B0
                    ESSID:"TP-LINK_D0B0-mxr"
                    Protocol:IEEE 802.11bgn
                    Mode:Master
                    Frequency:2.412 GHz (Channel 1)
                    Encryption key:on
                    Bit Rates:300 Mb/s
                    Extra:wpa_ie=dd160050f20101000050f20401000050f20401000050f202
                    IE: WPA Version 1
                        Group Cipher : CCMP
                        Pairwise Ciphers (1) : CCMP
                        Authentication Suites (1) : PSK
                    Extra:rsn_ie=30140100000fac040100000fac040100000fac020000
                    IE: IEEE 802.11i/WPA2 Version 1
                        Group Cipher : CCMP
                        Pairwise Ciphers (1) : CCMP
                        Authentication Suites (1) : PSK
                    Quality=88/100  Signal level=42/100  
          Cell 03 - Address: 34:CE:00:07:FD:F0
                    ESSID:"Xiaomi_FDEF"
                    Protocol:IEEE 802.11bgn
                    Mode:Master
                    Frequency:2.412 GHz (Channel 1)
                    Encryption key:on
                    Bit Rates:144 Mb/s
                    Extra:wpa_ie=dd1a0050f20101000050f20202000050f2020050f20401000050f202
                    IE: WPA Version 1
                        Group Cipher : TKIP
                        Pairwise Ciphers (2) : TKIP CCMP
                        Authentication Suites (1) : PSK
                    Extra:rsn_ie=30180100000fac020200000fac02000fac040100000fac020000
                    IE: IEEE 802.11i/WPA2 Version 1
                        Group Cipher : TKIP
                        Pairwise Ciphers (2) : TKIP CCMP
                        Authentication Suites (1) : PSK
                    IE: Unknown: DD480050F204104A0001101044000102104700102880288028801880A88034CE0007FDF01023000352334C1011000C5869616F4D69526F75746572103C0001011049000600372A000120
                    Quality=9/100  Signal level=43/100  

这里的TP-LINK_C781是无线网的名称。IEEE 802.11i/WPA2 Version 1是加密的方式。加密的类型是WPA2类型的。

添加有密码的WIFI网络

通过配置wpa_supplicant.conf可以设置要连接的无线网。

sudo vi /etc/wpa_supplicant/wpa_supplicant.conf

在文件末尾出添加一下代码,并替换掉ssid_name以及password即可。

network={
        ssid="ssid_name"
        key_mgmt=WPA-PSK
        psk="password"
}

重启网络服务

/etc/init.d/networking restart

通过

ifconfig wlan0

可以查看IP地址是否分配成功

wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.8  netmask 255.255.255.0  broadcast 192.168.10.255
        inet6 xxxx::xxxx:xxxx:xxxx:xxxx  prefixlen 64  scopeid 0x20<link>
        inet6 xxxx:xxxx:xxxx::xxx  prefixlen 128  scopeid 0x0<global>
        inet6 xxxx:xxxx:xxxx:x:xxx:xxxx:xxx:xxxx  prefixlen 64  scopeid 0x0<global>
        ether xx:xx:xx:xx:xx:xx  txqueuelen 1000  (Ethernet)
        RX packets 936  bytes 187707 (183.3 KiB)
        RX errors 0  dropped 10  overruns 0  frame 0
        TX packets 564  bytes 170931 (166.9 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

连接隐藏网络

如果要连接的是隐藏的网络,需要在配置文件中添加scan_ssid连接。需要添加的内容如下:

network={
    ssid="ssid_name"
    scan_ssid=1
    psk="password"
}

添加多个无线网络配置

network={
    ssid="ssid1_name"
    psk="password1"
    id_str="wifi1"
}

network={
    ssid="ssid2_name"
    psk="password2"
    id_str="wifi2"
}

如果您有两个网络的范围,可以添加优先级选项来在它们之间进行选择。具有最高优先级的范围内的网络将是连接的网络。

network={
    ssid="ssid1_name"
    psk="password1"
    priority=1
    id_str="wifi1"
}

network={
    ssid="ssid2"
    psk="password2"
    priority=2
    id_str="wifi2"
}

添加没有密码的WIFI网络

添加没有密码的WIFI网络需要注意将key_mgmt设置为NONE

network={
        ssid="ssid_name"
        key_mgmt=NONE
}

IP的设置

sudo vi /etc/network/interfaces
auto wlan0
iface wlan0 inet static
   address 192.168.10.11
   netmask 255.255.255.0
  wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

错误的调试

如果出现连接不成功的情况,有很大的可能是由于配置文件配置错误的原因。

sudo wpa_supplicant -c /etc/wpa_supplicant/wpa_supplicant.conf  -i wlan0

通过wpa_supplicant的直接连接,如果配置文件出现问题,则会直接提示配置文件的错误详情。

如果出现一下结果,则一般代表配置文件没有问题

Successfully initialized wpa_supplicant
nl80211: Driver does not support authentication/association or connect commands
wlan0: Failed to initialize driver interface