我希望能够仅使用 CLI 编辑 Ubuntu 16.04 桌面上的静态 IP。但是,当我这样做时,同一台机器有两个 IP 地址:一个由 DHCP 服务器分配,另一个从 /etc/network/interfaces 获取。
以下是流程
编辑 /etc/network/interfaces 并添加静态 IP。文件内容:
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
auto enp0s3
iface enp0s3 inet static
address 192.168.0.29
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
gateway 192.168.0.1
dns-nameservers 8.8.8.8 8.8.4.4
停止并删除 Ubuntu 网络管理器:
sudo service network-manager stop
sudo apt-get purge network-manager
重新启动网络服务:
sudo /etc/init.d/networking restart
还尝试重新启动界面本身:
ifdown enp0s3 && ifup enp0s3
尝试重新启动机器,但也没有帮助。
结果是我的机器被分配了两个 IP 地址。我甚至尝试 ssh 到它们两个,但它们实际上是同一台机器。第一个 IP 地址是从 DHCP 服务器分配的:
ping 192.168.0.189
PING 192.168.0.189 (192.168.0.189): 56 data bytes
64 bytes from 192.168.0.189: icmp_seq=0 ttl=64 time=0.341 ms
64 bytes from 192.168.0.189: icmp_seq=1 ttl=64 time=0.265 ms
64 bytes from 192.168.0.189: icmp_seq=2 ttl=64 time=0.315 ms
64 bytes from 192.168.0.189: icmp_seq=3 ttl=64 time=0.395 ms
第二个是通过编辑 /etc/network/interfaces 来分配的
ping 192.168.0.29
PING 192.168.0.29 (192.168.0.29): 56 data bytes
64 bytes from 192.168.0.29: icmp_seq=0 ttl=64 time=0.380 ms
64 bytes from 192.168.0.29: icmp_seq=1 ttl=64 time=0.742 ms
64 bytes from 192.168.0.29: icmp_seq=2 ttl=64 time=0.313 ms
64 bytes from 192.168.0.29: icmp_seq=3 ttl=64 time=0.308 ms
ifconfig 在编辑之前和编辑之后产生相同的结果:
ifconfig -a
enp0s3 Link encap:Ethernet HWaddr 08:00:27:f6:b1:d3
inet addr:192.168.0.189 Bcast:192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:13984 errors:0 dropped:0 overruns:0 frame:0
TX packets:1310 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2496671 (2.4 MB) TX bytes:124720 (124.7 KB)
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:252 errors:0 dropped:0 overruns:0 frame:0
TX packets:252 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:19145 (19.1 KB) TX bytes:19145 (19.1 KB)
那么这里发生了什么?
答案1
ps aux|grep dhclient
显示什么?如果是,则killall dhclient
重新启动网络。