本文記錄下Ubuntu Server 16.04修改IP、DNS、hosts的方法

--------

1. Ubuntu Server 16.04修改IP

sudo vi /etc/network/interfaces

回顯:

# The loopback network interface
auto lo
iface lo inet loopback # The primary network interface
auto eth0
iface eth0 inet static
address 192.168.0.88
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
gateway 192.168.0.1
# dns-* options are implemented by the resolvconf package, if instatlled
dns-nameservers 192.168.0.1
dns-search pcat

把address後的ip修改為自己想設定的ip後,儲存退出。

之後,重啟網路:

sudo /etc/init.d/networking restart

ps,ubuntu還有別的重啟方式(但不一定有效)

sudo service network restart

desktop版可以這樣重啟:

sudo service network-manager restart

如果只是修改了某個網絡卡(例如eth0)的資訊,也可以通過重啟網絡卡的方式使其修改生效。

sudo ifdown eth0
sudo ifup eth0

最後,我在自己機子上試了重啟網路或者網絡卡後,ifconfig裡顯示的還是舊ip,而且新舊兩個ip都可以ping通,只有重啟機子後才會顯示新的ip。

-= 2019.01 更新=-

修改靜態ip而不需要重啟才生效:

ip addr flush dev eth0
ifdown eth0
ifup eth0

2.  Ubuntu Server 16.04修改DNS

sudo vi /etc/resolvconf/resolv.conf.d/base

修改內容:

search localdomain #如果本Server為DNS伺服器,可以加上這一句,如果不是,可以不加
nameserver 8.8.8.8 #希望修改成的DNS
nameserver 114.114.114.114 #希望修改成的DNS

儲存退出,重啟網路:

sudo /etc/init.d/networking restart

檢視當前DNS:

cat /etc/resolv.conf

3.  Ubuntu Server 16.04修改hosts

檢視hostname:

cat /etc/hostname

記住其hostname,修改hosts:

sudo vi /etc/hosts

前兩行修改為:

#第1行預設這個
127.0.0.1 localhost
#第2行為你修改的ip 你剛才檢視的hostname
192.168.11.52 pcat

儲存退出。