有时,你也许想要给一个网卡多个地址你该怎么做呢?另外买一个网卡来分配地址?在小型网络中其实不用这么做我们现在可以在CentOS/RHEL 7中给一个网卡分配多个IP地址想知道怎么做么?好的,跟随我,这并不难。
首先,让我们找到网卡的IP地址在我的CentOS 7服务器中,我只使用了一个网卡用root特权运行下面的命令:ip addr 示例输出:1: lo: mtu 65536 qdisc noqueue state UNKNOWN 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: enp0s3: mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:80:63:19 brd ff:ff:ff:ff:ff:ff inet 192.168.1.150/24 brd 192.168.1.255 scope global enp0s3 valid_lft forever preferred_lft forever 。
如上所见,我的网卡名是enp0s3,ip地址是192.168.1.150如你所知,网卡的配置文件存储在 /etc/sysconfig/network-scripts/ 目录下每个网卡的详细内容将会以不同的名字存储,比如ifcfg-enp0s3。
让我们看下ifcfg-enp0s3的细节cat /etc/sysconfig/network-scripts/ifcfg-enp0s3 示例输出:TYPE=”Ethernet” BOOTPROTO=”none” DEFROUTE=”yes” IPV4_FAILURE_FATAL=”no” IPV6INIT=”yes” IPV6_AUTOCONF=”yes” IPV6_DEFROUTE=”yes” IPV6_FAILURE_FATAL=”no” NAME=”enp0s3″ UUID=”e9f9caef-cb9e-4a19-aace-767c6ee6f849″ ONBOOT=”yes” HWADDR=”08:00:27:80:63:19″ IPADDR0=”192.168.1.150″ PREFIX0=”24″ GATEWAY0=”192.168.1.1″ DNS1=”192.168.1.1″ IPV6_PEERDNS=”yes” IPV6_PEERROUTES=”yes” 。
好的,现在我们将在相同的子网中分配多个地址了编辑文件 /etc/sysconfig/network-scripts/ifcfg-enp0s3:vi /etc/sysconfig/network-scripts/ifcfg-enp0s3 。
像下面那样加入额外的IP地址TYPE=”Ethernet” BOOTPROTO=”none” DEFROUTE=”yes” IPV4_FAILURE_FATAL=”no” IPV6INIT=”yes” IPV6_AUTOCONF=”yes” IPV6_DEFROUTE=”yes” IPV6_FAILURE_FATAL=”no” NAME=”enp0s3″ UUID=”933cdc9b-b383-4ddd-b219-5a72c69c9cf0″ ONBOOT=”yes” HWADDR=”08:00:27:3F:AB:68″ IPADDR0=”192.168.1.150″ IPADDR1=”192.168.1.151″ IPADDR2=”192.168.1.152″ PREFIX0=”24″ GATEWAY0=”192.168.1.1″ DNS1=”192.168.1.1″ IPV6_PEERDNS=”yes” IPV6_PEERROUTES=”yes” 。
如你所见,我已经加了两个IP地址:IPADDR1=”192.168.1.151″ & IPADDR2=”192.168.1.152″类似地,你可以加入更多的ip地址最后,保存并退出文件重启网络服务来使更改生效。
systemctl restart network 现在,让我们检查是否已经加入了IP地址ip addr 示例输出:lo: mtu 65536 qdisc noqueue state UNKNOWN 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: enp0s3: mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:3f:ab:68 brd ff:ff:ff:ff:ff:ff inet 192.168.1.150/24 brd 192.168.1.255 scope global enp0s3 valid_lft forever preferred_lft forever inet 192.168.1.151/24 brd 192.168.1.255 scope global secondary enp0s3 valid_lft forever preferred_lft forever inet 192.168.1.152/24 brd 192.168.1.255 scope global secondary enp0s3 valid_lft forever preferred_lft forever inet6 fe80::a00:27ff:fe3f:ab68/64 scope link valid_lft forever preferred_lft forever 。
如你所见,单个网卡已经有3个IP地址了让我们ping一下新增的IP地址:ping -c 4 192.168.1.151 示例输出:PING 192.168.1.151 (192.168.1.151) 56(84) bytes of data. 64 bytes from 192.168.1.151: icmp_seq=1 ttl=64 time=0.048 ms 64 bytes from 192.168.1.151: icmp_seq=2 ttl=64 time=0.075 ms 64 bytes from 192.168.1.151: icmp_seq=3 ttl=64 time=0.077 ms 64 bytes from 192.168.1.151: icmp_seq=4 ttl=64 time=0.077 ms — 192.168.1.151 ping statistics — 4 packets transmitted, 4 received, 0% packet loss, time 2999ms rtt min/avg/max/mdev = 0.048/0.069/0.077/0.013 ms 。
ping -c 4 192.168.1.152 示例输出:PING 192.168.1.152 (192.168.1.152) 56(84) bytes of data. 64 bytes from 192.168.1.152: icmp_seq=1 ttl=64 time=0.034 ms 64 bytes from 192.168.1.152: icmp_seq=2 ttl=64 time=0.075 ms 64 bytes from 192.168.1.152: icmp_seq=3 ttl=64 time=0.073 ms 64 bytes from 192.168.1.152: icmp_seq=4 ttl=64 time=0.075 ms — 192.168.1.152 ping statistics — 4 packets transmitted, 4 received, 0% packet loss, time 2999ms rtt min/avg/max/mdev = 0.034/0.064/0.075/0.018 ms
如果你想要使用不同的子网,你要改变PREFIX0=24成不同的子网,比如 PREFIX1=16比如,我想要添加一个A类地址(*比如10.0.0.1)到我的网卡中TYPE=”Ethernet” BOOTPROTO=”none” DEFROUTE=”yes” IPV4_FAILURE_FATAL=”no” IPV6INIT=”yes” IPV6_AUTOCONF=”yes” IPV6_DEFROUTE=”yes” IPV6_FAILURE_FATAL=”no” NAME=”enp0s3″ UUID=”933cdc9b-b383-4ddd-b219-5a72c69c9cf0″ ONBOOT=”yes” HWADDR=”08:00:27:3F:AB:68″ IPADDR0=”192.168.1.150″ IPADDR1=”192.168.1.151″ IPADDR2=”192.168.1.152″ IPADDR3=”10.0.0.1″ PREFIX0=”24″ PREFIX1=16 GATEWAY0=”192.168.1.1″ DNS1=”192.168.1.1″ IPV6_PEERDNS=”yes” IPV6_PEERROUTES=”yes” 。
你可以看到我已经添加一个A类地址(10.0.0.1)并且前缀是16保存并退出文件重启网络服务,接着,ping新增的地址:ping -c 4 10.0.0.1 示例输出:PING 10.0.0.1 (10.0.0.1) 56(84) bytes of data. 64 bytes from 10.0.0.1: icmp_seq=1 ttl=64 time=0.097 ms 64 bytes from 10.0.0.1: icmp_seq=2 ttl=64 time=0.073 ms 64 bytes from 10.0.0.1: icmp_seq=3 ttl=64 time=0.074 ms 64 bytes from 10.0.0.1: icmp_seq=4 ttl=64 time=0.075 ms — 10.0.0.1 ping statistics — 4 packets transmitted, 4 received, 0% packet loss, time 3000ms rtt min/avg/max/mdev = 0.073/0.079/0.097/0.014 ms 。
相似地,你可以添加不同的网关,就是这样。