Misc
Suspending a VM
- Don’t suspend the VM from within the OS. Instead, close the VM window and select Save the machine state
http://askubuntu.com/a/63545/18665
Managing VMs from the command line
List the VMs
vboxmanage list vms
Get the power state of a VM
vboxmanage showvminfo "VM name" | grep ^State
Start a VM
vboxmanage startvm "VM name" --type headless
Safely shut down a VM
vboxmanage controlvm "VM name" acpipowerbutton
Show port forwarding rules
vboxmanage showvminfo "VM name" | grep Rule
Delete a port forwarding rule
$ vboxmanage showvminfo "VM name" | grep Rule
NIC 1 Rule(0): name = Rule 1, protocol = tcp, host ip = , host port = 15672, guest ip = , guest port = 15672
$ vboxmanage controlvm "VM name" natpf1 delete "Rule 1"
$ vboxmanage showvminfo "VM name" | grep Rule
Create a port forwarding rule
$ vboxmanage controlvm "VM name" natpf1 "Rule 1,tcp,,15671,,15671"
$ vboxmanage showvminfo "VM name" | grep Rule
NIC 1 Rule(0): name = Rule 1, protocol = tcp, host ip = , host port = 15671, guest ip = , guest port = 15671
Storage
Grow virtual disk
Note: The virtual disk type must be VDI. If not, see below to convert it.
-
Power off the VM
- Resize the disk
vboxmanage modifyhd /path/to/image.vdi --resize 61440
(size is in MB)
- Power on the VM and resize the filesystem within the OS
Convert a virtual disk from VMDK to VDI
- Clone the virtual disk to a new VDI image
vboxmanage clonehd --format VDI /path/to/image.vmdk /path/to/image.vdi
- Remove the old disk image
- VirtualBox > select the VM > Settings > Storage > right-click the old disk image > Remove Attachment
- Add the new disk image
- Right-click Controller: SATA > Add Hard Disk > Choose Existing Disk > browse to the new disk image you created > Open > OK
Convert a raw disk image (created by dd) to VDI
vboxmanage convertfromraw /path/to/disk.img /path/to/disk/vdi
Networking
To allow VMs to talk to each other as well as the outside network
This method requires a DHCP server for your outside network with available addresses:
-
Select the VM > Settings > Network > Adapter 1 > Attached to > Bridged Adapter
-
Name > select the appropriate adapter to be bridged (e.g. eth0 for a wired connection, wlan0 for wireless) > OK
If that isn’t an option:
-
Power off the VM (required to add or remove network adapters)
- Set the first network adapter for the VM to NAT (the default)
- Select the VM > Settings > Network > Adapter 1 > Attached to > NAT
- Set the second network adapter for the VM to Host-only Adapter
- Adapter 2 > check Enable Network Adapter > Attached to > Host-only Adapter > OK
- If OK is greyed out and you see Invalid settings detected:
-
Cancel > File > Host Network Manager > Create > Close
-
Repeat the above instructions to add a new network adapter to the newly created VM
-
- If OK is greyed out and you see Invalid settings detected:
- Adapter 2 > check Enable Network Adapter > Attached to > Host-only Adapter > OK
-
Power on the VM
- Configure the second NIC
- Ubuntu/Debian:
- Configuration should be automatic
- RHEL/CentOS:
- Copy the configuration from the first NIC
sudo cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth1
- Get the MAC address for the new adapter (the one with eth1 in the name), or you can get it from the Virtualbox settings:
cat /etc/udev/rules.d/70-persistent-net.rules
- Edit /etc/sysconfig/network-scripts/ifcfg-eth1
sudo vi /etc/sysconfig/network-scripts/ifcfg-eth1
- Update DEVICE:
DEVICE=eth1
- Update HWADDR. Ex:
HWADDR=08:00:27:89:14:B8
- Delete the UUID line
- Update DEVICE:
- Restart the network
sudo service network restart
- Copy the configuration from the first NIC
- Ubuntu/Debian:
Guest additions
Install VirtualBox guest additions on Ubuntu
- After installing Ubuntu, install prerequisite packages for the VirtualBox Guest Additions
sudo apt-get -y install build-essential dkms linux-headers-generic
- Install the VirtualBox Guest Additions:
http://askubuntu.com/a/22745/18665-
Devices > Insert Guest Additions CD image
- Open a terminal and run:
/media/$USER/VBOXADDITIONS_X.X.X_XXXXXX/autorun.sh
-
Reboot
- If you get error messages related to guest additions or they don’t appear to be working, try reinstalling them and reboot again
-
Troubleshooting
If you are unable to attach USB devices
Your user must be part of the vboxusers group
- Use the id command to list your group membership
id
- If vboxusers isn’t in the list of groups, run this command to add your user
sudo usermod -a -G vboxusers $USER
- Log out and log back in to update your membership. If you’d rather not log out:
-
Completely close VirtualBox
- Run this command in a terminal to update your groups temporarily:
su - $USER
- Run the virtualbox command to open VirtualBox with the new groups
virtualbox
-
Errors when trying to start a VM from the command line
VBoxManage: error: This VM was configured to use 3D acceleration.
- Edit the .vbox file for the VM and set
accelerate3D="false"
- Edit the .vbox file for the VM and set
VBoxManage: error: Failed to load unit 'HGCM' (VERR_SSM_UNEXPECTED_DATA)
- Delete the saved state:
vboxmanage discardstate "VM name"
- Then try to start the VM again
- Delete the saved state:
VM is unable to access the internet
-
Double-check the network settings (see above)
-
If you’re running Docker, you may need to stop it or change the default network range used by Docker, as this can conflict with the private network ranges used by VirtualBox