ip a命令查看网络接口,应能看到类似enp0s3(NAT网卡)、enp0s8(仅主机网卡)的两个接口(接口名称可能因VirtualBox版本略有差异,如eth0、eth1)。/etc/network/interfaces文件(传统方式,适用于静态IP)nano)打开网络配置文件:sudo nano /etc/network/interfaces
# 循环回路接口(无需修改)
auto lo
iface lo inet loopback
# NAT网卡(enp0s3):使用DHCP获取IP(自动访问外网)
auto enp0s3
iface enp0s3 inet dhcp
# 仅主机网卡(enp0s8):静态IP配置(用于与主机通信)
auto enp0s8
iface enp0s8 inet static
    address 192.168.56.101  # 仅主机网段IP(与VirtualBox Host-Only网关同一网段,默认192.168.56.1)
    netmask 255.255.255.0   # 子网掩码
    # gateway 192.168.56.1  # 仅主机模式无需网关(若需访问主机外网络,可保留注释)
Ctrl+O→回车→Ctrl+X退出nano)。sudo systemctl restart networking
sudo apt update
sudo apt install network-manager -y
sudo systemctl enable NetworkManager
sudo systemctl start NetworkManager
nmcli命令行工具添加并配置网卡:
enp0s3):sudo nmcli connection add type ethernet con-name nat-connection ifname enp0s3
sudo nmcli connection modify nat-connection ipv4.method auto  # DHCP自动获取IP
enp0s8):sudo nmcli connection add type ethernet con-name host-only-connection ifname enp0s8
sudo nmcli connection modify host-only-connection ipv4.addresses 192.168.56.101/24  # 静态IP
sudo nmcli connection modify host-only-connection ipv4.gateway 192.168.56.1         # 网关(可选)
sudo nmcli connection modify host-only-connection ipv4.dns "8.8.8.8"                # DNS
sudo nmcli connection modify host-only-connection ipv4.method manual               # 手动配置
sudo nmcli connection up nat-connection
sudo nmcli connection up host-only-connection
ping 8.8.8.8  # 若能ping通,说明外网正常
ping 192.168.56.101  # 若能ping通,说明主机与虚拟机通信正常
ping 192.168.56.1  # 若能ping通,说明仅主机网卡与主机网关连接正常
ip a,确认接口是否存在;若仍不存在,检查VirtualBox网络设置中的网卡是否启用。192.168.56.101)未被其他设备占用,可通过VirtualBox Host-Only网络的DHCP范围调整(如将DHCP起始IP设为192.168.56.100,避免与静态IP冲突)。iface enp0s3 inet dhcp),或尝试重启网络服务:sudo systemctl restart networking