在我的/etc/network/interfaces.d/eth0
我已经设置:
auto eth0
iface eth0 inet static
address 10.0.0.25
netmask 255.255.255.0
gateway 10.0.0.1
dns-nameservers 8.8.8.8 8.8.4.4
它将在我的接口上为我的本地机器分配一个来自本地网络的静态 IP eth0
。
重启网络后,service networking restart
检查时发现我的旧 IP 为 192.168.178.25ifconfig
eth0 Link encap:Ethernet HWaddr 09:c5:e3:f1:a3:05
inet addr:192.168.178.25 Bcast:192.168.178.255 Mask:255.255.255.0
inet6 addr: XXXX::XXXX:XXXX:XXXX:XXXX/64 Scope:Link
inet6 addr: XXXX:XXXX:XXXX:X:XXXX:XXXX:XXXX:XXXX/64 Scope:Global
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:21005449 errors:0 dropped:2187 overruns:0 frame:0
TX packets:19834119 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:15429610791 (15.4 GB) TX bytes:12129886014 (12.1 GB)
Interrupt:114
现在到了有趣的部分:
当在我的本地网络上 ping 这台机器时,我收到来自 10.0.0.25 或 192.168.178.25 的响应:
PING 10.0.0.25 (10.0.0.25) 56(84) bytes of data.
64 bytes from 10.0.0.25: icmp_seq=1 ttl=64 time=0.220 ms
64 bytes from 10.0.0.25: icmp_seq=2 ttl=64 time=0.189 ms
64 bytes from 10.0.0.25: icmp_seq=3 ttl=64 time=0.196 ms
--- 10.0.0.25 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1998ms
rtt min/avg/max/mdev = 0.189/0.201/0.220/0.021 ms
PING 192.168.178.25 (192.168.178.25) 56(84) bytes of data.
64 bytes from 192.168.178.25: icmp_seq=1 ttl=64 time=0.189 ms
64 bytes from 192.168.178.25: icmp_seq=2 ttl=64 time=0.191 ms
64 bytes from 192.168.178.25: icmp_seq=3 ttl=64 time=0.191 ms
--- 192.168.178.25 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1998ms
rtt min/avg/max/mdev = 0.189/0.190/0.191/0.011 ms
(由于网关也发生了变化,我现在获得了与以前不同的外部 IP 地址,但仍然通过我的旧(网关和)外部 IP 路由流量!?)
有什么安全的方法可以仅使用 ssh 即可获取新分配的 ip,而无需重新启动机器,并且不会失去访问权限?
有些人说ifdown eth0; ifup eth0
这是可行的方法,但是有些人说这在 ssh 上运行时会搞乱整个机器的连接。