由於網絡環境的需要,我的樹莓派需要從自動獲取ip(DHCP)配置成靜態ip( static )。於是在網絡搜索了半天,也嘗試了各種方法,其主要是修改/etc/network/interfaces 這個文件:
cd /etc/network
sudo cp interfaces interfaces.old #備份
sudo vi 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
其中將iface eth0 inet dhcp更改為
auto eth0 #注意這一句若原先沒有的話就一一定要加上,否則配置無效
iface eth0 inet static
address 192.168.1.10 #這里寫你的ip地址
netmask 255.255.255.0 #子網掩碼
gateway 192.168.1.1 #網關ip
最后刪除或注釋掉 iface default inet dhcp這一行(如果沒有的話就不管了)
然后重啟樹莓派或者重啟服務來應用配置:
sudo service networking restart
如果它提示“Warning:Unit file of networking.service changed on disk,'systemctl daemon-reload...'”
之類的,就執行命令:systemctl daemon-reload
然后再重啟服務即可。
之后就ping一下某個網站,如果不能解析域名,就要配置dns:
sudo vi /etc/resolv.conf
然后添加:
nameserver 8.8.8.8
或者
nameserver 114.114.114.114
等等。。。最后保存再重啟服務或者樹莓派即可完成配置。