7

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) :

  1. I tried several ifconfig commands with no success and read that those were deprecated anyway, so I focused on ip commands instead.

  2. 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
  1. 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
  1. 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...
Flo
  • 85

2 Answers2

12

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 value promote_secondaries to 1.

sysctl -w net.ipv4.conf.all.promote_secondaries=1
sysctl -w net.ipv4.conf.default.promote_secondaries=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.

Matthew Ife
  • 24,261
1

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:

sysctl -w net.ipv4.conf.all.promote_secondaries=1
sysctl -w net.ipv4.conf.default.promote_secondaries=1

Remove the IP address from the interface:

ip addr del 192.168.220.94/24 dev eth0:311

Add it back when needed:

ip addr add 192.168.220.94/24 dev eth0:311

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.

Flo
  • 85