When we are testing something on the server, it’s convenient and safe to do all the things in the virtual machine to avoid crash. And this blog records the tips to use the headless virtual box.

1. Create a virtual machine

  • Create the headless server with specific name and ostype
1
VBoxManage createvm --name "xxx" --ostype Ubuntu_64 --register         
  • Allocate the memory for the VM
1
VBoxManage modifyvm "xxx" --memory 2048 --acpi on --boot1 dvd --nic1 nat
  • Create a virtual hard disk for the VM, make sure to allocate enough space. Even though, we can change the size later, there are more complex steps to change the partition.
1
VBoxManage createhd --filename "xxx.vdi" --size 40000
  • Add an IDE Controller to the new VM
1
VBoxManage storagectl "xxx" --name "IDE Controller" --add ide --controller PIIX4
  • Set the VDI file created before as the first virtual hard disk for the new VM
1
VBoxManage storageattach "xxx" --storagectl "IDE Controller" --port 0 --device 0 --type hdd --medium "xxx.vdi"
  • Attach the ISO file (You can download different version of OS in Download Linux | Linux.org) that you want to install in the VM.
1
VBoxManage storageattach "xxx" --storagectl "IDE Controller" --port 0 --device 1 --type dvddrive --medium ubuntu-22.04-live-server-amd64.iso                        
  • Change the number of CPU core
1
VBoxManage modifyvm "xxx" --cpus 4
1
VBoxManage modifyvm "xxx" --vrde on

In some special situation, the server only expose a few port, you can not using the VRDP to connect with the headless VirtualBox in port 3389 (default port for rdp protocol) directly. You can deal with this problem with port forwarding. Run the following command in your own PC to forward the request to the server in port 3389. By doing so, you can visit any port in the server as long as you can connect into the server in port 22 and pass the authentication.

1
ssh -v -L 3389:localhost:3389 userID@serverIP -T  

2. Configure the SSH for VM

  • Install opens-server and start the service
1
2
3
4
5
6
sudo apt update
sudo apt upgrade
sudo apt install openssh-server

sudo service ssh start
sudo systemctl status ssh
  • Configure firewall and open the port, the default port is 22
1
2
3
sudo ufw allow ssh
sudo ufw enable
sudo ufw status
  • Configure and check the port forwarding from server port 8080 to VM port 22
1
2
VBoxManage modifyvm "xxx" --natpf1 "SSH,tcp,127.0.0.1,8080,10.0.2.15,22"
VBoxManage showvminfo "xxx" | grep NIC
  • Connect into the VM in the server by ssh
1
ssh -p 8080 userID@127.0.0.1

3. Some useful commands for headless virtual box

  • Start or stop the VM
1
2
3
4
5
6
nohup VBoxHeadless --startvm "xxx" & # start the VM

VBoxManage controlvm "xxx" savestate # stop the VM and save the state
VBoxManage discardstate "xxx" # discard the state

vboxmanage startvm "xxx" --type emergencystop # stop the VM without save the state
  • Manage the snapshot
1
2
3
4
VBoxManage snapshot "xxx" list # list the snapshot
VBoxManage snapshot "xxx" take "yourDiscribe" --live # take the snapshot
VBoxManage snapshot "xxx" restore "snapshotID" # restore the snapshot
VBoxManage snapshot "xxx" delete "snapshotID" # delete the snapshot
  • List info
1
2
VBoxManage list vms # list the existing VM
VBoxManage list runningvms # list running VM

The entire commands can be found in Chapter 8. VBoxManage (virtualbox.org)