Context
I am working on Centos 7 and I have a network interface eth0
configured with multiple fixed IP addresses (I am only concerned about the IPv4 addresses here, I do not care what happens to the IPV6 one) :
$ ip a
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:50:56:bf:83:39 brd ff:ff:ff:ff:ff:ff
inet 192.168.220.92/24 scope global eth0:311
valid_lft forever preferred_lft forever
inet 192.168.220.82/24 scope global secondary eth0:312
valid_lft forever preferred_lft forever
inet6 fe80::250:56ff:febf:8339/64 scope link
valid_lft forever preferred_lft forever
These addresses are automatically affected by some software so I do not have a handle on either their IP value (192.168.220.92), their alias name (eth0:311), or the order in which they are affected (primary/secondary).
Objective
What I want is a way to temporarily disable 192.168.220.92 while keeping 192.168.220.82 up and running.
Then I need a way to turn 192.168.220.92 back online still without interfering with 192.168.220.82.
Attempts
What I tried so far (with no success, or I wouldn't be here) :
I tried several
ifconfig
commands with no success and read that those were deprecated anyway, so I focused onip
commands instead.Turn the virtual interface down with
ip link set eth0:311 down
. This removes all addresses on this interface:
$ ip a
2: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
link/ether 00:50:56:bf:83:39 brd ff:ff:ff:ff:ff:ff
- Remove the IP only with
ip addr del 192.168.220.94/24 dev eth0:311
. This removes all IPv4 addresses on this interface:
$ ip a
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:50:56:bf:83:39 brd ff:ff:ff:ff:ff:ff
inet6 fe80::250:56ff:febf:8339/64 scope link
valid_lft forever preferred_lft forever
- Removing 192.168.220.84 only with method 3) works (it keeps 192.168.220.92), but this is not what I need, so I started looking for a way to swap primary and secondary IP addresses but couldn't find any. The most relevant post I found was this one which concluded it was not possible...
The setting to remove all IPs when the primary is removed is a setting that exists in Linux, I'm not entirely sure why you'd want this behaviour and I assume historically it is to emulate some backwards compatibility from older 2.4.x kernels.
Most modern distros set this to 1 by default last I checked.
Set the
sysctl
valuepromote_secondaries
to 1.You should add the relevant bit to
/etc/sysctl.conf
if you want to make the change permanent.As a side note, EL7 is a very old distro and you may want to consider upgrading if you value your systems overall integrity and security.
Based on @Matthew Ife's answer below here is what I did to achieve the objective:
Update the system settings so that the secondary address will become primary when the primary is removed:
Remove the IP address from the interface:
Add it back when needed:
Note that after this operation the "primary" and "secondary" affectation of both IPv4 IPs is now inverted compared to what it was initially, but that was not an issue in my case.