10

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?

  • Your requuirement is not clear. Can you tell what is the expected output here? – Guru Mar 12 '13 at 7:12
  • I need to say ip just to delete one IP not both of them – Hamed JML Mar 12 '13 at 7:18
  • Please add ip addr show output – Gevial Mar 12 '13 at 7:54
9

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.

  • sudo systemctl net.ipv4.conf.eth0.promote_secondaries Unknown operation net.ipv4.conf.eth0.promote_secondaries. – Laryx Decidua Jan 19 '18 at 5:52
  • @LaryxDecidua systemctl is entirely different command from sysctl – yahol May 4 at 10:20
4

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.

  • This does not work as I have gave the system /24 netmask not /32. – Hamed JML Mar 12 '13 at 7:22
  • Try without netmask at all: ip addr del 1.1.1.2 dev eth2 – Gevial Mar 12 '13 at 7:35
  • Again it Delete both IPs!! – Hamed JML Mar 12 '13 at 7:37
  • Strange. On my testing CentOS box deleting without netmask was successful. Though deleting with /24 also affected only one address. What distro are you using? – Gevial Mar 12 '13 at 7:38
  • 1
    I am using Ubuntu 12.04 – Hamed JML Mar 12 '13 at 7:46
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.

  • So, how can I delete first address without removing others? – Hamed JML Mar 13 '13 at 12:53
0

I would try the following:

ip addr del 1.1.1.2 dev eth2

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.