添加回环地址的命令和说明如下:
#添加一个回环地址到lo网卡,添加多个可以改lo:后边的序号
ifconfig lo: [要添加的地址] netmask 255.255.255.255 broadcast [要添加的地址]
#增加本地路由
/sbin/route add -host [要添加的地址] dev lo:
#禁止lo网卡的arp的发出,并屏蔽arp消息
echo "" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "" >/proc/sys/net/ipv4/conf/lo/arp_announce
#禁止网卡默认配置的arp的发出,并屏蔽arp消息
echo "" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "" >/proc/sys/net/ipv4/conf/all/arp_announce
#内核运行时动态修改内核参数,从文件中读取配置,可以让配置马上生效
sysctl -p >/dev/null >&
删除回环地址:
ifconfig lo: down
route del $SNS_VIP >/dev/null >&
echo "" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "" >/proc/sys/net/ipv4/conf/all/arp_announce
实例脚本一枚:
SNS_VIP=$
SNS_VIP_INDEX=$
#/etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig lo:${SNS_VIP_INDEX:-} $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
/sbin/route add -host $SNS_VIP dev lo:
echo "" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "" >/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p >/dev/null >&
echo "RealServer Start OK"
;;
stop)
ifconfig lo:${SNS_VIP_INDEX:-} down
route del $SNS_VIP >/dev/null >&
echo "" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "RealServer Stoped"
;;
*)
echo "Usage: $0 {start|stop}"
exit
esac
exit
使用方法:
在CentOS7中随便编辑个文件,贴上以上内容,并执行 chmod +x [文件名],使该文件具备执行权限。
脚本命令示例:
#增加
./[文件名] start 192.168.1.110
#删除
./[文件名] stop 192.168.1.110 #增加第二个(第N个类似,需要把最后数字变成N-),后边多了个数字,删除的时候也要对应起来
./[文件名] start 192.168.1.111
#删除
./[文件名] stop 192.168.1.111 #查看效果,可以看到lo网卡上绑定了多个回环IP
ip a