# 樹莓派的 Wifi 設定 : 使用迅捷 FW150US
# Raspberry Pi B+ WiFi 無法連線問題
# 樹莓派的 "無頭存取" (headless access)
如果樹莓派是透過手機分享上網, 查 IP 會比較方便一點, 可在 "設定/更多內容/網路共用與可攜式無線基地台/可攜式 WiFi 無線基地台設定" 中查到所分派的 IP). 不過, 我還是希望樹莓派連上基地台時獲得固定 IP, 這樣用 Putty 遠端連線時 IP 就不用改來改去.
下面是根據柯博文寫的 "Raspberry Pi 最佳入門與實戰應用第二版" 第 3.3.3 節測試固定 IP 設定方法的實驗記錄.
首先用 ifconfig 查詢 wlan0 的網路資訊 :
pi@raspberrypi:~ $ ifconfig
eth0 Link encap:Ethernet HWaddr b9:27:eb:2d:74:39
inet6 addr: fe80::f6a5:9334:bd66:f9a7/64 Scope:Link
UP BROADCAST MULTICAST MTU:1500 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)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:4478 errors:0 dropped:0 overruns:0 frame:0
TX packets:4478 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:936384 (914.4 KiB) TX bytes:936384 (914.4 KiB)
wlan0 Link encap:Ethernet HWaddr 09:1d:43:20:0b:26 (MAC 位址)
inet addr:192.168.43.26 Bcast:192.168.43.255 Mask:255.255.255.0
inet6 addr: fe80::21d:43ff:fe20:b26/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:36546 errors:0 dropped:13624 overruns:0 frame:0
TX packets:33900 errors:0 dropped:1 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:128164113 (122.2 MiB) TX bytes:10314492 (9.8 MiB)
主要是 inet address (我想要的固定 IP), Bcast (廣播位址), 以及 Mask (遮罩). 可見我的手機基地台分派 192.168.43 網域之網址.
然後編輯網路介面設定檔 /etc/network/interfaces :
然後編輯網路介面設定檔 /etc/network/interfaces :
pi@raspberrypi:~ $ sudo nano /etc/network/interfaces
原來 DHCP 的設定是 :
source-directory /etc/network/interfaces.d
auto lo
iface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0
auto wlan0
iface wlan0 inet dhcp
wpa-ssid "H30-L02-webbot"
wpa-psk "1234567890"
將其修改為 (藍色部分) :
source-directory /etc/network/interfaces.d
auto lo
iface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0
auto wlan0
iface wlan0 inet static #固定 IP
address 192.168.43.26 #我想要的 IP
gateway 192.168.43.254 #閘道器位址
netmask 255.255.255.0 #網路遮罩
network 192.168.43.1 #網路位址
broadcast 192.168.43.255 #廣播位址
wpa-ssid "H30-L02-webbot"
wpa-psk "1234567890"
按 Ctrl+O 存檔後按 Ctrl+X 跳出, 再輸入下列指令重啟網路介面 :
pi@raspberrypi:~ $ sudo /etc/init.d/networking restart
重啟後從手機的可攜式 WiFi 無線基地台設定中看到確實再次連線成功, 其 IP 即我所要的 192.168.43.26 :
這樣當突然停電造成樹莓派重新開機時, 就會固定獲得所要的 IP, 不會變來變去. 雖然在使用 DHCP 時, 每次樹莓派重開機都能獲得同樣的 IP, 不過我想那是因為當時剛好沒別的設備連線的關係, 使用固定 IP 仍有必要.
參考 :
# Tutorial - How to give your Raspberry Pi a Static IP Address
2017-09-24 補充 :
今天將鄉下的 Pi 3 改為固定 IP, 如此一來用 PC 做 headless access 時才不用去查目前 Pi 3 的 IP 是多少. 原先用 DHCP 取得 IP 的設定 :
pi@raspberrypi:~ $ ifconfig
eth0 Link encap:Ethernet HWaddr b8:27:eb:54:a9:XX
inet6 addr: fe80::4387:9902:66d9:3062/64 Scope:Link
UP BROADCAST MULTICAST MTU:1500 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)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:9 errors:0 dropped:0 overruns:0 frame:0
TX packets:9 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:524 (524.0 B) TX bytes:524 (524.0 B)
wlan0 Link encap:Ethernet HWaddr b8:27:eb:01:fc:XX
inet addr:192.168.2.116 Bcast:192.168.2.255 Mask:255.255.255.0
inet6 addr: fe80::ba27:ebff:fe01:fca3/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:66 errors:0 dropped:0 overruns:0 frame:0
TX packets:91 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:9593 (9.3 KiB) TX bytes:14030 (13.7 KiB)
上面的 inet addr 為 DHCP 指派給 Pi 3 的 IP; Bcast 是廣播位址要抄下來給 broadcast, 而 Mask 則是要給 netmask. 先用 nano 在 home 下編輯一個 interfaces_fix 檔案 :
pi@raspberrypi:~ $ nano interfaces_fix
pi@raspberrypi:~ $ cat interfaces_fix
source-directory /etc/network/interfaces.d
auto lo
iface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0
auto wlan0
iface wlan0 inet static
address 192.168.2.192 #我要的 IP
gateway 192.168.2.2 #無線基地台 IP
netmask 255.255.255.0 #從 ifconfig 得到
network 192.168.2.1 #無線基地台起始位址
broadcast 192.168.2.255 #從 ifconfig 得到
wpa-ssid "EDIMAX-meixxxx"
wpa-psk "a5572056"
注意, 上面 # 與後面的註解都要拿掉, 否則無法運作.
然後將 interfaces_fix 複製到 /etc/network/interfaces 將 interfaces 蓋掉, 再重新起始網路介面 :
pi@raspberrypi:~ $ sudo cp interfaces_fix /etc/network/interfaces
pi@raspberrypi:~ $ sudo /etc/init.d/networking restart
[....] Restarting networking (via systemctl): networking.serviceWarning: Unit file of networking.service changed on disk, 'systemctl daemon-reload' recommended.
. ok
pi@raspberrypi:~ $ ifconfig
eth0 Link encap:Ethernet HWaddr b8:27:eb:54:a9:f6
inet6 addr: fe80::4387:9902:66d9:3062/64 Scope:Link
UP BROADCAST MULTICAST MTU:1500 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)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:69 errors:0 dropped:0 overruns:0 frame:0
TX packets:69 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:4852 (4.7 KiB) TX bytes:4852 (4.7 KiB)
wlan0 Link encap:Ethernet HWaddr b8:27:eb:01:fc:a3
inet addr:192.168.2.192 Bcast:192.168.2.255 Mask:255.255.255.0
inet6 addr: fe80::a434:e9f3:65ef:6334/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:24192 errors:0 dropped:2 overruns:0 frame:0
TX packets:14960 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:13299614 (12.6 MiB) TX bytes:2151929 (2.0 MiB)
可見已向基地台取得固定 IP 192.168.2.192 了.
參考 :
# Tutorial - How to give your Raspberry Pi a Static IP Address
2017-09-24 補充 :
今天將鄉下的 Pi 3 改為固定 IP, 如此一來用 PC 做 headless access 時才不用去查目前 Pi 3 的 IP 是多少. 原先用 DHCP 取得 IP 的設定 :
pi@raspberrypi:~ $ ifconfig
eth0 Link encap:Ethernet HWaddr b8:27:eb:54:a9:XX
inet6 addr: fe80::4387:9902:66d9:3062/64 Scope:Link
UP BROADCAST MULTICAST MTU:1500 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)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:9 errors:0 dropped:0 overruns:0 frame:0
TX packets:9 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:524 (524.0 B) TX bytes:524 (524.0 B)
wlan0 Link encap:Ethernet HWaddr b8:27:eb:01:fc:XX
inet addr:192.168.2.116 Bcast:192.168.2.255 Mask:255.255.255.0
inet6 addr: fe80::ba27:ebff:fe01:fca3/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:66 errors:0 dropped:0 overruns:0 frame:0
TX packets:91 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:9593 (9.3 KiB) TX bytes:14030 (13.7 KiB)
上面的 inet addr 為 DHCP 指派給 Pi 3 的 IP; Bcast 是廣播位址要抄下來給 broadcast, 而 Mask 則是要給 netmask. 先用 nano 在 home 下編輯一個 interfaces_fix 檔案 :
pi@raspberrypi:~ $ nano interfaces_fix
pi@raspberrypi:~ $ cat interfaces_fix
source-directory /etc/network/interfaces.d
auto lo
iface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0
auto wlan0
iface wlan0 inet static
address 192.168.2.192 #我要的 IP
gateway 192.168.2.2 #無線基地台 IP
netmask 255.255.255.0 #從 ifconfig 得到
network 192.168.2.1 #無線基地台起始位址
broadcast 192.168.2.255 #從 ifconfig 得到
wpa-ssid "EDIMAX-meixxxx"
wpa-psk "a5572056"
注意, 上面 # 與後面的註解都要拿掉, 否則無法運作.
上面 Gateway IP 即無線基地台 IP, 與 netmask 可在基地台管理網頁查得 :
然後將 interfaces_fix 複製到 /etc/network/interfaces 將 interfaces 蓋掉, 再重新起始網路介面 :
pi@raspberrypi:~ $ sudo cp interfaces_fix /etc/network/interfaces
pi@raspberrypi:~ $ sudo /etc/init.d/networking restart
[....] Restarting networking (via systemctl): networking.serviceWarning: Unit file of networking.service changed on disk, 'systemctl daemon-reload' recommended.
. ok
pi@raspberrypi:~ $ ifconfig
eth0 Link encap:Ethernet HWaddr b8:27:eb:54:a9:f6
inet6 addr: fe80::4387:9902:66d9:3062/64 Scope:Link
UP BROADCAST MULTICAST MTU:1500 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)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:69 errors:0 dropped:0 overruns:0 frame:0
TX packets:69 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:4852 (4.7 KiB) TX bytes:4852 (4.7 KiB)
wlan0 Link encap:Ethernet HWaddr b8:27:eb:01:fc:a3
inet addr:192.168.2.192 Bcast:192.168.2.255 Mask:255.255.255.0
inet6 addr: fe80::a434:e9f3:65ef:6334/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:24192 errors:0 dropped:2 overruns:0 frame:0
TX packets:14960 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:13299614 (12.6 MiB) TX bytes:2151929 (2.0 MiB)
可見已向基地台取得固定 IP 192.168.2.192 了.
沒有留言 :
張貼留言