Linux/CentOS7给一个网卡设定多个IP地址, Linux网卡配置虚拟IP
此文章摘自书籍: (跟阿铭学Linux第十三章 13.3.2部分内容)
案发现场
login as: root
root@127.1.1.2's password:
Last login: Tue May 15 12:09:25 2018 from 172.1.0.1
[root@centos7_client ~]# ip addr
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:9e:8a:37 brd ff:ff:ff:ff:ff:ff
inet 127.1.1.1 /24 brd 10.93.143.255 scope global ens160 目前这里只有一个IP地址
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe9e:8a37/64 scope link
valid_lft forever preferred_lft forever
解决方式:
[root@centos7_client ~]# cd /etc/sysconfig/network-scripts/
[root@centos7_client network-scripts]# cp ifcfg-ens160 ifcfg-ens160\:1 反斜杠\用来转义 因为要把(:)进行转义,不然Linux命令行则无法识别。
[root@centos7_client network-scripts]# vim ifcfg-ens160:1
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"
IPADDR=127.1.1.8 需要修改的参数如下:IPADDR NAME DEVICE
NETMASK=255.255.255.0
GATEWAY=0.0.0.0
DNS1=0.0.0.0
NAME="ens160:1"
UUID="c4bf48f7-637e-4130-b428-3b1e589e7235"
DEVICE="ens160:1"
ONBOOT="yes"
[root@centos7_client network-scripts]# ifdown ens160 && ifup ens160 重启网卡指令, 在下有试过使用:systemctl start network 似乎启动虚拟网卡失败。
Device 'ens160' successfully disconnected.
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/2)
[root@centos7_client network-scripts]# 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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:9e:8a:37 brd ff:ff:ff:ff:ff:ff
inet 127.1.1.1.2/24 brd 10.93.143.255 scope global ens160
valid_lft forever preferred_lft forever
inet 127.1.1.8/24 brd 10.93.143.255 scope global secondary ens160:1 127.1.1.8是新增的IP,可以用来建立Apache网页或做其它作用,请自定义。
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe9e:8a37/64 scope link
valid_lft forever preferred_lft forever
[root@centos7_client network-scripts]#
删除虚拟网卡方式: 删除该网卡的配置文件,将设备重启即可。 这个方法在下亲测~~,跟书籍内容无关。
Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.
C:\windows\system32>ping 127.1.1.8
Pinging 10.93.143.8 with 32 bytes of data:
Reply from 127.1.1.8: bytes=32 time=788ms TTL=64
Reply from 127.1.1.8: bytes=32 time=19ms TTL=64
Reply from 127.1.1.8: bytes=32 time=16ms TTL=64
Reply from 127.1.1.8: bytes=32 time=16ms TTL=64
Ping statistics for 1.1.1.8:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 16ms, Maximum = 788ms, Average = 209ms
C:\windows\system32>