上课作业有一个脚本,是批量添加ip别名,批量删除ip别名,脚本如下
[root@loadblance scripts]# cat ip.sh #!/bin/bash function up { for i in `seq 1 16`;do if [ $i -eq 10 ];then continue fi ifconfig eth0:$i 10.0.2.$i netmask 255.255.255.0 up done } function down { for i in `seq 1 16`;do if [ $i -eq 10 ];then continue fi ifconfig eth0:$i down done } case $1 in up)up #ifconfig eth0:0 down ;; down)down;; esac
脚本执行批量添加没有什么问题,但在执行批量删除时出现如下报错
[root@loadblance scripts]# bash ip.sh down SIOCSIFFLAGS: Cannot assign requested address SIOCSIFFLAGS: Cannot assign requested address SIOCSIFFLAGS: Cannot assign requested address SIOCSIFFLAGS: Cannot assign requested address SIOCSIFFLAGS: Cannot assign requested address
后来分析发现是在批量删除的for循环执行第一次循环 ifconfig eth0:1 down的时候,就把所有的ip别名给删除了,导致后面的循环都会报错。
但是为什么执行ifconfig eth0:1 down就会把所有的别名ip都给down掉了呢,后来查了一下,翻到了redhat的bugzilla里面有篇记录,大体翻译如下:
新添加的ip地址如果是和原有的ip的子网相同,那么新添加的ip都将成为原有ip的secondary ,原有的ip称为primary address。当primary被删除后,secondary都会被删除。而如果只删除某个secondary,不会影响其他的ip地址。查看这些信息,建议用ip addr命令。
再看看我们的环境(如下),由于新添加的eth0:1和eth0并不是同一个子网,所以新添加的eth0:1是一个新的primary address,其他的eth0:2-eth0:16是eth0:1的secondary,正因为如此,按照上面理论,当删除eth0:1时,其他ip别名都会自动被干掉。
[root@loadblance scripts]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000 link/ether 00:0c:29:29:3f:fd brd ff:ff:ff:ff:ff:ff inet 192.168.1.9/24 brd 192.168.1.255 scope global eth0 inet 10.0.2.1/24 brd 10.0.2.255 scope global eth0:1 inet 10.0.2.2/24 brd 10.0.2.255 scope global secondary eth0:2 inet 10.0.2.3/24 brd 10.0.2.255 scope global secondary eth0:3 inet 10.0.2.4/24 brd 10.0.2.255 scope global secondary eth0:4 inet 10.0.2.5/24 brd 10.0.2.255 scope global secondary eth0:5 inet 10.0.2.6/24 brd 10.0.2.255 scope global secondary eth0:6 inet 10.0.2.7/24 brd 10.0.2.255 scope global secondary eth0:7 inet 10.0.2.8/24 brd 10.0.2.255 scope global secondary eth0:8 inet 10.0.2.9/24 brd 10.0.2.255 scope global secondary eth0:9 inet 10.0.2.11/24 brd 10.0.2.255 scope global secondary eth0:11 inet 10.0.2.12/24 brd 10.0.2.255 scope global secondary eth0:12 inet 10.0.2.13/24 brd 10.0.2.255 scope global secondary eth0:13 inet 10.0.2.14/24 brd 10.0.2.255 scope global secondary eth0:14 inet 10.0.2.15/24 brd 10.0.2.255 scope global secondary eth0:15 inet 10.0.2.16/24 brd 10.0.2.255 scope global secondary eth0:16
解决方法:修改脚本,把所有新添加的ip别名的都设成和eth0一个网段的,这样所有新加ip别名都是eth0的secondary了,任意删除一个secondary不会影响其他的ip别名。
转载于:https://blog.51cto.com/daiqingyang/1330938