How to create VirtualBox virtual machines from Linux terminal

Contents

VirtualBox logo

Most cloud providers rent virtual servers instead of real hardware. If you intend to do the same on your own servers, whether to run different operating systems or manage multiple environments, you can do it totally from your terminal.

If you are planning only using virtual machines, you might be interested in a hypervisor. A hypervisor replaces your host operating system and is custom designed to manage multiple virtual machines. You will not be able to run applications on the host operating system, but you can easily virtualize Ubuntu Server or something and run the applications on top of the hypervisor.

Installing VirtualBox and the VBox extension pack

VirtualBox is a no-cost GUI-based virtualization software that runs as an application on your host operating system (a hypervisor of type 2). You may have used it before on a personal computer to set up a virtual machine, but VirtualBox actually has a full command line interface and can be fully used only from a terminal, which makes it perfect for this use case. Another solution would be QEMU, but VirtualBox is tested and is unbeatable for being free and open source.

Despite this, VirtualBox CLI is a bit difficult to handle, so if you're only playing on a home server and have a little RAM to spare, you can install a lightweight desktop environment on your server and configure VirtualBox in the normal way. You can still run virtual machines in headless mode to keep them running after logging out of DE.

If you plan to stick to the CLI, installation is quite simple, since it is enabled directly from apt:

sudo apt-get install virtualbox

Despite this, you will want to make a note of the version that is installed ( VBoxManage -v ), since you will need to install a few more things. Because we are setting up in headless mode, we will need a way to connect to the machine before SSH can be configured / RDP.

Go to VirtualBox download pageand find the extension pack link for the version it is running on. Download it with wget:

wget https://download.virtualbox.org/virtualbox/5.2.42/Oracle_VM_VirtualBox_Extension_Pack-5.2.42.vbox-extpack

And install it:

sudo VBoxManage extpack install Oracle_VM_VirtualBox_Extension_Pack-5.2.42.extpack

For some reason, VRDE didn't seem to work for us with the built-in VNC extension pack installed. Removing this package solved the problem:

sudo VBoxManage extpack uninstall VNC

Set up a virtual machine

To start, we will create a new virtual machine called “Ubuntu”. You can see a list of supported OS types VBoxManage list ostypes.

sudo VBoxManage createvm --name "Ubuntu" --ostype Ubuntu_64 --register

Give the machine some RAM and create a new hard drive. We will go with 4 GB of RAM and 32 GB of hard disk space, but both can be expanded later. Please note that the hard disk is a virtual disk and is not pre-allocated.

sudo VBoxManage modifyvm "Ubuntu" --memory 4096
sudo VBoxManage createhd --filename "Ubuntu.vdi" --size 32000

Set the number of virtual CPUs. This is measured in threads, not on full CPU cores.

sudo VBoxManage modifyvm "Ubuntu" --Cpus 2

the following is one more configuration for the hard disk. In general, this happens automatically when you create machines from the gui, but you will have to manually create a sas driver and connect it to the vm for the hard disk to work properly.

sudo VBoxManage storagectl "Ubuntu" --name "SAS Controller" --add sas --controller LSILogicSAS
sudo VBoxManage storageattach "Ubuntu" --storagectl "SAS Controller" --port 0 --device 0 --type hdd --medium "Ubuntu.vdi"
sudo VBoxManage storagectl "Ubuntu" --name "SAS Controller" --hostiocache on

We will do the same with the disk that will contain the ISO to install the operating system. This assumes that your ISO is at /home/user/Ubuntu.iso, therefore change it accordingly.

sudo VBoxManage storagectl "Ubuntu" --name "IDE Controller" --add ide --controller PIIX4
sudo VBoxManage storageattach "Ubuntu" --storagectl "IDE Controller" --port 0 --device 0 --type dvddrive --medium "/home/user/Ubuntu.iso"

After, turn on some performance-related switches to avoid slowdowns from excessive RAM usage:

sudo VBoxManage modifyvm "Ubuntu" --nestedpaging on
sudo VBoxManage modifyvm "Ubuntu" --largepages on

Next, we will create the network that this virtual machine will use. There are many options for this step:

  • NAT, the simplest, which simply enables the VM to communicate with the outside world via network address translation (NAT). This does not allow virtual machines to communicate with each other and does not allow host-to-guest contact unless port is forwarded.
  • NAT network is equivalent to NAT, except that the network is shared between the virtual machines and they can communicate with each other. The NAT network acts as a virtual subnet.
  • Bridged, that binds the physical connection directly to the VM. The virtual machine has a real IP address on your network. This can work even with a single Wi-Fi connection / Ethernet, but if you have a network card with many Ethernet ports, can allocate a full interface directly to a virtual machine.
  • Red virtual, connecting multiple instances in an isolated virtualized network. This is useful for modeling real networks, but not suitable for most use cases.
  • Host-only adapter, is equivalent to the virtual network, but makes it possible to talk to the host (not with the Internet).

For this example, we will use the NAT network, since it is simple to configure and the most versatile of the group, offering a balance of connectivity and isolation.

Create a new network in the 10.0.0.0/24 subnet, Start:

sudo VBoxManage natnetwork add --netname natnet1 --network "10.0.0.0/24" --enable
sudo VBoxManage natnetwork start --netname natnet1

And attach it to the VM:

sudo VBoxManage modifyvm "Ubuntu" --nic1 natnetwork --nat-network1 natnet1

Next, we will have to configure how we access this instance. Because we are installing this on a headless server (no video output), we will not be able to complete the installation procedure and install an RDP client on the guest operating system. To solve this, We will use the VirtualBox VRDE server, the main reason we installed the VirtualBox extension pack earlier. This captures the virtual screen output and makes it available via RDP.

Turn this on and set a port for this. you will want to make sure that the host operating system has this port open.

sudo VBoxManage modifyvm "Ubuntu" --vrde on
sudo VBoxManage modifyvm "Ubuntu" --vrdeport 5587

Finally, we can start the virtual machine in headless mode and start the installation of the operating system.

sudo VBoxManage startvm "Ubuntu" --type headless

note that headless mode means that you are running on a headless server, and not that the guest operating system does not have video output.

It will take a second or two to start, and you will be able to start an RDP client and connect to it using the port you specified. You can request a username and password; VRDE by default is “admin” no password.

Port forwarding after configuration

Once the operating system installation is complete, you will have to do port forwarding to keep access after VRDE shuts down, essentially like opening firewall rules. If you are setting up a headless environment like Linux, you will simply have to forward SSH. The following command will add a port forwarding rule to your NAT network to forward the port 2200 on the host OS to the default SSH port on the guest. Note that this port will need to be open on your host's firewall for this to work.

sudo VBoxManage natnetwork modify --netname natnet1 --port-forward-4 "ssh:tcp:[]:2200:[10.0.0.2]:22"

If you are configuring RDP, you will need to configure the RDP server in the guest OS and make sure the RDP port is open on the guest firewall. Thereafter, you can forward the port in the same way:

sudo VBoxManage natnetwork modify --netname natnet1 --port-forward-4 "rdp:tcp:[]:5589:[10.11.0.2]:3389"

If you need to clear port forwarding rules, you will have to delete them by name with this rather unwieldy command (the first string before the colon):

sudo VBoxManage natnetwork modify --netname natnet1 --port-forward-4 delete rdp

Once you have verified that you can connect via SSH / RDP directly to the guest OS, you can disable VRDE:

sudo VBoxManage modifyvm "Ubuntu" --vrde off

If you plan to use this virtual machine over RDP for long periods of time, you might want to install VirtualBox Guest Additions suite for your guest OS.


If you decide to manage VirtualBox using GUI after configuring this VM from CLI, note that this VM was created as root, so you will need to start VirtualBox GUI like root to see this virtual machine in the list. Opposite case, should work regularly.

At the same time, if you want the machine to run while your desktop environment is off, you will have to right click to start the machine in headless mode.

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.