给网卡设备添加两个IP别名(一个网卡绑定多个ip)

首先执行ifconfig,查看网卡设备名称

[root@localhost conf]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.3.128  netmask 255.255.255.0  broadcast 192.168.3.255
        inet6 fe80::6b14:b58e:faa2:525  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:5e:7c:27  txqueuelen 1000  (Ethernet)
        RX packets 46410  bytes 31588589 (30.1 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 25430  bytes 3803612 (3.6 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

可以看到网卡设备名称为ens33
下面我们给该设备添加ip别名

325  ifconfig ens33:1 192.168.3.123 broadcast 192.168.3.255 netmask 255.255.255.0 up
326  route add -host 192.168.3.123 dev ens33:1
327  ifconfig
328  ifconfig ens33:2 192.168.3.125 broadcast 192.168.3.255 netmask 255.255.255.0 up
329  route add -host 192.168.3.125 dev ens33:2
330  ifconfig
331  history
[root@localhost conf]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.3.128  netmask 255.255.255.0  broadcast 192.168.3.255
        inet6 fe80::6b14:b58e:faa2:525  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:5e:7c:27  txqueuelen 1000  (Ethernet)
        RX packets 47199  bytes 31648090 (30.1 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 25448  bytes 3805064 (3.6 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.3.123  netmask 255.255.255.0  broadcast 192.168.3.255
        ether 00:0c:29:5e:7c:27  txqueuelen 1000  (Ethernet)

ens33:2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.3.125  netmask 255.255.255.0  broadcast 192.168.3.255
        ether 00:0c:29:5e:7c:27  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 184495  bytes 29154176 (27.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 184495  bytes 29154176 (27.8 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:61:e7:bc  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@localhost conf]# 

上面直接通过命令行配置的ip别名会在系统重启后失效,如果想让系统开机时自动运行,需要把那四条命令写入到/etc/rc.local文件里(该文件是系统启动时自动执行的文件)
这种方法执行的效果跟我们在虚拟机里面增加虚拟网卡后进行配置ip的效果是一样的
下面是我用物理机对这些ip进行ping的效果

root@BP:~# ping 192.168.3.128
PING 192.168.3.128 (192.168.3.128) 56(84) bytes of data.
64 bytes from 192.168.3.128: icmp_seq=1 ttl=64 time=0.280 ms
64 bytes from 192.168.3.128: icmp_seq=2 ttl=64 time=0.253 ms
^C
--- 192.168.3.128 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1004ms
rtt min/avg/max/mdev = 0.253/0.266/0.280/0.021 ms
root@BP:~# ping 192.168.3.125
PING 192.168.3.125 (192.168.3.125) 56(84) bytes of data.
64 bytes from 192.168.3.125: icmp_seq=1 ttl=64 time=0.357 ms
64 bytes from 192.168.3.125: icmp_seq=2 ttl=64 time=0.192 ms
^C
--- 192.168.3.125 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1022ms
rtt min/avg/max/mdev = 0.192/0.274/0.357/0.084 ms
root@BP:~# ping 192.168.3.123
PING 192.168.3.123 (192.168.3.123) 56(84) bytes of data.
64 bytes from 192.168.3.123: icmp_seq=1 ttl=64 time=0.266 ms
64 bytes from 192.168.3.123: icmp_seq=2 ttl=64 time=0.209 ms
^C
--- 192.168.3.123 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1030ms
rtt min/avg/max/mdev = 0.209/0.237/0.266/0.032 ms