Actions: | Security

Navigation: Home | Services | Tools | Articles | Other

Getting Started with VirtualBox from the CLI

There's plenty of good basic tutorial information available to get started with VirtualBox using its fine graphical interface, but there are circumstances when such interfaces are not useful such as when the host machine is headless. Indeed, my world is usually remote and command line driven, so I'm going to document creating a new virtual machine only using the CLI.

Installing VirtualBox was a snap, follow their documentation. Be sure that the Personal and Evaluation Licence is sufficient for your purposes or use the Open Source Edition. The differences are clearly spelled out on the download page. Almost all of the configuration work uses the various subcommands of VBoxManage.

Make sure you leave enough memory for your host OS otherwise you'll end in a disaster of thrashing swap.

Decide which type of vm you want:

VBoxManage list ostypes

Make sure you pick a 64bit type if your VM should be 64bit, because VirtualBox will set some configuration items based upon this.

Create a new VM and register it with VirtualBox:

VBoxManage createvm --name 'TestVM1' --ostype 'WindowsXP' --basefolder '/home/dhutty/VM/winXP' --register

See it:

VBoxManage showvminfo "TestVM1"

Create a new virtual hard disk:

VBoxManage createhd --size 16384 --filename /home/dhutty/VM/disks/TestVM1_hda --remember

(Note that all these modifyvm commands can be combined into one)

Modify the new VM to use the new disk:

VBoxManage modifyvm --name 'TestVM1' --hda '/home/dhutty/VM/disks/TestVM1_hda'

Modify memory and some settings:

VBoxManage modifyvm --name 'TestVM1' --memory 768 --vram 32 --accelerate3d on

Point the virtual optical disk to the image you want to use for the OS installation:

VBoxManage modifyvm --name 'TestVM1' --dvd /path/to/install.iso

Tell the new VM to boot from this new virtual optical drive:

VBoxManage modifyvm TestVM1 --boot1 dvd

Set up an audio device and other hardware settings:

VBoxManage modifyvm TestVM1 --audio alsa --audiocontroller ac97 --acpi on

Start the VM, enabling the VRDP server so you can connect and monitor the installation:

VBoxHeadless --startvm TestVM1

Connect to the new VM:

rdesktop -a 16 -N

To setup port forwarding to you new VM


VBoxManage setextradata "<vm name>" "VBoxInternal/Devices/e1000/0/LUN#0/Config/guestssh/HostPort" 3022
VBoxManage setextradata "<vm name>" "VBoxInternal/Devices/e1000/0/LUN#0/Config/guestssh/GuestPort" 22
VBoxManage setextradata "<vm name>" "VBoxInternal/Devices/e1000/0/LUN#0/Config/guestssh/Protocol" TCP

You might need to change "e1000" depending on which type of network adapter your VM is using, perhaps "pcnet". You might need to restart VirtualBox (or perhaps just the host's networking?) to get this to stick.

But this seems to have been superseded (perhaps in VirtualBox4+?) by:

VBoxManage modifyvm "<vm name>" --natpf1 "guestssh,tcp,,3022,,22"

Then if sshd in the VM is listening on the normal port 22, then, on the host, you can:

ssh -p 3022 localhost

and you will be offered a login prompt. You may need to jumpstart networking in the guest by attempting to reach out before trying to reach in immediately after resuming a VBox session.

Similarly, for http:

VBoxManage setextradata "<vm name>" "VBoxInternal/Devices/e1000/0/LUN#0/Config/guesthttp/HostPort" 3080
VBoxManage setextradata "<vm name>" "VBoxInternal/Devices/e1000/0/LUN#0/Config/guesthttp/GuestPort" 80
VBoxManage setextradata "<vm name>" "VBoxInternal/Devices/e1000/0/LUN#0/Config/guesthttp/Protocol" TCP

Then if a webserver in the VM is listening on the normal port 80, then, on the host, you can:

links http://localhost:3080

and you will be offered the webserver's index page.

FreeBSD guests

  • Network Adapter: PCNet-PCI II

  • Storage:

    Under Settings -> Storage, select the box for 'Enable Additional Controller' and choose 'SATA(AHCI)'. Then, in the 'Attachments' pane, choose 'SATA port 0' for the slot column. Otherwise, you end up with intermittent kernel panics with 'unrecoverable I/O error'.