树莓派配置静态IP和开机自动连接wifi
说明:
树莓派操作系统版本: raspbian-jessie-lite
材料准备:
- 树莓派
- 无线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,方便!