改正:虚拟机可以通过配置一个桥接网卡,访问外网, 不需要配置两个网卡
最小化安装后,nat网卡没有启用,需要修改ifcfg-enp0s3
[root@localhost network-scripts]# cat /etc/sysconfig/network-scripts/ifcfg-enp0s3
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp0s3
UUID=97735b81-9f4a-40bc-8ec3-94e6dcc6b3a3
DEVICE=enp0s3
ONBOOT=yes
重启
systemctl restart network.service
检查 nat网卡是否启用
ip addr show
nat网卡启用后,就可以通过主机访问外网了,
关闭虚拟机,
在添加一个桥接网卡
启动虚拟机, 进入命令行, 检查ip
ip addr show
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:aa:52:27 brd ff:ff:ff:ff:ff:ff
inet 10.0.2.15/24 brd 10.0.2.255 scope global dynamic enp0s3
valid_lft 85945sec preferred_lft 85945sec
inet6 fe80::7ce6:ca1e:93ad:db2e/64 scope link
valid_lft forever preferred_lft forever
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:25:5d:85 brd ff:ff:ff:ff:ff:ff
inet 172.18.7.128/22 brd 172.18.7.255 scope global enp0s8
valid_lft forever preferred_lft forever
inet6 fe80::c05e:1093:a124:5dd3/64 scope link
valid_lft forever preferred_lft forever
enp0s8是新创建的桥接网络
/24 表示这个IP掩码是255.255.255.0
表示这个地址的前24(也就是IP前三位十进制)位为网络位。(局域网中的ip地址,前3位是一样的172.18.7, 只有最后一位是不一样的???)
表示这个网段内有8位主机位,也就是2的8次方个主机(局域网可以提供的ip地址172.18.7.0--172.18.7.255???)
表示这个地址为C类标准IP
记下红色的网卡mac地址和名称,这个ip地址是系统自动分配的,可以按需修改
复制enp0s3
cp ifcfg-enp0s3 ifcfg-enp0s8
修改fcfg-enp0s8后的如下
[root@localhost network-scripts]# cat /etc/sysconfig/network-scripts/ifcfg-enp0s8
HWADDR=08:00:27:25:5d:85 #mac地址
TYPE=Ethernet
BOOTPROTO=static #静态ip
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp0s8 #名称
UUID=97735b81-9f4a-40bc-8ec3-94e6dcc6b3b6 #uuid随便修改,不能和enp0s3重复, 这里只修改了最后两位a3->b6
DEVICE=enp0s8
ONBOOT=yes #开机启动
IPADDR=172.18.7.128 #可以按需要修改,不能跟局域网其他ip冲突
NETMASK=255.255.252.0 #子网掩码如果是24,那这里应该是NETMASK=255.255.255.0
GATEWAY=172.18.7.1
DNS1=172.18.7.1
DNS2=4.4.4.4
重启网络
systemctl restart network.service
检查 ip addr show
检查和主机连通性, 检查虚机和虚机连通性, 检查和外网连通性
ping -c 4 fedora
完成
======================================
测试桥接方式访问外网
centos7 最小化安装后, nat网卡默认是没有启用的,如果需要启用,需要修改ifcfg-enp0s3
ONBOOT=yes
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-enp0s3
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=enp0s3
UUID=25b75a1b-07cf-4d30-a937-6c17bae9e1a9
DEVICE=enp0s3
ONBOOT=no
关闭虚拟机, 禁用nat网卡, 添加桥接网卡, 重新登陆
[root@localhost ~]# ip addr show
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: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:61:5e:3e brd ff:ff:ff:ff:ff:ff
inet 172.18.6.198/22 brd 172.18.7.255 scope global dynamic enp0s8
valid_lft 3096sec preferred_lft 3096sec
inet6 fe80::b259:4893:4bc4:93c1/64 scope link
valid_lft forever preferred_lft forever
[root@localhost ~]# ping -c4 www.baidu.com
PING www.a.shifen.com (61.135.169.125) 56(84) bytes of data.
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=1 ttl=56 time=4.74 ms
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=2 ttl=56 time=3.67 ms
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=3 ttl=56 time=5.50 ms
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=4 ttl=56 time=3.44 ms
--- www.a.shifen.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3011ms
rtt min/avg/max/mdev = 3.447/4.343/5.502/0.832 ms
可以通过桥接方式访问外网
[root@localhost ~]# nmcli connection show
NAME UUID TYPE DEVICE
Wired connection 1 2a9eda70-eb5b-3e7d-8948-2cbdebb69c65 802-3-ethernet enp0s8
enp0s3 25b75a1b-07cf-4d30-a937-6c17bae9e1a9 802-3-ethernet --
[root@localhost ~]# ll /etc/sysconfig/network-scripts/ifcfg-*
-rw-r--r--. 1 root root 311 May 3 21:55 /etc/sysconfig/network-scripts/ifcfg-enp0s3
-rw-r--r--. 1 root root 254 Sep 12 2016 /etc/sysconfig/network-scripts/ifcfg-lo
没有找到新添加的桥接网卡的配置文件,????