How to define a secondary IP address that is down by default
I don't know if you can achieve this with ifup
/ifdown
, but you can add or remove an IP address from an interface at runtime by doing
ip addr add 192.168.9.101/32 dev eth0
and
ip addr del 192.168.9.101/32 dev eth0
instead.
You don't need to define any virtual 'eth0:1' interfaces for that (although you can, if you want, by adding label eth0:1
in front of dev eth0
, I think. I never saw the point of that.).
Finally, if at some point you change your mind and decide this secondary IP to be available by default, you can do that in /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
Related videos on Youtube
Calmarius
Updated on September 17, 2022Comments
-
Calmarius almost 2 years
I can define a secondary IP address on the same network card by adding the lines
auto eth0:1 iface eth0:1 inet static address 192.168.9.101 ...
to
/etc/network/interfaces
.This gives me an IP address that I can turn on or off using
ifup
andifdown
. However, by default it is on. From reading the man page, it seems that to have it off by default I just need to remove the lineauto eth0:1
. However, this makes ifup/down not work at all.Is there a way to do this?
UPDATE: OK, so the problem was a lot simpler than I thought. Here's what I originally did:
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
This is the point where I got stuck, and figured there's something wrong with using ifup/down in such a configuration. As it turns out, the solution is quite simple:
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)
Thanks @Gilles for calling me out, and thanks @Marius for the alternate solution.
-
h3. over 13 yearsWhat do you mean by “not work at all”: do they produce no visible effect? Are there any error messages? Post the complete contents of
/etc/network/interfaces
, the output ofifconfig -a
before your attempts, and the exactifup
command(s) you tried.
-