Я могу определить вторичный IP-адрес на той же сетевой карте, добавив строки
auto eth0:1
iface eth0:1 inet static
address 192.168.9.101
...
в /etc/network/interfaces
.
Это дает мне IP-адрес, который я могу включить или выключить, используя ifup
и 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-адрес из интерфейса во времени выполнения путем выполнения
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