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...
3
  • At first glance I thought you had VLAN IDs 311 and 312 tagged on that interface
    – Criggie
    Commented Jun 21 at 0:02
  • Hi Flo, on Stack Exchange sites it's conventional not to edit your chosen answer into the question. In fact some might consider it poor etiquette because it takes attention away from the actual answer and the person who posted it. Instead, you can post your own answer to the question (as long as it adds something that isn't in someone else's answer), and you can even accept your own answer if you honestly think it's the one that actually solves your problem.
    – David Z
    Commented Jun 22 at 9:40
  • 1
    @DavidZ All right, my point was to list exactly all the commands I used and the effect they had, but if positing that in a separate answer is more relevant then I will switch it :)
    – Flo
    Commented Jun 24 at 7:23

2 Answers 2

11

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.

2
  • This worked exactly as intended when combined with method 3), thank you. Sadly I cannot make decisions about the OS being used in this context, but I agree that this one is quite outdated...
    – Flo
    Commented Jun 20 at 9:51
  • 1
    The behavior kind of goes together with the Linux 2.2 (or was it 2.0?) era "eth0:122" pseudo interfaces / alias names that OP was trying to use. Commented Jun 21 at 10:41
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .