1

I am using netlink API rtnl_addr_delete to delete the ipv4 address configured on interface in Linux. Two IPs belonging to the same subnet are configured to an interface.

When i delete the first configured IP, both IPs are getting deleted which is unexpected. But vice versa is not true, when I delete the second configured IP, the first one does not getting deleted which is expected.

Suppose if 2 IPs belonging to the different subnet, issue is not seen. That is, if i delete first then only first one is deleting.

Any idea weather it is how this rtnl_addr_delete behaves or any resolutions can be made to fix this issue?

jww
  • 97,681
  • 90
  • 411
  • 885
Khrusos
  • 35
  • 6

1 Answers1

2

When you remove the first added IP address from one subnet (the «primary» address), all the subnet is being deleted. That's the default kernel behaviour in some distributions. To change that, you have to set sysctl on the interface you work with:

$ sysctl -a | grep promote_secondaries

Say, you want to change that on eth0:

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

With promote_secondaries the rest of the subnet will not be removed, but instead one of the addresses will be promoted as the new primary.

Docs: https://www.kernel.org/doc/Documentation/networking/ip-sysctl.txt

svinota
  • 779
  • 8
  • 10