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