如何通过VBoxManage控制台配置VirtualBox虚拟机网络及TCP/IPv4静态参数
Hey there! Let's tackle your two questions about configuring VirtualBox networking via VBoxManage from the console.
VBoxManage lets you fully control your VM's network setup without using the GUI. Here are common configurations for popular network modes:
Bridged Networking (connects the VM directly to your physical local network, same as a physical device):
vboxmanage modifyvm VMNAME --nic1 bridged --bridgeadapter1 eth0Replace
VMNAMEwith your VM's actual name, andeth0with your host's physical network interface (useip link showon Linux/macOS oripconfigon Windows to list available interfaces).NAT Networking (default mode, shares the host's IP address for internet access):
vboxmanage modifyvm VMNAME --nic1 natYou can add port forwarding to access services on the VM from the host:
vboxmanage modifyvm VMNAME --natpf1 "guest_ssh,tcp,,2222,,22"This forwards host port 2222 to the VM's port 22 (for SSH access).
Host-Only Networking (VM can only communicate with the host and other VMs on the same host-only network):
First create a host-only adapter if you don't have one:vboxmanage hostonlyif create vboxmanage hostonlyif ipconfig vboxnet0 --ip 192.168.56.1 --netmask 255.255.255.0Then attach it to your VM:
vboxmanage modifyvm VMNAME --nic1 hostonly --hostonlyadapter1 vboxnet0Internal Networking (VMs can only communicate with each other on a private internal network, no host access):
vboxmanage modifyvm VMNAME --nic1 intnet --intnet1 my_internal_networkReplace
my_internal_networkwith a name for your private network.
Important note: VBoxManage handles the virtual network adapter setup, but it doesn't configure the guest OS's network stack directly. To set static IP, subnet mask, gateway, and DNS, you need to adjust settings inside the guest operating system. Here are two ways to do this:
Option 1: Access the VM's terminal directly
If you can open the VM's console or connect via SSH:
Linux Guests (Debian/Ubuntu example):
Edit the Netplan configuration file (location may vary by distro—some use/etc/network/interfaces):sudo nano /etc/netplan/00-installer-config.yamlPaste this configuration (adjust values to match your network):
network: ethernets: enp0s3: # Replace with your VM's network interface name (check with `ip addr`) addresses: [192.168.1.100/24] gateway4: 192.168.1.1 nameservers: addresses: [8.8.8.8, 1.1.1.1] version: 2Apply the changes:
sudo netplan applyFor RHEL/CentOS/Fedora:
sudo nmcli con mod enp0s3 ipv4.addresses 192.168.1.100/24 sudo nmcli con mod enp0s3 ipv4.gateway 192.168.1.1 sudo nmcli con mod enp0s3 ipv4.dns "8.8.8.8 1.1.1.1" sudo nmcli con mod enp0s3 ipv4.method manual sudo nmcli con up enp0s3Windows Guests:
Open Command Prompt as Administrator and run these commands (replaceEthernet0with your VM's adapter name, check withnetsh interface ip show config):# Set static IP, subnet mask, and default gateway netsh interface ip set address "Ethernet0" static 192.168.1.100 255.255.255.0 192.168.1.1 1 # Set primary and secondary DNS servers netsh interface ip set dns "Ethernet0" static 8.8.8.8 netsh interface ip add dns "Ethernet0" 1.1.1.1 index=2
Option 2: Use VBoxManage GuestControl (requires Guest Additions)
If you can't access the VM's terminal directly, you can run commands remotely using guestcontrol (you'll need Guest Additions installed on the VM and know the guest OS credentials):
Linux Example:
vboxmanage guestcontrol VMNAME run --username YOUR_GUEST_USER --password YOUR_GUEST_PASS -- /bin/bash -c " sudo tee /etc/netplan/00-installer-config.yaml <<EOF network: ethernets: enp0s3: addresses: [192.168.1.100/24] gateway4: 192.168.1.1 nameservers: addresses: [8.8.8.8, 1.1.1.1] version: 2 EOF sudo netplan apply "Windows Example:
vboxmanage guestcontrol VMNAME run --username YOUR_GUEST_USER --password YOUR_GUEST_PASS -- cmd.exe /c " netsh interface ip set address \"Ethernet0\" static 192.168.1.100 255.255.255.0 192.168.1.1 1 && netsh interface ip set dns \"Ethernet0\" static 8.8.8.8 && netsh interface ip add dns \"Ethernet0\" 1.1.1.1 index=2 "
内容的提问来源于stack exchange,提问作者FeR-S

