简体   繁体   English

使用netlink API从linux接口删除ipv4地址属于同一子网

[英]deleting ipv4 address belongs to the same subnet from linux interface using netlink API

I am using netlink API rtnl_addr_delete to delete the ipv4 address configured on interface in Linux. 我正在使用netlink API rtnl_addr_delete删除Linux接口上配置的ipv4地址。 Two IPs belonging to the same subnet are configured to an interface. 属于同一子网的两个IP被配置到一个接口。

When i delete the first configured IP, both IPs are getting deleted which is unexpected. 当我删除第一个配置的IP时,两个IP都将被删除,这是意外的。 But vice versa is not true, when I delete the second configured IP, the first one does not getting deleted which is expected. 但是反之亦然,当我删除第二个配置的IP时,第一个不会被删除,这是正常的。

Suppose if 2 IPs belonging to the different subnet, issue is not seen. 假设如果2个I​​P属于不同的子网,则不会出现问题。 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? 知道这是rtnl_addr_delete行为方式,还是可以通过任何解决方法解决此问题?

When you remove the first added IP address from one subnet (the «primary» address), all the subnet is being deleted. 当您从一个子网中删除第一个添加的IP地址(“主”地址)时,将删除所有子网。 That's the default kernel behaviour in some distributions. 在某些发行版中,这是默认的内核行为。 To change that, you have to set sysctl on the interface you work with: 要更改此设置,必须在使用的接口上设置sysctl:

$ sysctl -a | grep promote_secondaries

Say, you want to change that on eth0: 假设您要在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. 使用promote_secondaries不会删除子网的其余部分,而是将其中一个地址提升为新的主要子网。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Linux上使用libnl3(netlink版本3)获取接口的ipv4地址? - How to get ipv4 address of an interface using libnl3 (netlink version 3) on linux? 如何在Linux中仅使用fs和bash从IPv4 ip地址获取接口名称? - How to get interface name from IPv4 ip address using only fs and bash in Linux? 接收未分配地址以与Linux / C / IPv4接口的最广泛的UDP - Receive UDP broadast with no address assigned to interface with Linux/C/IPv4 来自任何有效地址的ipv4和ipv6 - ipv4 and ipv6 from any valid address 为什么Linux的IPv4地址采用16个字节而不是4个字节 - Why are linux's IPv4 address taking 16 bytes instead of 4 在Linux上面临的问题-具有来自同一子网的2个IP地址的PC - Facing issue on Linux - PC with 2 IP address from same subnet 给定IP地址,在同一子网中查找接口 - Given an IP address, find interface on same subnet 如何使 Fail2Ban 同时禁止 IPv6 地址和各自的 IPv4 地址? - How to make Fail2Ban ban both the IPv6 address and the respective IPv4 address at the same time? 如何在 Linux 中获取每个接口和协议(IPv6/IPv4)的统计信息? - How to get per interface and protocol (IPv6/IPv4) statistics in Linux? Linux:检索每个接口发送/接收的数据包计数器(ethernet,ipv4,ipv6) - Linux: retrieve per-interface sent/received packet counters (ethernet, ipv4, ipv6)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM