使用下面的命令启动多个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