我可以定义辅助 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 提供的替代解决方案。
答案1
我不知道你是否可以使用ifup
/来实现这一点ifdown
,但你可以在运行时通过以下方式从接口添加或删除 IP 地址
ip addr add 192.168.9.101/32 dev eth0
和
ip addr del 192.168.9.101/32 dev eth0
反而。
你不需要为此定义任何虚拟的“eth0:1”接口(尽管你可以,如果你想label eth0:1
,我认为是在 前面添加dev eth0
。我从来没有明白这一点。)。
最后,如果您在某个时候改变主意并决定默认使用此辅助 IP,您可以在 /etc/network/interfaces 中执行此操作:
auto eth0
iface eth0 inet ...
... whatever you have there already for eth0 ...
up ip addr add 192.168.9.101/32 dev eth0