2019年 · Linux

Linux增加/删除虚拟ip

在网卡eth1上添加一个虚拟ip 192.168.53.209
1.添加ip
root@ubuntu:~# ip addr add 192.168.53.209/32 dev eth0

2.查看添加的虚拟ip
root@ubuntu:~# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:83:a7:64 brd ff:ff:ff:ff:ff:ff
inet 192.168.53.208/24 brd 192.168.53.255 scope global eth0
valid_lft forever preferred_lft forever
inet 192.168.53.209/32 scope global eth0        #=============> 新增的IP
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe83:a764/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:83:a7:6e brd ff:ff:ff:ff:ff:ff
inet 172.16.10.151/24 brd 172.16.10.255 scope global eth1
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe83:a76e/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever

3.测试网络是否可通
root@ubuntu:~# ping 192.168.53.209
PING 192.168.53.209 (192.168.53.209) 56(84) bytes of data.
64 bytes from 192.168.53.209: icmp_seq=1 ttl=64 time=0.038 ms
64 bytes from 192.168.53.209: icmp_seq=2 ttl=64 time=0.034 ms
^C
— 192.168.53.209 ping statistics —
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.034/0.036/0.038/0.002 ms

4.删除虚拟IP
root@ubuntu:~# ip addr del 192.168.53.209/32 dev eth0

5. 设置开启自动添加虚拟IP
root@ubuntu:~# cat /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will “exit 0” on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
ip addr add 192.168.53.209/32 dev eth0              #服务器启动添加虚拟IP
exit 0