首先执行ifconfig,查看网卡设备名称
[root@localhost conf]# ifconfigens33: flags=4163mtu 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 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 up326 route add -host 192.168.3.123 dev ens33:1327 ifconfig328 ifconfig ens33:2 192.168.3.125 broadcast 192.168.3.255 netmask 255.255.255.0 up329 route add -host 192.168.3.125 dev ens33:2330 ifconfig331 history[root@localhost conf]# ifconfigens33: flags=4163mtu 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 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 0ens33:1: flags=4163 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 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 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 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 0virbr0: flags=4099 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.128PING 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 ms64 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 1004msrtt min/avg/max/mdev = 0.253/0.266/0.280/0.021 msroot@BP:~# ping 192.168.3.125PING 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 ms64 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 1022msrtt min/avg/max/mdev = 0.192/0.274/0.357/0.084 msroot@BP:~# ping 192.168.3.123PING 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 ms64 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 1030msrtt min/avg/max/mdev = 0.209/0.237/0.266/0.032 ms