VirtualBox 版本:7.0
虚拟机版本:CentOS-7-x86_64-Everything-1804_2.iso
宿主机版本:Windows 10-21H2
VirtualBox 和 虚拟机安装
省略~
VirtualBox 配置
在虚拟机的【设置】选项中配置两张网卡,图下图所示,网卡一用来上外网,网卡二用来宿主机和虚拟机互通;
宿主机配置
在宿主机的网络配置中可以看到有两张网卡,配置如下
虚拟机配置
在 /etc/sysconfig/network-scripts 下我们可以看到有 ifcfg-enp* 的几个网卡
[root@localhost ~]# ls /etc/sysconfig/network-scripts
ifcfg-enp0s3 ifdown-bnep ifdown-isdn ifdown-sit ifup ifup-ippp ifup-plusb ifup-sit ifup-wireless
ifcfg-enp0s8 ifdown-eth ifdown-post ifdown-Team ifup-aliases ifup-ipv6 ifup-post ifup-Team init.ipv6-global
ifcfg-lo ifdown-ippp ifdown-ppp ifdown-TeamPort ifup-bnep ifup-isdn ifup-ppp ifup-TeamPort network-functions
ifdown ifdown-ipv6 ifdown-routes ifdown-tunnel ifup-eth ifup-plip ifup-routes ifup-tunnel network-functions-ipv6
ifcfg-enp0s3 内容如下
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-enp0s3
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="dhcp"
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="b68e88aa-c2b0-491d-b68a-ba42d5909da0"
DEVICE="enp0s3"
ONBOOT="yes"
ifcfg-enp0s8 内容如下,相对于 enp0s3 有以下差异
- 其实 VirtualBox 的网卡一相当于 enp0s3,网卡二相当于 enp0s8;
- BOOTPROTO:因为要固定 IP,所以这里改为了 static,如果不需要固定 IP 这里可以不变;
- IPADDR:因为要固定 IP,这里可以自行定义一个和宿主机上图中 IP 地址同网段的 IP 地址;
- GATEWAY:这里采用宿主机中网卡的 IP 地址作为网关地址,借用宿主机的这个新的网卡来让宿主机和虚拟机互通;
- DNS、NETMASK 需要如下所示即可;
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-enp0s8
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEM_MODE=stable-privacy
NAME=enp0s8
UUID=89a26f7a-cff6-41c9-967d-f24d8a487656
DEVICE=enp0s8
ONBOOT=yes
IPV6_PRIVACY=no
IPADDR=192.168.56.66
PREFIX=24
GATEWAY=192.168.56.1
NDS1=192.168.56.1
NETMASK=255.255.255.0
当我们执行 ifconfig 时,会看到以下内容,包含了 enp0s3 和 enp0s8 的相关详细,和上述的两个文件一一对应,如果 enp0s8 不存在,可以尝试 ifconfig enp0s8 up 来启用网卡
[root@localhost ~]# ifconfig
br-333369dc300e: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.18.0.1 netmask 255.255.0.0 broadcast 172.18.255.255
inet6 fe80::42:b2ff:fefd:dd65 prefixlen 64 scopeid 0x20<link>
ether 02:42:b2:fd:dd:65 txqueuelen 0 (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
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.2.15 netmask 255.255.255.0 broadcast 10.0.2.255
inet6 fe80::aa2:ef4e:8e39:f70e prefixlen 64 scopeid 0x20<link>
ether 08:00:27:77:8d:18 txqueuelen 1000 (Ethernet)
RX packets 1445586277 bytes 99534207139 (92.6 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 724852849 bytes 80007504058 (74.5 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
enp0s8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.56.66 netmask 255.255.255.0 broadcast 192.168.56.255
inet6 fe80::a00:27ff:fe20:15a5 prefixlen 64 scopeid 0x20<link>
ether 08:00:27:20:15:a5 txqueuelen 1000 (Ethernet)
RX packets 8234963 bytes 11649264390 (10.8 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 911044 bytes 201611576 (192.2 MiB)
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 1000 (Local Loopback)
RX packets 1671278 bytes 133346849 (127.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1671278 bytes 133346849 (127.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0