31

If I add two IPs like this:

ip addr add 1.1.1.1/24 dev eth2
ip addr add 1.1.1.2/24 dev eth2

and then I try to delete one of like this:

ip addr del 1.1.1.2/24 dev eth2

BOTH IPs will be deleted and I don't know how to handle it.

Any one knows how to solve it?

MadHatter
  • 79,770
  • 20
  • 184
  • 232
Hamed JML
  • 443
  • 2
  • 5
  • 10

5 Answers5

28

This happens because you use /24 network mask when delete. It is applied to address you input. Try the following:

ip addr del 1.1.1.2/32 dev eth2

The /32 mask defines exactly one host.

gevial
  • 1,324
  • 9
  • 13
14

Deleting a primary IP address from an interface will, by default, delete all other address on the same subnet.

This is a reply to a REHL bug 1136733 reported by the user lain who provided an answer on 12-Mar-13 ...

You're adding multiple addresses in the same subnet, thus the second and subsequent ones become secondaries. This can be seen in the "ip addr show" output, note the keyword "secondary" next to those addresses.

By default, when deleting a primary address, kernel deletes also all respective secondaries.

If you want one of the secondaries to be promoted to be a new primary on primary deletion, set the net.ipv4.conf.eth9.promote_secondaries sysctl.

bvs
  • 141
  • 1
  • 2
2

I don't have an eth2 to check this on but it works correctly on eth0 and I see no reason why eth2 should be magical. I presume you're using ip addr show dev eth2 as ifconfig eth2 doesn't appear to show the additional addresses added by ip.

One thing I have noticed is that the order you create/delete the addresses is important. The second and subsequent addresses added within the same network will be deleted if the primary address is deleted. Consider ...

    inet 1.1.1.1/24 scope global eth0
    inet 1.1.2.3/24 scope global eth0
    inet 1.1.1.2/24 scope global secondary eth0
    inet 1.1.1.3/24 scope global secondary eth0

If you delete 1.1.1.1 then the 1.2 and 1.3 addresses will be deleted too. If you delete any other address then only that address will be deleted.

user9517
  • 115,471
  • 20
  • 215
  • 297
1

The answer of @bvs pointed in exactly the right direction of the problem and solved it for me. Here, are some details on the precise commands. As for many the default interface is eth0, I am taking that in the following commands, which has to replaced by eth2 for the original question.

For a temporary solution (until reboot) use

sudo sysctl -w net.ipv4.conf.eth0.promote_secondaries=1

for a permanent solution which persists after reboots create a file /etc/sysctl.d/ip-promote.conf (or other file name ending with .conf) with the following content:

# The following makes that a second IP on the eth0 interface
# is promoted as primary IP instead of being removed.
net.ipv4.conf.eth0.promote_secondaries=1
freiheitsnetz
  • 393
  • 1
  • 3
  • 7
0

I would try the following:

ip addr del 1.1.1.2 dev eth2

brain
  • 163
  • 1
  • 4