本文章使用的映像檔為 2014-09-09-wheezy-raspbian.img 。
一般設置 Raspberry Pi 的無線網路大多是透過 WiFi Config 這個應用程式做設定。
但在某些情況,例如 從序列埠登入到 Raspberry Pi ,就只能靠命列列設置無線網路。但是記得,做任何修改前要先備份原始設定,以免出錯了無法回復原始狀態。
用命令列設置無線網路的步驟如下:
1. 確認硬體資訊,我們使用 EDIMAX 7811Un 這張無線網卡做設定。建議使用的網卡有在清單 中,才可隨插即用。
pi@raspberrypi ~ $ lsusb
Bus 001 Device 005: ID 7392:7811 Edimax Technology Co., Ltd EW-7811Un 802.11n Wireless Adapter [Realtek RTL8188CUS]
2. 查看目前無線網路設定,一開始還沒連接上無線網路,所以狀態會是 “unassociated”。
pi@raspberrypi ~ $ iwconfig wlan0
wlan0 unassociated Nickname:""
Mode:Managed Frequency=2.412 GHz Access Point: Not-Associated
Sensitivity:0/0
Retry:off RTS thr:off Fragment thr:off
Power Management:off
Link Quality:0 Signal level:0 Noise level:0
Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0
Tx excessive retries:0 Invalid misc:0 Missed beacon:0
3. 掃描無線網路,我們會根據掃描結果來設定無線網路。假設本例的 SSID 為 foo,加密方式為 WPA2 ,使用的 pre-shared key 為 1234567890123。
pi@raspberrypi ~ $ sudo iwlist wlan0 scan
wlan0 Scan completed :
Cell 03 - Address: 40:4A:03:92:BA:4B
ESSID:"foo"
Protocol:IEEE 802.11bgn
Mode:Master
Frequency:2.462 GHz (Channel 11)
Encryption key:on
Bit Rates:144 Mb/s
Extra:rsn_ie=30140100000fac040100000fac040100000fac020c00
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
4. 修改 /etc/wpa_supplicant/wpa_supplicant.conf
。
pi@raspberrypi:~$ sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
所對應的設定檔如下。
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid="foo"
psk="1234567890123"
proto=RSN
key_mgmt=WPA-PSK
pairwise=CCMP
auth_alg=OPEN
}
掃描結果與設定檔對應的欄位說明如下。
IE: IEEE 802.11i/WPA2 Version 1
表示加密方式為 WPA2,所對應的欄位 proto。RSN
:WPA(2)WPA
:WPA(1)
Group Cipher : CCMP
Pairwise Ciphers (1) : CCMP
表示 WPA2 使用 AES 加密方式,所對應的欄位 pairwise。CCMP
:AES cipher,WPA(2)TKIP
:TKIP cipher,WPA(1)
Authentication Suites (1) : PSK
表示使用 pre-shared key 做鑑別,所對應的欄位為 key_mgmt。WPA-PSK
:Authentication via pre-shared keyWPA-EAP
:Authentication via enterprise authentication server。
再例如使用 SSID 為 bar,加密的方式為 WEP ,使用的 WEP key 為 1234567890123。
pi@raspberrypi ~ $ iwlist wlan0 scan
wlan0 Scan completed :
Cell 03 - Address: 40:4A:03:92:BA:4B
ESSID:"bar"
Protocol:IEEE 802.11bg
Mode:Master
Frequency:2.437 GHz (Channel 6)
Encryption key:on
Bit Rates:54 Mb/s
Quality=72/100 Signal level=43/100
所對應的設定檔如下。
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid="bar"
key_mgmt=NONE
auth_alg=OPEN
wep_key0="1234567890123"
}
更多 wpa_supplicant.conf 的範例可參考這裡 。
5. 停用 wlan0 網卡。
pi@raspberrypi ~ $ sudo ifdown wlan0
6. 啟用 wlan0 網卡。
pi@raspberrypi ~ $ sudo ifup wlan0
可能會回傳以下訊息,我們忽略掉。
ioctl[SIOCSIWAP]: Operation not permitted
ioctl[SIOCSIWENCODEEXT]: Invalid argument
ioctl[SIOCSIWENCODEEXT]: Invalid argument
7. 將原來讀取 wpa_supplicant.conf 的程序 wpa_supplicant 殺掉。
pi@raspberrypi ~ $ sudo kill -9 $(ps -ef | grep wpa | awk '{print $2}')
8. 重新執行 wpa_supplicant,並讀取 wpa_supplicant.conf 設定。
pi@raspberrypi ~ $ sudo wpa_supplicant -B -i wlan0 -c /etc/wpa_supplicant/wpa_supplicant.conf
-B
表示以 daemon 方式在背景執行。-i
表示指定介面(interface)名稱。-c
表示設定檔路徑。
若回傳以下訊息,我們忽略掉。
rfkill: Cannot open RFKILL control device
ioctl[SIOCSIWAP]: Operation not permitted
ioctl[SIOCSIWENCODEEXT]: Invalid argument
ioctl[SIOCSIWENCODEEXT]: Invalid argument
9. 執行 DHCP 用戶端,取得 IP。
pi@raspberrypi ~ $ sudo dhclient
若回傳以下訊息,我們忽略掉。
RTNETLINK answers: File exists
10. 查尋 IP 位址,成功取得 192.168.1.117
。
pi@raspberrypi ~ $ ifconfig wlan0
wlan0 Link encap:Ethernet HWaddr 74:da:38:05:68:4c
inet addr:192.168.1.117 Bcast:192.168.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1158 errors:0 dropped:79 overruns:0 frame:0
TX packets:53 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:109024 (106.4 KiB) TX bytes:6214 (6.0 KiB)
常見問與答:
1. WiFi連不上怎麼辦?
接螢幕用 GUI 設定吧,可避免錯誤的設定。
2. WiFi Config的Adapter不見了怎麼辦?
將 /etc/network/interfaces
和 /etc/wpa_supplicant/wpa_supplicant.conf
回復成預設值吧。
##### Default configuration of /etc/network/interfaces #####
auto lo
iface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp
##### Default configuration of /etc/wpa_supplicant/wpa_supplicant.conf #####
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1