Remove IP with ip command in linux

2

If I add two IPs like this:

  1. ip addr add 1.1.1.1/24 dev eth2
  2. ip addr add 1.1.1.2/24 dev eth2

and then I try to delete one of like this:

  1. 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?

Hamed JML

Posted 2013-03-12T07:06:20.620

Reputation: 387

Answers

1

If you do ip addr list dev eth2 after your two add commands, you will see something like this (emphasis mine):

2: eth2:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
     link/ether 00:11:22:33:44:55 brd ff:ff:ff:ff:ff:ff
     inet 192.168.1.1/24 brd 192.168.1.255 scope global eth2
     inet 1.1.1.1/24 scope global eth2
     inet 1.1.1.2/24 scope global secondary eth2

The important part to notice is that Linux treats the second address as secondary to the first one, as they are in the same logical subnet (/24). Deleting the secondary address has no impact on the primary one, but vice versa.

If you need to add/del addresses in 1.1.1.0/24, you probably should first add one address which is always valid (e.g. 1.1.1.99/24) and then add 1.1.1.1 and 1.1.1.2 as secondary addresses. Then you can add/delete the .1 and .2 address at will.

If this is not possible, then you can also temporarily add 1.1.1.2/32 before deleting 1.1.1.1. This will maintain connectivity for all connections from outside your network. Depending on your network setup (i.e., whether your router supports proxy ARP), you may also be able to use 1.1.1.2/0 temporarily.

Marcel Waldvogel

Posted 2013-03-12T07:06:20.620

Reputation: 401

1

Something else is likely doing things behind your back. NetworkManager is a known offender.

Run ip monitor while executing your commands, and see if someone else is modifying the configuration.

BatchyX

Posted 2013-03-12T07:06:20.620

Reputation: 1 836