VirtualBox command line tools
Nowadays, with tools like Vagrant and Docker, it is a given you can control your virtual machines from the command line, but I did not realise until recently that it is also possible to do the same with VirtualBox.
VirtualBox has a super useful utility called VBoxManage for this.
List all virtual machines
To get a list of all available virtual machines on your system:
VBoxManage list vms
This will display a compact list with each VM’s name and UUID.
List the running virtual machines
If you need a list of all running virtual machines, simply use:
VBoxManage list runningvms
Start a virtual machine
Start a virtual machine without the graphical user interface (i.e. headless):
VBoxManage startvm <name-of-the-vm> --type headless
Enter the virtual machine
Now you can access the virtual machine with SSH. You can connect just like with any (remote) SSH-server. In stead of the default port 22, you will need to specify the specific port that you added for the VM. For example:
ssh -p 3022 <user>@127.0.0.1
If you forgot which portnumber you used, there’s a command that will show you the network rules:
VBoxManage showvminfo <name-of-the-vm> | grep Rule NIC 1 Rule(0): name = ssh, protocol = tcp, host ip = , host port = 3022, guest ip = , guest port = 22
When nothing is returned, you will need to add port forwarding. This can be done from the GUI (Network Settings > Port Forwarding), but you guessed it, there’s also a command line version to do this:
VBoxManage modifyvm <name-of-the-vm> --natpf1 "ssh,tcp,,3022,,22"
This wil map port 3022 on the host to port 22 on the guest.