以下是CentOS虚拟机在VirtualBox中的网络设置方法,支持多种模式,可根据需求选择:
ping www.baidu.com,但宿主机无法直接ping虚拟机IP。vi /etc/sysconfig/network-scripts/ifcfg-enp0s3(网卡名以ip a命令为准)。BOOTPROTO=static、IPADDR=与宿主机同网段IP(如宿主机IP为192.168.1.100,则虚拟机设为192.168.1.101)、NETMASK=255.255.255.0、GATEWAY=宿主机网关、ONBOOT=yes。systemctl restart network。ping IP,且虚拟机可访问外网。vi /etc/sysconfig/network-scripts/ifcfg-enp0s3。BOOTPROTO=static、IPADDR=与Host-Only网卡同网段IP(如192.168.56.10)、NETMASK=255.255.255.0、GATEWAY=Host-Only网卡IP、ONBOOT=yes。systemctl restart network。ping 192.168.56.10,虚拟机可ping 宿主机Host-Only网卡IP,但无法访问外网。systemctl restart network。ping 虚拟机Host-Only IP(如ping 192.168.56.10)。ping 宿主机IP(通过ip a查看宿主机物理网卡IP)和ping www.baidu.com。systemctl stop firewalld(Linux)或关闭Windows Defender防火墙。systemctl stop firewalld(CentOS 7)或systemctl stop firewalld(CentOS 8+)。enp0s3、eth0),可通过ip a命令确认。ping 虚拟机IP(根据不同模式选择IP,如仅主机模式下用Host-Only IP)。ping 宿主机IP(宿主机物理网卡IP或Host-Only网卡IP)。ping 8.8.8.8或ping www.baidu.com。根据实际需求选择模式,桥接模式适合需要与局域网设备互通的场景,仅主机模式适合隔离开发环境,双网卡组合可同时满足内外网需求。