redhat 7/Centos 7 配置多个IP地址,目前已知有2种方法。一种是虚拟一个网卡出来,第二种是直接增加IP地址。后面验证了通过图形客户端修改的结果。
目录导航
方法1、增加虚拟网卡
该方法在redhat6系列一样有效,而且配置出来的IP可以通过ifconfig看到。当然ip addr更能看到了。
查看当前
当前只能看到一个IP地址。无关的输出我去掉了。
[root@gbase_rh7_003 network-scripts]# ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.2.103 netmask 255.255.255.0 broadcast 10.0.2.255
inet6 fe80::ab19:72ea:2472:cc28 prefixlen 64 scopeid 0x20<link>
ether 08:00:27:8e:d9:28 txqueuelen 1000 (Ethernet)
RX packets 105915 bytes 154096500 (146.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 9594 bytes 779720 (761.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@gbase_rh7_003 ~]# cd /etc/sysconfig/network-scripts/
[root@gbase_rh7_003 network-scripts]# cat ifcfg-enp0s3
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp0s3
UUID=054d2f84-2976-45a6-baa2-1420019b55f5
DEVICE=enp0s3
ONBOOT=yes
DNS1=114.114.114.114
IPADDR=10.0.2.103
PREFIX=24
GATEWAY=10.0.2.2
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
[root@gbase_rh7_003 network-scripts]#
复制一个配置文件
复制一份原始配置文件,后缀为:0
[root@gbase_rh7_003 network-scripts]# mv ifcfg-enp0s3_0 ifcfg-enp0s3:0
[root@gbase_rh7_003 network-scripts]# vi ifcfg-enp0s3:0
[root@gbase_rh7_003 network-scripts]# cat ifcfg-enp0s3:0
DEVICE="enp0s3:0"
IPADDR="10.0.2.104"
NETMASK="255.255.255.0"
ONBOOT="yes"
[root@gbase_rh7_003 network-scripts]#
重启网卡
重启网卡,然后通过ifconfig 可以看到新增加的虚拟网卡,以及配置的IP.
在ip addr里显示如下
inet 10.0.2.104/24 brd 10.0.2.255 scope global secondary enp0s3:0
valid_lft forever preferred_lft forever
[root@gbase_rh7_003 network-scripts]# service network restart
Shutting down interface enp0s3: Device 'enp0s3' successfully disconnected.
[ OK ]
Shutting down loopback interface: [ OK ]
Bringing up loopback interface: [ OK ]
Bringing up interface enp0s3: Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3)
[ OK ]
[root@gbase_rh7_003 network-scripts]#
[root@gbase_rh7_003 network-scripts]#
[root@gbase_rh7_003 network-scripts]# ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.2.103 netmask 255.255.255.0 broadcast 10.0.2.255
inet6 fe80::ab19:72ea:2472:cc28 prefixlen 64 scopeid 0x20<link>
ether 08:00:27:8e:d9:28 txqueuelen 1000 (Ethernet)
RX packets 106499 bytes 154142898 (147.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 9993 bytes 818761 (799.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
enp0s3:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.2.104 netmask 255.255.255.0 broadcast 10.0.2.255
ether 08:00:27:8e:d9:28 txqueuelen 1000 (Ethernet)
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 84 bytes 6748 (6.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 84 bytes 6748 (6.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:4a:d6:8a 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@gbase_rh7_003 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: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:8e:d9:28 brd ff:ff:ff:ff:ff:ff
inet 10.0.2.103/24 brd 10.0.2.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet 10.0.2.104/24 brd 10.0.2.255 scope global secondary enp0s3:0
valid_lft forever preferred_lft forever
inet6 fe80::ab19:72ea:2472:cc28/64 scope link
valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000
link/ether 52:54:00:4a:d6:8a brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000
link/ether 52:54:00:4a:d6:8a brd ff:ff:ff:ff:ff:ff
[root@gbase_rh7_003 network-scripts]#
确认IP可用
通过ping 可以连通。
[root@gbase_rh7_003 network-scripts]# ping 10.0.2.104
PING 10.0.2.104 (10.0.2.104) 56(84) bytes of data.
64 bytes from 10.0.2.104: icmp_seq=1 ttl=64 time=0.075 ms
64 bytes from 10.0.2.104: icmp_seq=2 ttl=64 time=0.077 ms
^C
--- 10.0.2.104 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.075/0.076/0.077/0.001 ms
方法2、直接在原有配置里增加IP地址
编辑原有的配置文件
我们再增加个IP,IPADDR1=10.0.2.105
[root@gbase_rh7_003 network-scripts]# cat ifcfg-enp0s3
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp0s3
UUID=054d2f84-2976-45a6-baa2-1420019b55f5
DEVICE=enp0s3
ONBOOT=yes
DNS1=114.114.114.114
IPADDR1=10.0.2.105
IPADDR=10.0.2.103
PREFIX=24
PREFIX1=24
GATEWAY=10.0.2.2
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
重启服务查看
注意,在ifconfig里是看不到我们第二种方法配置的IP的。而ip addr能看到。显示区别如下:就是名字不同,第一种是有自己独立的名字,第二种没有。
inet 10.0.2.105/24 brd 10.0.2.255 scope global secondary enp0s3
valid_lft forever preferred_lft forever
inet 10.0.2.104/24 brd 10.0.2.255 scope global secondary enp0s3:0
valid_lft forever preferred_lft forever
[root@gbase_rh7_003 network-scripts]# ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.2.103 netmask 255.255.255.0 broadcast 10.0.2.255
inet6 fe80::ab19:72ea:2472:cc28 prefixlen 64 scopeid 0x20<link>
ether 08:00:27:8e:d9:28 txqueuelen 1000 (Ethernet)
RX packets 107040 bytes 154185736 (147.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 10389 bytes 856901 (836.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
enp0s3:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.2.104 netmask 255.255.255.0 broadcast 10.0.2.255
ether 08:00:27:8e:d9:28 txqueuelen 1000 (Ethernet)
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 96 bytes 7764 (7.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 96 bytes 7764 (7.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:4a:d6:8a 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@gbase_rh7_003 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: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:8e:d9:28 brd ff:ff:ff:ff:ff:ff
inet 10.0.2.103/24 brd 10.0.2.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet 10.0.2.105/24 brd 10.0.2.255 scope global secondary enp0s3
valid_lft forever preferred_lft forever
inet 10.0.2.104/24 brd 10.0.2.255 scope global secondary enp0s3:0
valid_lft forever preferred_lft forever
inet6 fe80::ab19:72ea:2472:cc28/64 scope link
valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000
link/ether 52:54:00:4a:d6:8a brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000
link/ether 52:54:00:4a:d6:8a brd ff:ff:ff:ff:ff:ff
确认IP可用
IP可以正常ping通,且可以执行ssh远程命令。
[root@gbase_rh7_003 network-scripts]# ping 10.0.2.105
PING 10.0.2.105 (10.0.2.105) 56(84) bytes of data.
64 bytes from 10.0.2.105: icmp_seq=1 ttl=64 time=0.086 ms
64 bytes from 10.0.2.105: icmp_seq=2 ttl=64 time=0.069 ms
^C
--- 10.0.2.105 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.069/0.077/0.086/0.012 ms
[root@gbase_rh7_003 network-scripts]# ssh 10.0.2.105 "date"
The authenticity of host '10.0.2.105 (10.0.2.105)' can't be established.
ECDSA key fingerprint is 18:cd:92:ce:4c:19:2c:55:7b:35:aa:e8:60:4a:f3:84.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.2.105' (ECDSA) to the list of known hosts.
Wed Dec 9 09:35:00 CST 2020
通过linux图形配置界面
可以看到如上手工配置的2个IP.
如果我们点击了apply保存,会自动统一到第二种的方式,虚拟网卡配置文件将消失。
[root@gbase_rh7_003 ~]# cd /etc/sysconfig/network-scripts/
[root@gbase_rh7_003 network-scripts]# ll ifcfg-enp0s3*
-rw-r--r-- 1 root root 411 Dec 9 09:38 ifcfg-enp0s3
[root@gbase_rh7_003 network-scripts]# cat ifcfg-enp0s3
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp0s3
UUID=054d2f84-2976-45a6-baa2-1420019b55f5
DEVICE=enp0s3
ONBOOT=yes
DNS1=114.114.114.114
IPADDR1=10.0.2.105
PREFIX1=24
IPADDR=10.0.2.103
PREFIX=24
IPADDR2=10.0.2.104
PREFIX2=24
GATEWAY=10.0.2.2
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
[root@gbase_rh7_003 network-scripts]#
如上输出可以看到,原始配置文件里增加了IPADDR2配置,而ifcfg_enp0s3:0配置文件消失了。同时可以看到PREFIX 是可以独立配置的。
如下重启后,可以看到ifconfig看不到新增的IP,而ip addr可以。
[root@gbase_rh7_003 network-scripts]# service network restart
Shutting down interface enp0s3: Device 'enp0s3' successfully disconnected.
[ OK ]
Shutting down loopback interface: [ OK ]
Bringing up loopback interface: [ OK ]
Bringing up interface enp0s3: Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)
[ OK ]
[root@gbase_rh7_003 network-scripts]# ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.2.103 netmask 255.255.255.0 broadcast 10.0.2.255
inet6 fe80::ab19:72ea:2472:cc28 prefixlen 64 scopeid 0x20<link>
ether 08:00:27:8e:d9:28 txqueuelen 1000 (Ethernet)
RX packets 107456 bytes 154223018 (147.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 10707 bytes 900781 (879.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 147 bytes 17166 (16.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 147 bytes 17166 (16.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:4a:d6:8a 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@gbase_rh7_003 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: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:8e:d9:28 brd ff:ff:ff:ff:ff:ff
inet 10.0.2.103/24 brd 10.0.2.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet 10.0.2.104/24 brd 10.0.2.255 scope global secondary enp0s3
valid_lft forever preferred_lft forever
inet 10.0.2.105/24 brd 10.0.2.255 scope global secondary enp0s3
valid_lft forever preferred_lft forever
inet6 fe80::ab19:72ea:2472:cc28/64 scope link
valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN qlen 1000
link/ether 52:54:00:4a:d6:8a brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000
link/ether 52:54:00:4a:d6:8a brd ff:ff:ff:ff:ff:ff
[root@gbase_rh7_003 network-scripts]#
总结
在redhat 6里,可以通过方法1增加IP, 而在redhat 7之后,都统一到了方法2. 同时也暗示着ifconfig 功能落伍了。