文章目录
第1章 用ip命令添加/删除虚拟IP
1.1 添加单个虚拟IP时指定标签
#### 添加
[root@node21 ~]# ip addr add 10.0.0.30/24 dev ens33 label ens33:30
[root@node21 ~]#
#### 查看
[root@node21 ~]# ip addr show|grep "10.0.0.30"
inet 10.0.0.30/24 scope global secondary ens33:30
[root@node21 ~]#
[root@node21 ~]# ifconfig ens33:30 # ifconfig命令之所以能看到是因为在添加时用label
ens33:30: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.0.30 netmask 255.255.255.0 broadcast 0.0.0.0
ether 00:0c:29:e5:fc:f9 txqueuelen 1000 (Ethernet)
#### 检查是否正常
[root@node21 ~]# ping -c 2 10.0.30|head -3
PING 10.0.30 (10.0.0.30) 56(84) bytes of data.
64 bytes from 10.0.0.30: icmp_seq=1 ttl=64 time=0.012 ms
64 bytes from 10.0.0.30: icmp_seq=2 ttl=64 time=0.028 ms
#### 删除
[root@node21 ~]# ip addr del 10.0.0.30/24
Not enough information: "dev" argument is required. # 报错,让你指定设置,前面在添加时加了label ens33:30
[root@node21 ~]#
[root@node21 ~]# ip addr del 10.0.0.30/24 dev ens33:30
[root@node21 ~]#
1.2 添加单个虚拟IP时不指定标签
#### 添加
[root@node21 ~]# ip addr add 10.0.0.30/24 dev ens33
[root@node21 ~]#
#### 查看
[root@node21 ~]# ifconfig|grep "10.0.0.30" # 是看不到的
[root@node21 ~]#
[root@node21 ~]# ip addr show|grep "10.0.0.30" # 是可以看到的
inet 10.0.0.30/24 scope global secondary ens33 <== 这里是ens33
#### 检查是否正常
[root@node21 ~]# ping -c 2 10.0.30|head -3
PING 10.0.30 (10.0.0.30) 56(84) bytes of data.
64 bytes from 10.0.0.30: icmp_seq=1 ttl=64 time=0.012 ms
64 bytes from 10.0.0.30: icmp_seq=2 ttl=64 time=0.028 ms
#### 删除
[root@node21 ~]# ip addr del 10.0.0.30/24 dev ens33
[root@node21 ~]#
#### 查看
[root@node21 ~]# ip addr show|grep "10.0.0.30"
[root@node21 ~]#
1.3 添加多个虚拟IP同时指定标签
##### 在ens33网卡上添加多个虚拟IP,同时指定标签
[root@node21 ~]# ip addr add 10.0.0.30/24 dev ens33 label ens33:30
[root@node21 ~]# ip addr add 10.0.0.31/24 dev ens33 label ens33:31
[root@node21 ~]#
##### 查看添加的虚拟IP
[root@node21 ~]# ip addr show|grep -E "10.0.0.30|10.0.0.31"
inet 10.0.0.30/24 scope global secondary ens33:30
inet 10.0.0.31/24 scope global secondary ens33:31
##### 检测添加的虚拟IP
[root@node21 ~]# ping -c 2 10.0.0.30|head -3
PING 10.0.0.30 (10.0.0.30) 56(84) bytes of data.
64 bytes from 10.0.0.30: icmp_seq=1 ttl=64 time=0.016 ms
64 bytes from 10.0.0.30: icmp_seq=2 ttl=64 time=0.026 ms
[root@node21 ~]#
[root@node21 ~]# ping -c 2 10.0.0.31|head -3
PING 10.0.0.31 (10.0.0.31) 56(84) bytes of data.
64 bytes from 10.0.0.31: icmp_seq=1 ttl=64 time=0.013 ms
64 bytes from 10.0.0.31: icmp_seq=2 ttl=64 time=0.027 ms
###### 删除10.0.0.30虚拟IP
[root@node21 ~]# ip addr del 10.0.0.30/24 dev ens33:30
[root@node21 ~]#
####### 检查是否删除成功,以及10.0.0.31是否存在
[root@node21 ~]# ip addr show|grep -E "10.0.0.30|10.0.0.31"
inet 10.0.0.31/24 scope global secondary ens33:31
1.3 总结
01:添加虚拟IP时建议为其指定标签(label)
02:这样用ifconfig命令也可查看,用ip addr show也可查看
03:在删除时也减少出错的概率;
04:重启network服务虚拟IP会消失,不想让其消失,有方法(根据你的场景)
04:当网卡设置被ifdown掉再ifup起来时,虚拟IP也会消失,不想让其消失,有方法(根据你的场景)
第2章 用ifconfig命令添加/删除虚拟IP
Centos 7操作系统如果是最小化安装,是没有ifconfig命令,可用yum install net-tools -y命令进行安装