Ubuntu 16.04 设置静态 IP / 固定 IP

Ubuntu 16.04 设置静态 IP / 固定 IP

1. ifconfig -> enp3s0

网络连接名称:enp3s0
IP 地址:192.168.6.37

strong@foreverstrong:~$ ifconfig
enp3s0    Link encap:Ethernet  HWaddr 1c:1b:0d:e3:af:e9  
          inet addr:192.168.6.37  Bcast:192.168.6.255  Mask:255.255.255.0
          inet6 addr: fe80::1b78:61:bd4c:2d87/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1808365712 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10376878243 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:196698885166 (196.6 GB)  TX bytes:15639587311254 (15.6 TB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:78508 errors:0 dropped:0 overruns:0 frame:0
          TX packets:78508 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:8266536 (8.2 MB)  TX bytes:8266536 (8.2 MB)

strong@foreverstrong:~$

2. nmcli device show [ifname] -> IP4.DNS[1]

Show detailed information about devices. Without an argument, all devices are examined. To get information for a specific device, the interface name has to be provided.
显示有关设备的详细信息。没有参数,所有设备都会被检查。要获取特定设备的信息,必须提供接口名称。

strong@foreverstrong:~$ nmcli device show
GENERAL.DEVICE:                         enp3s0
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         1C:1B:0D:E3:AF:E9
GENERAL.MTU:                            1500
GENERAL.STATE:                          100 (connected)
GENERAL.CONNECTION:                     Auto Ethernet
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/4
WIRED-PROPERTIES.CARRIER:               on
IP4.ADDRESS[1]:                         192.168.6.37/24
IP4.GATEWAY:                            192.168.6.1
IP4.ROUTE[1]:                           dst = ***.***.***.***/16, nh = 0.0.0.0, mt = 1000
IP4.DNS[1]:                             ***.***.***.***
IP6.ADDRESS[1]:                         fe80::1b78:61:bd4c:2d87/64
IP6.GATEWAY:                            

GENERAL.DEVICE:                         lo
GENERAL.TYPE:                           loopback
GENERAL.HWADDR:                         00:00:00:00:00:00
GENERAL.MTU:                            65536
GENERAL.STATE:                          10 (unmanaged)
GENERAL.CONNECTION:                     --
GENERAL.CON-PATH:                       --
IP4.ADDRESS[1]:                         127.0.0.1/8
IP4.GATEWAY:                            
IP6.ADDRESS[1]:                         ::1/128
IP6.GATEWAY:                            
strong@foreverstrong:~$

3. /etc/network/interfaces

修改配置文件。

3.1 cat /etc/network/interfaces

strong@foreverstrong:~$ cat /etc/network/interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback

strong@foreverstrong:~$

3.2 sudo vim /etc/network/interfaces

strong@foreverstrong:~$ sudo vim /etc/network/interfaces
strong@foreverstrong:~$

Esc -> :wq!

# interfaces(5) file used by ifup(8) and ifdown(8)
# auto lo
# iface lo inet loopback

# yongqiang cheng
#网络连接名称
auto enp3s0 
# 设置为静态
iface enp3s0 inet static
# 设置静态 IP 地址
address 192.168.6.37
# 设置子网掩码
netmask 255.255.255.0
# 设置网关
gateway 192.168.6.1
# 设置 DNS
dns-nameservers 221.11.***.***

3.3 cat /etc/network/interfaces

strong@foreverstrong:~$ cat /etc/network/interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)
# auto lo
# iface lo inet loopback

# yongqiang cheng
auto enp3s0
iface enp3s0 inet static
address 192.168.6.37
netmask 255.255.255.0
gateway 192.168.6.1
dns-nameservers 221.11.***.***

strong@foreverstrong:~$

3.4 cat /etc/resolv.conf

strong@foreverstrong:~$ cat /etc/resolv.conf
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 127.0.1.1
strong@foreverstrong:~$

4. restart

重启网络服务。

4.1 sudo /etc/init.d/networking restart

strong@foreverstrong:~$ sudo /etc/init.d/networking restart
[....] Restarting networking (via systemctl): networking.serviceJob for networking.service failed because the control process exited with error code. See "systemctl status networking.service" and "journalctl -xe" for details.
 failed!
strong@foreverstrong:~$

4.2 sudo service network-manager restart

Ubuntu 16.04 使用 sudo service network-manager restart。

strong@foreverstrong:~$ sudo service network-manager restart
strong@foreverstrong:~$

5. reboot

strong@foreverstrong:~$ cat /etc/resolv.conf
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 221.11.1.67
strong@foreverstrong:~$ 
strong@foreverstrong:~$ cat /etc/network/interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)
# auto lo
# iface lo inet loopback

# yongqiang cheng
auto enp3s0
iface enp3s0 inet static
address 192.168.6.37
netmask 255.255.255.0
gateway 192.168.6.1
dns-nameservers 221.11.***.***

strong@foreverstrong:~$ 
strong@foreverstrong:~$ ping www.baidu.com
PING www.a.shifen.com (61.135.169.125) 56(84) bytes of data.
64 bytes from 61.135.169.125: icmp_seq=1 ttl=53 time=23.8 ms
64 bytes from 61.135.169.125: icmp_seq=2 ttl=53 time=23.9 ms
64 bytes from 61.135.169.125: icmp_seq=3 ttl=53 time=24.4 ms
64 bytes from 61.135.169.125: icmp_seq=4 ttl=53 time=23.6 ms
^C
--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 23.652/23.977/24.461/0.298 ms
strong@foreverstrong:~$

Server 版和 Desktop 版两种版本的设置方式有很大区别,主要在于软件的问题。Service 版依照常规设置即可,但是 Desktop 版有 Network-manager 的软件,该软件是简化 IP 的设置。如果按照 Server 版的设置后,重启 Network-manager 就会失效,不是重启服务生效。
/etc/resolv.conf 设置 DNS,重启后文件内容会被覆盖。如果要持久的保存,需要修改:/etc/resolvconf/resolv.conf.d/base。
sudo service network-manager stop 停止 network-manager 服务。
Ubuntu 使用网络管理器而不是传统的 Linux 网络模型。所以你应该重新启动 network-manager 服务而不是 network 服务。

你可能感兴趣的:(ubuntu,16.04,server,LTS)