Ubuntu 的圖形介面網路設定偶爾會「設了等於沒設」,這時候直接動手改啟動腳本與設定檔反而更快。核心只涉及兩個檔案:/etc/network/interfaces 與 /etc/resolv.conf。
先打開 /etc/network/interfaces,預設內容通常只有:
auto lo
iface lo inet loopback
若要 DHCP 自動取得位址,改成:
auto eth0
iface eth0 inet dhcp
若要固定靜態位址,則寫成:
auto eth0
iface eth0 inet static
address 192.168.0.1
netmask 255.255.255.0
gateway 192.168.0.1
依需求選一種存檔即可。
開啟 /etc/resolv.conf,加入一行 nameserver,例如:
nameserver 202.99.166.4
整組重啟:
$/etc/init.d/networking restart
或者只針對單一介面重啟(一台機器可能有多張網卡):
$ifdown eth0
$ifup eth0
用 ifconfig 檢視目前的位址是否已套用:
$ifconfig
再用 ping 逐層排查:ping 不到閘道代表網路設定有問題;閘道通了但 ping 不到網域名稱,問題就在 DNS。
暫時性的做法:
$ifconfig eth0 hw ether XX:XX:XX:XX:XX:XX
$/etc/init.d/networking restart
永久生效的做法:編輯 /etc/network/interfaces,在 iface eth0 inet static 那行之後補上一行 pre-up:
pre-up ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx
$ sudo /etc/init.d/networking restart
這樣重開機後 MAC 位址也會固定。