樹莓派配置靜態IP和開機自動連接wifi


說明:

  樹莓派操作系統版本: raspbian-jessie-lite

 

材料准備:

  1. 樹莓派
  2. 無線USB接收器(無線WIFI)

 

樹莓派啟動后插入無線WIFI,查看是否正常識別(個別廠家新生產的無線USB WIFI可能無法識別!

  列出usb設備命令:

  lsusb

pi@raspberrypi:~$ lsusb
Bus 001 Device 004: ID 148f:5370 Ralink Technology, Corp. RT5370 Wireless Adapter
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Standard Microsystems Corp. 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

 

看到類似:Wireless Adapter  字樣,表示正常識別了!

 

查看網絡設備

  查看當前網絡設備

   ifconfig 

   可以看到無線WIFI網絡設備正常!

  

pi@raspberrypi:~$ ifconfig
eth0      略

lo       略

wlan0    略

 

 

開始配置:

/etc/network/interfaces 內容:

 

sudo less /etc/network/interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)

# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'

# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet manual

allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

allow-hotplug wlan1
iface wlan1 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

 

/etc/dhcpcd.conf 最后添加(讓網線(eth0)和 usb WIFI (wlan0)變成靜態ip,這樣每次SSH時都是固定ip):

這里的ip地址按自己實際情況填寫,筆者這里路由器設置在 192.168.0.1 網段

 1 interface eth0
 2 
 3 static ip_address=192.168.0.188/24
 4 static routers=192.168.0.1
 5 static domain_name_servers=192.168.0.1
 6 
 7 interface wlan0
 8 
 9 static ip_address=192.168.0.189/24
10 static routers=192.168.0.1
11 static domain_name_servers=192.168.0.1

 

最后就是WIFI密碼的配置:

 /etc/wpa_supplicant/wpa_supplicant.conf 文件內容:

 

country=CN
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
  ssid="你的wifi ssid(路由器管理頁面直接復制)"
  psk="wifi密碼"
}

 

測試:

重啟網絡:

  sudo service networking restart

 

嘗試啟動wlan0:

  sudo ifup wlan0

 

沒有報錯的話,wifi 即可正常連接!連接成功后使用SSH登錄測試下是否ok。

 

重啟樹莓派

輸入命令重啟:

   sudo reboot

 

pi@raspberrypi:~$ sudo reboot

Broadcast message from pi@raspberrypi on pts/2:

The system is going down for reboot NOW!

 

馬上拔掉網線,等待樹莓派重啟;

 

這里重啟 + 重啟后連接WIFI 需要時間,大概重啟后1 min之內可以連上wifi。剛連接可能不穩定,需要稍微等待一下。

 

這樣插網線時,IP固定為:192.168.0.188,只連接wifi 時,ip固定為 192.168.0.189,方便!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM