How to create virtualbox by commandline

First create the virtual machine called myVirtualHost and register it:

VBoxManage createvm --name myVirtualHost --register

Then create a hard disk of 10Gbytes:

VBoxManage createhd --filename myhdd1.vdi -size 10240

This will create the file ~/.VirtualBox/HardDisks/myhdd1.vdi

Add an ide controller and the hard disk:

VBoxManage storagectl myVirtualHost --name "IDE Controller" --add ide
VBoxManage modifyvm myVirtualHost --hda .VirtualBox/HardDisks/myhdd1.vdi

You can add more disks with these commands:

VBoxManage modifyvm myVirtualHost --hdb .VirtualBox/HardDisks/myhdd2.vdi
VBoxManage storageattach myVirtualHost --storagectl "IDE Controller" --device 1 --port 0 --type hdd --medium .VirtualBox/HardDisks/myhdd3.vdi 

Now I will give this machine 512Mbytes of RAM:

VBoxManage modifyvm myVirtualHost --memory 512 --acpi on

You have different network options. If you want to give your virtual machine its own IP addpress you have to use the bridged mode:

VBoxManage modifyvm myVirtualHost --nic1 bridged

If you want to use the same IP address than your host machine you can use NAT. In this case you can configure NAT port forwarding to access to some services, for example with these lines you can access the SSH service of our virtual machine by connecting to the port 2222 from your host.

VBoxManage modifyvm myVirtualHost --nic1 nat
VBoxManage modifyvm myVirtualHost --natpf1 "guestssh,tcp,,2222,,22"

To launch the virtual machine and leave it in background you can use one of the following lines:

nohup VBoxHeadless -s myVirtualHost --vnc --vncpass mys3cr3t &
nohup VBoxHeadless -s myVirtualHost --vrde off &

The first one will enable VNC and also terminal server.
The second one will disable both, VNC and terminal server.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s