1. 程式人生 > >ifconfig 刪除虛擬網路接口出現的問題

ifconfig 刪除虛擬網路接口出現的問題

使用下面的命令啟動多個IP別名
/sbin/ifconfig eth0:1 1.42.9.162 netmask 255.255.255.224 /sbin/ifconfig eth0:2 1.42.9.163 netmask 255.255.255.224 /sbin/ifconfig eth0:3 1.42.9.186 netmask 255.255.255.224 /sbin/ifconfig eth0:4 1.42.9.182 netmask 255.255.255.224 /sbin/ifconfig eth0:5 1.42.9.169 netmask 255.255.255.224 /sbin/ifconfig eth0:6 1.42.9.168 netmask 255.255.255.224 /sbin/ifconfig eth0:7 1.42.9.190 netmask 255.255.255.224 /sbin/ifconfig eth0:8 1.42.9.183 netmask 255.255.255.224 /sbin/ifconfig eth0:9 1.42.9.184 netmask 255.255.255.224 /sbin/ifconfig eth0:10 1.42.9.185 netmask 255.255.255.224 然後執行ifconfig eth0:1 dwon注意, 這時,你會發現,ifconfig把所有的IP別名都down掉了。。。。 但是如果你down帶哦2,3.。。即除了1以外的其餘的ip別名的話,不會出現上面的問題。


使用iproute2的高階路由命令設定IP,效果也是一樣的
使用下面的命令設定多個IP
ip addr add  192.168.4.1/28 dev eth0
ip addr add  192.168.4.2/28 dev eth0
ip addr add  192.168.4.3/28 dev eth0
ip addr add  192.168.4.4/28 dev eth0

使用下面的命令
ip addr del 192.168.4.1/28 dev eth0
其餘的IP也消失了

分析及解決:
1、相關資料
multi-homing, IP aliasing, Primary address與Secondary address概念辨析

host address: A unique address assigned to a communications device in a computer. If a computer has multiple communications devices (e.g., Ethernet cards or modems), each of these devices will have its own unique address. This means that a host (computer or router) can be multi-homed, i.e., have multiple IP addresses. This can also be artificially created by assigning different IP addresses to the same device (called IP aliasing). 
 
LInux中為同一個物理網絡卡增加多個ip地址,以前通過ifconfig命令來建立和維護ip alias, 而在新的IPROUTE2中通過ip address命令來建立和維護Primary address與Secondary address。
在每一個介面上可以配置多個Primary地址和多個Secondary地址。
對一個特定的網路掩碼(例子中的網路掩碼為/24),只能有一個Primary地址。
在路由程式碼中對許多事件和條件作出響應依賴於IP地址為Primary地址還是Secondary地址。下面給一些例子:

 Primary addresses contribute to the entropy of the CPU that happens to run the code that applies the configuration.
 當刪除一個Primary地址時,所有相關的Secondary地址也被刪除。但通過/proc可以配置一個選項,在當前Primary地址被刪除時可以將Secondary地址提升為Primary地址。
 當主機為本地生成的流量選擇源IP地址時,只考慮Primary地址。

2、解決辦法
使用ip addr show 命令檢視ip的狀態
  eth0: mtu 1500 qdisc pfifo_fast qlen 1000  
    link/ether 00:0c:29:c8:9b:3c brd ff:ff:ff:ff:ff:ff
    inet 192.168.7.191/24 brd 192.168.7.255 scope global eth0
    inet 1.42.9.162/27 brd 1.42.9.191 scope global eth0:1
    inet 1.42.9.163/27 brd 1.42.9.191 scope global secondary eth0:2
    inet 1.42.9.186/27 brd 1.42.9.191 scope global secondary eth0:3
    inet 1.42.9.182/27 brd 1.42.9.191 scope global secondary eth0:4
可以發現,紅色部分表示eth0:1是掩碼為27網段的Primary地址,當我們刪除掉這個地址時,下面的輔助地址也被刪除了。

設定引數: /sbin/sysctl net.ipv4.conf.eth0.promote_secondaries=1 (晉升輔助ip地址)
If this is enabled, and primary address of an interface gets deleted, an alias of the interface (secondary) will be upgraded to become primary.

The default is to purge all the secondaries when you delete the primary

設定重啟後仍然生效:echo "net.ipv4.conf.eth0.promote_secondaries=1" >>/etc/sysctl.conf

https://blog.csdn.net/suyingshipp/article/details/51145691