用root特权运行下面的命令:

  1. ip addr

示例输出: 

  1. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
  2. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  3. inet 127.0.0.1/8 scope host lo
  4. valid_lft forever preferred_lft forever
  5. inet6 ::1/128 scope host
  6. valid_lft forever preferred_lft forever
  7. 2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
  8. link/ether 08:00:27:80:63:19 brd ff:ff:ff:ff:ff:ff
  9. inet 192.168.1.150/24 brd 192.168.1.255 scope global enp0s3
  10. valid_lft forever preferred_lft forever

如上所见,我的网卡名是enp0s3,ip地址是192.168.1.150。

如你所知,网卡的配置文件存储在 /etc/sysconfig/network-scripts/ 目录下。每个网卡的详细内容将会以不同的名字存储,比如ifcfg-enp0s3

让我们看下ifcfg-enp0s3的细节。 

  1. cat /etc/sysconfig/network-scripts/ifcfg-enp0s3

示例输出: 

  1. TYPE="Ethernet"
  2. BOOTPROTO="none"
  3. DEFROUTE="yes"
  4. IPV4_FAILURE_FATAL="no"
  5. IPV6INIT="yes"
  6. IPV6_AUTOCONF="yes"
  7. IPV6_DEFROUTE="yes"
  8. IPV6_FAILURE_FATAL="no"
  9. NAME="enp0s3"
  10. UUID="e9f9caef-cb9e-4a19-aace-767c6ee6f849"
  11. ONBOOT="yes"
  12. HWADDR="08:00:27:80:63:19"
  13. IPADDR0="192.168.1.150"
  14. PREFIX0="24"
  15. GATEWAY0="192.168.1.1"
  16. DNS1="192.168.1.1"
  17. IPV6_PEERDNS="yes"
  18. IPV6_PEERROUTES="y

好的,现在我们将在相同的子网中分配多个地址了。

编辑文件 /etc/sysconfig/network-scripts/ifcfg-enp0s3 

  1. vi /etc/sysconfig/network-scripts/ifcfg-enp0s3

像下面那样加入额外的IP地址。 

  1. TYPE="Ethernet"
  2. BOOTPROTO="none"
  3. DEFROUTE="yes"
  4. IPV4_FAILURE_FATAL="no"
  5. IPV6INIT="yes"
  6. IPV6_AUTOCONF="yes"
  7. IPV6_DEFROUTE="yes"
  8. IPV6_FAILURE_FATAL="no"
  9. NAME="enp0s3"
  10. UUID="933cdc9b-b383-4ddd-b219-5a72c69c9cf0"
  11. ONBOOT="yes"
  12. HWADDR="08:00:27:3F:AB:68"
  13. IPADDR0="192.168.1.150"
  14. IPADDR1="192.168.1.151"
  15. IPADDR2="192.168.1.152"
  16. PREFIX0="24"
  17. GATEWAY0="192.168.1.1"
  18. DNS1="192.168.1.1"
  19. IPV6_PEERDNS="yes"
  20. IPV6_PEERROUTES="yes"

如你所见,我已经加了两个IP地址:IPADDR1=”192.168.1.151″ & IPADDR2=”192.168.1.152″

类似地,你可以加入更多的ip地址。

***,保存并退出文件。重启网络服务来使更改生效。 

  1. systemctl restart network

现在,让我们检查是否已经加入了IP地址。 

  1. ip addr

示例输出: 

  1. : lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
  2. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  3. inet 127.0.0.1/8 scope host lo
  4. valid_lft forever preferred_lft forever
  5. inet6 ::1/128 scope host
  6. valid_lft forever preferred_lft forever
  7. 2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
  8. link/ether 08:00:27:3f:ab:68 brd ff:ff:ff:ff:ff:ff
  9. inet 192.168.1.150/24 brd 192.168.1.255 scope global enp0s3
  10. valid_lft forever preferred_lft forever
  11. inet 192.168.1.151/24 brd 192.168.1.255 scope global secondary enp0s3
  12. valid_lft forever preferred_lft forever
  13. inet 192.168.1.152/24 brd 192.168.1.255 scope global secondary enp0s3
  14. valid_lft forever preferred_lft forever
  15. inet6 fe80::a00:27ff:fe3f:ab68/64 scope link
  16. valid_lft forever preferred_lft forever

如你所见,单个网卡已经有3个IP地址了。

让我们ping一下新增的IP地址: 

  1. ping -c 4 192.168.1.151

示例输出: 

  1. PING 192.168.1.151 (192.168.1.151) 56(84) bytes of data.
  2. 64 bytes from 192.168.1.151: icmp_seq=1 ttl=64 time=0.048 ms
  3. 64 bytes from 192.168.1.151: icmp_seq=2 ttl=64 time=0.075 ms
  4. 64 bytes from 192.168.1.151: icmp_seq=3 ttl=64 time=0.077 ms
  5. 64 bytes from 192.168.1.151: icmp_seq=4 ttl=64 time=0.077 ms
  6.  
  7. --- 192.168.1.151 ping statistics ---
  8. 4 packets transmitted, 4 received, 0% packet loss, time 2999ms
  9. rtt min/avg/max/mdev = 0.048/0.069/0.077/0.013 ms 
  1. ping -c 4 192.168.1.152

示例输出: 

  1. PING 192.168.1.152 (192.168.1.152) 56(84) bytes of data.
  2. 64 bytes from 192.168.1.152: icmp_seq=1 ttl=64 time=0.034 ms
  3. 64 bytes from 192.168.1.152: icmp_seq=2 ttl=64 time=0.075 ms
  4. 64 bytes from 192.168.1.152: icmp_seq=3 ttl=64 time=0.073 ms
  5. 64 bytes from 192.168.1.152: icmp_seq=4 ttl=64 time=0.075 ms
  6.  
  7. --- 192.168.1.152 ping statistics ---
  8. 4 packets transmitted, 4 received, 0% packet loss, time 2999ms
  9. rtt min/avg/max/mdev = 0.034/0.064/0.075/0.018 ms.

如果你想要使用不同的子网,你要改变PREFIX0=24成不同的子网,比如 PREFIX1=16

比如,我想要添加一个A类地址(*比如10.0.0.1)到我的网卡中。 

  1. TYPE="Ethernet"
  2. BOOTPROTO="none"
  3. DEFROUTE="yes"
  4. IPV4_FAILURE_FATAL="no"
  5. IPV6INIT="yes"
  6. IPV6_AUTOCONF="yes"
  7. IPV6_DEFROUTE="yes"
  8. IPV6_FAILURE_FATAL="no"
  9. NAME="enp0s3"
  10. UUID="933cdc9b-b383-4ddd-b219-5a72c69c9cf0"
  11. ONBOOT="yes"
  12. HWADDR="08:00:27:3F:AB:68"
  13. IPADDR0="192.168.1.150"
  14. IPADDR1="192.168.1.151"
  15. IPADDR2="192.168.1.152"
  16. IPADDR3="10.0.0.1"
  17. PREFIX0="24"
  18. PREFIX1=16
  19. GATEWAY0="192.168.1.1"
  20. DNS1="192.168.1.1"
  21. IPV6_PEERDNS="yes"
  22. IPV6_PEERROUTES="yes"

你可以看到我已经添加一个A类地址(10.0.0.1)并且前缀是16。

保存并退出文件。重启网络服务,接着,ping新增的地址: 

  1. ping -c 4 10.0.0.1

示例输出:

  1. PING 10.0.0.1 (10.0.0.1) 56(84) bytes of data.
  2. 64 bytes from 10.0.0.1: icmp_seq=1 ttl=64 time=0.097 ms
  3. 64 bytes from 10.0.0.1: icmp_seq=2 ttl=64 time=0.073 ms
  4. 64 bytes from 10.0.0.1: icmp_seq=3 ttl=64 time=0.074 ms
  5. 64 bytes from 10.0.0.1: icmp_seq=4 ttl=64 time=0.075 ms
  6.  
  7. --- 10.0.0.1 ping statistics ---
  8. 4 packets transmitted, 4 received, 0% packet loss, time 3000ms
  9. rtt min/avg/max/mdev = 0.073/0.079/0.097/0.014 ms

相似地,你可以添加不同的网关。