我可以通过添加行在同一网卡上定义辅助 IP 地址
auto eth0:1
iface eth0:1 inet static
address 192.168.9.101
...
到/etc/network/interfaces
.
ifup
这给了我一个 IP 地址,我可以使用和打开或关闭它ifdown
。但是,默认情况下它是打开的。通过阅读手册页,似乎默认情况下将其关闭,我只需要删除该行auto eth0:1
。但是,这使得 ifup/down 根本不起作用。
有没有办法做到这一点?
更新:好的,所以问题比我想象的要简单得多。这是我最初做的:
root@dev003:~# cat /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.2.101
netmask 255.255.0.0
network 192.168.0.0
broadcast 192.168.255.255
gateway 192.168.1.1
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 192.168.1.1
auto eth0:1
iface eth0:1 inet static
address 192.168.9.101
netmask 255.255.0.0
network 192.168.0.0
broadcast 192.168.255.255
gateway 192.168.1.1
# dns-* options are implemented by the resolvconf package, if installed
dns-nameservers 192.168.1.1
root@dev003:~# /etc/init.d/networking restart
* Reconfiguring network interfaces... ssh stop/waiting
ssh start/running, process 3339
ssh stop/waiting
ssh start/running, process 3373
[ OK ]
root@dev003:~# ifconfig
eth0 Link encap:Ethernet HWaddr 08:00:27:03:43:82
inet addr:192.168.2.101 Bcast:192.168.255.255 Mask:255.255.0.0
inet6 addr: fe80::a00:27ff:fe03:4382/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2836310 errors:0 dropped:0 overruns:0 frame:0
TX packets:2806585 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:220309837 (220.3 MB) TX bytes:187167315 (187.1 MB)
eth0:1 Link encap:Ethernet HWaddr 08:00:27:03:43:82
inet addr:192.168.9.101 Bcast:192.168.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
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:16436 Metric:1
RX packets:1909267 errors:0 dropped:0 overruns:0 frame:0
TX packets:1909267 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:160262241 (160.2 MB) TX bytes:160262241 (160.2 MB)
root@dev003:~# sed -i 's/auto eth0:1/#auto eth0:1/' /etc/network/interfaces
root@dev003:~# /etc/init.d/networking restart
* Reconfiguring network interfaces... SIOCDELRT: No such process
ssh stop/waiting
ssh start/running, process 3787
[ OK ]
root@dev003:~# ifconfig
eth0 Link encap:Ethernet HWaddr 08:00:27:03:43:82
inet addr:192.168.2.101 Bcast:192.168.255.255 Mask:255.255.0.0
inet6 addr: fe80::a00:27ff:fe03:4382/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2840372 errors:0 dropped:0 overruns:0 frame:0
TX packets:2810267 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:220637496 (220.6 MB) TX bytes:187451978 (187.4 MB)
eth0:1 Link encap:Ethernet HWaddr 08:00:27:03:43:82
inet addr:192.168.9.101 Bcast:192.168.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
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:16436 Metric:1
RX packets:1911328 errors:0 dropped:0 overruns:0 frame:0
TX packets:1911328 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:160435069 (160.4 MB) TX bytes:160435069 (160.4 MB)
root@dev003:~# ifdown eth0:1
ifdown: interface eth0:1 not configured
这就是我被卡住的地方,并认为在这样的配置中使用 ifup/down 有问题。事实证明,解决方案非常简单:
root@dev003:~# ifup eth0:1
ssh stop/waiting
ssh start/running, process 3829
root@dev003:~# ifdown eth0:1
root@dev003:~# ifconfig
eth0 Link encap:Ethernet HWaddr 08:00:27:03:43:82
inet addr:192.168.2.101 Bcast:192.168.255.255 Mask:255.255.0.0
inet6 addr: fe80::a00:27ff:fe03:4382/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2840609 errors:0 dropped:0 overruns:0 frame:0
TX packets:2810473 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:220656625 (220.6 MB) TX bytes:187469288 (187.4 MB)
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:16436 Metric:1
RX packets:1911447 errors:0 dropped:0 overruns:0 frame:0
TX packets:1911447 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:160445053 (160.4 MB) TX bytes:160445053 (160.4 MB)
感谢@Gilles 给我打电话,并感谢@Marius 提供替代解决方案。
我不知道您是否可以使用
ifup
/来实现这一点ifdown
,但是您可以在运行时从接口中添加或删除 IP 地址和
反而。
你不需要为此定义任何虚拟的“eth0:1”接口(虽然你可以,如果你想,通过
label eth0:1
在前面添加dev eth0
,我想。我从来没有看到这一点。)。最后,如果您在某个时候改变主意并决定默认使用此辅助 IP,您可以在 /etc/network/interfaces 中执行此操作: