VirtualBox中Ubuntu虚拟机无法联网,需同时实现宿主机与虚拟机互通
Hey there, let's get your Ubuntu Server VM connected to the internet while keeping that host-VM connectivity you already have. Since you're using bridge mode and the host-VM link works, we just need to adjust a few network settings on the VM side. Here's how to fix it step by step:
You mentioned enp0s3 is the working interface (with eth0 being unused), so first let's make sure it's enabled:
- Run
ifconfig -aorip link showto confirmenp0s3is listed. - If it's not marked as "UP", enable it with:
sudo ip link set enp0s3 up
Since you're using bridge mode, your VM should sit on the same network segment as your Windows 7 host's WiFi connection. Let's start with the simplest option first:
Option A: Use DHCP (Auto-IP Assignment)
Most home routers support DHCP, which will automatically assign your VM a valid IP, gateway, and DNS:
- Run this command to request an IP from your router:
sudo dhclient enp0s3 - Check if it worked by running
ip addr show enp0s3— you should see an IP in the same range as your host's WiFi (e.g., if your host is192.168.1.5, the VM might get192.168.1.10).
Option B: Manual Static IP (If DHCP Fails)
If DHCP doesn't work, set a static IP that matches your host's network:
Get your host's network details: On Windows 7, open Command Prompt and run
ipconfig. Look for your WiFi adapter's:- IPv4 Address (e.g.,
192.168.1.5) - Subnet Mask (e.g.,
255.255.255.0) - Default Gateway (e.g.,
192.168.1.1) - DNS Servers (you can also use public ones like
8.8.8.8or1.1.1.1)
- IPv4 Address (e.g.,
Configure the VM's network:
For Ubuntu 18.04+ (uses Netplan):
Edit the Netplan config file (usually in/etc/netplan/— look for a.yamlfile like00-installer-config.yaml):network: ethernets: enp0s3: addresses: [192.168.1.100/24] # Pick an unused IP in your host's network range gateway4: 192.168.1.1 # Your host's default gateway nameservers: addresses: [8.8.8.8, 1.1.1.1] # Public DNS or your router's DNS version: 2Apply the changes with:
sudo netplan applyFor older Ubuntu versions (uses
/etc/network/interfaces):
Edit the file with:sudo nano /etc/network/interfacesAdd these lines (replace values with your host's details):
auto enp0s3 iface enp0s3 inet static address 192.168.1.100 netmask 255.255.255.0 gateway 192.168.1.1 dns-nameservers 8.8.8.8 1.1.1.1Restart the network service:
sudo /etc/init.d/networking restart
Let's confirm everything works:
- First, verify host-VM connectivity is still intact:
ping [your-windows-host-ip](replace with your host's actual IP fromipconfig). - Next, ping your router's gateway:
ping 192.168.1.1(use your gateway IP). If this works, your VM can reach the router. - Then ping a public DNS server:
ping 8.8.8.8. Success here means your VM can reach the internet. - Finally, test DNS resolution:
ping google.com. If this works, your VM can resolve domain names properly.
- Ensure bridge mode targets the right adapter: In your virtualization software (VirtualBox/VMware), double-check that bridge mode is set to your WiFi adapter (not a wired Ethernet adapter, since you're using WiFi).
- Ubuntu Firewall: Even if Windows Firewall is off, Ubuntu's default firewall (
ufw) might block traffic. Temporarily disable it to test:
If this fixes it, you can re-enable it and add rules to allow internet traffic later.sudo ufw disable - Host WiFi is working: Confirm your Windows 7 laptop can actually connect to the internet — if the host has no internet, the VM won't either via bridge mode.
内容的提问来源于stack exchange,提问作者Albert Lazaro de Lara

