CentOS 7 单网卡配置多个地址

centos7  

查看网卡名和地址

  • ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1  
    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: enp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000  
    link/ether 60:45:cb:82:20:35 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.13/24 brd 192.168.1.255 scope global enp2s0
       valid_lft forever preferred_lft forever
    inet6 fe80::6245:cbff:fe82:2035/64 scope link 
       valid_lft forever preferred_lft forever
  • 如上所见,我的网卡名是enp2s0,ip地址是192.168.1.13

修改网卡的配置在/etc/sysconfig/network-scripts/路径下面

  • vim ifcfg-enp2s0
TYPE="Ethernet"  
BOOTPROTO="static"  
DEFROUTE="yes"  
PEERDNS="yes"  
PEERROUTES="yes"  
IPV4_FAILURE_FATAL="no"  
NAME="enp2s0"  
UUID="daeab084-aece-45b7-b3f8-13e2fcaa3b33"  
DEVICE="enp2s0"  
ONBOOT="yes"  
IPADDR0="192.168.1.13"         #配置地址  
IPADDR1="192.168.1.14"  
IPADDR2="192.168.1.15"  
PREFIX0="24"                   #网络号  
GATEWAY0="192.168.1.1"         #网关  
DNS1="192.168.1.1"             #DNS地址  
DNS2="8.8.8.8"  
  • 现在加了两个IP地址:IPADDR1=”192.168.1.14″ & IPADDR2=”192.168.1.15″

重启网卡生效

  • systemctl restart network

查看是否配置成功

  • ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1  
    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: enp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000  
    link/ether 60:45:cb:82:20:35 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.13/24 brd 192.168.1.255 scope global enp2s0
       valid_lft forever preferred_lft forever
    inet 192.168.1.14/24 brd 192.168.1.255 scope global secondary enp2s0
       valid_lft forever preferred_lft forever
    inet 192.168.1.15/24 brd 192.168.1.255 scope global secondary enp2s0
       valid_lft forever preferred_lft forever
    inet6 fe80::6245:cbff:fe82:2035/64 scope link 
       valid_lft forever preferred_lft forever
  • ping 测试
[root@]#ping 192.168.1.14 -c1
PING 192.168.1.14 (192.168.1.14) 56(84) bytes of data.  
64 bytes from 192.168.1.14: icmp_seq=1 ttl=64 time=0.029 ms

--- 192.168.1.14 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms  
rtt min/avg/max/mdev = 0.029/0.029/0.029/0.000 ms  
[root@]# ping 192.168.1.15 -c1
PING 192.168.1.15 (192.168.1.15) 56(84) bytes of data.  
64 bytes from 192.168.1.15: icmp_seq=1 ttl=64 time=0.027 ms

--- 192.168.1.15 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms  
rtt min/avg/max/mdev = 0.027/0.027/0.027/0.000 ms