Manage VirtualBox VMs From Command Line using VboxManage

Posted on 100 views

There was a time back then when the command line was the only mode you can use to communicate with your system. Times have changed and GUI tools have been developed to ease the system administration task. But then, there are times that you need a low-level way to interact with your system and its components, that is the command line.

The Oracle VirtualBox is a virtualization tool that allows one to run multiple VMs simultaneously. VirtualBox comes with a pretty and easy-to-use GUI convenient for managing Virtual Machines(VMs). In addition to that, there is VBOxManage, the command-line tool for VirtualBox installed by default. Aside from the two, Oracle VirtualBox offers some other tools to use to control the VM i.e the Main API, implemented using the Component Object Model (COM/XPCOM) and the web service, which maps nearly the entire Main API for web applications. These components interact as shown in the diagram below.

Manage-VirtualBox-VMs-From-Command-Line-using-VboxManage

As much as the GUI tool is easy to use, VBoxManage gives you more functionality such as:

  1. It gives one direct access to the virtualization engine
  2. It allows you to access extra features unavailable through the GUI.
  3. Ability to manage your VMs even in headless mode on servers with no GUI.

Using the VBoxManage CLI is easy. Remember, all commands begin with vboxmanage and then are followed by a subcommand such as controlvmlist, etc. This guide aims to equip you with knowledge on how to manage VirtualBox VMs From Command Line using VboxManage

Setup pre-reqs

Before we commence on this guide, you are required to have the latest version of VirtualBox installed on your system. This can be achieved using the aid from the below guides:

1. List Virtual Machines using VboxManage

Before you begin managing the VMs using VboxManage, it is important that you list and get to know the VM and their names on your system. You will notice that each VM has its own Universally Unique Identifier (UUID) which identifies the VM.

vboxmanage list vms 

Sample Output:

Manage-VirtualBox-VMs-From-Command-Line-using-VboxManage-1

This command can be elongated to provide detailed information for each VM using the -l or --long flag

vboxmanage list vms --long

At times, you may be interested in listing only running VMs. This can be done using the command below.

vboxmanage list runningvms

2. Get the VM Info usingVboxManage

You can also get more information related to a specific VM such as the hardware, network, and other configuration information using the command below.

vboxmanage showvminfo ubuntu20

Sample output:

Manage-VirtualBox-VMs-From-Command-Line-using-VboxManage-2

3. Start and Stop a VM using VboxManage

After listing available VMs on your system, you can start a VM say Ubuntu20 as below.

vboxmanage startvm ubuntu20

Sample Output:

Manage-VirtualBox-VMs-From-Command-Line-using-VboxManage-3

This VM will now be available under running VMs as below.

$ vboxmanage list runningvms
"ubuntu20" 3b21f1b4-b93f-4e82-909a-47ecea31f097

Now stopping the VM, we use the controlvm subcommand, the VM name and poweroff option.

vboxmanage controlvm ubuntu20 poweroff

Sample Output:

Manage-VirtualBox-VMs-From-Command-Line-using-VboxManage-4

From the output, you can see the shutdown progress.

Alternatively, you can pause a VM and resume it later using the command:

vboxmanage controlvm ubuntu20 pause

The paused VM will still be listed under running VMs but currently not consuming the system’s resources. Additional information will be shown such as the time when the VM was paused.

vboxmanage list runningvms -l

Output:

Manage-VirtualBox-VMs-From-Command-Line-using-VboxManage-5

The paused VM can be resumed as below.

vboxmanage controlvm ubuntu20 resume

4. Create a VM Using VboxManage.

Another important task when managing VMs is knowing how to create a VM and make configurations to it. For example to create a Fedora Linux VM, run the command:

vboxmanage createvm --name FedoraLinuxVM --ostype Fedora_64 --register

Sample Output:

Virtual machine 'FedoraLinuxVM' is created and registered.
UUID: 7d2a0d73-8ff7-4379-b48b-fdfda9fbcbd5
Settings file: '/home/thor/VirtualBox VMs/FedoraLinuxVM/FedoraLinuxVM.vbox'

In case you are not sure of the available OS types, check as below.

vboxmanage list ostypes

Sample output:

Manage-VirtualBox-VMs-From-Command-Line-using-VboxManage-6

Now you will have your Fedora VM created, you can modify the VM such as changing the name, assigning memory using the modifyvmsubcommand.

For example, we can change the name from FedoraLinuxVM to Fedora35 as below.

vboxmanage modifyvm FedoraLinuxVM --name Fedora35

Assign memory, CPUs, and the graphic controller to the VM as below.

vboxmanage modifyvm Fedora35 --memory 4096 --cpus 2 --vram 20 --graphicscontroller vmsvga --rtcuseutc on

5. Set VM network usingVboxManage

VboxManage can be used to update VM Network settings – e.g set network type to bridge. See an example below:

VBoxManage modifyvm Fedora35 \
--nic1 bridged \
--bridgeadapter1 eth0 \
--vrde on \
--vrdeport 5001

6. Manage Storage for a VM usingVboxManage

Creating the storage for the VM is a bit different, here we use several subcommands as shown.

Create the storage for the VM:

vboxmanage storagectl Fedora35 --name SATA --add sata --controller IntelAhci --portcount 30 --bootable on

Now create the hard disk, set the disk size and attach it to the VM:

First export the VM name and path for the storage disk as below.

VM_NAME='Fedora35'
VM_DISK=$(realpath ~/VirtualBox\ VMs/$VM_NAME)/$VM_NAME.vdi

Now proceed and create the storage.

vboxmanage createmedium --filename "$VM_DISK" --size 10240 --format VDI
vboxmanage storageattach "$VM_NAME" --storagectl SATA --type hdd --port 0 --device 0 --medium "$VM_DISK"

Mount/attach an ISO file for installation on your VM using the below steps.

Export the PATH of your ISO file.

ISO_DISK=$(realpath ~/Downloads/fedora-coreos-35.20210711.3.0-live.x86_64.iso)

Now mount the ISO as below.

vboxmanage storagectl Fedora35 --name IDE --add ide --controller PIIX4 --hostiocache on --portcount 2 --bootable on
vboxmanage storageattach Fedora35 --storagectl IDE --type dvddrive --port 1 --device 0 --medium "$ISO_DISK"

7. Take and restore Snapshots usingVboxManage

Taking Snapshots is a very important task in keeping systems secure. On VirtualBox, users have the ability to take and revert to snapshots whenever something unusual happens to the system.

Taking a snapshot of a VM is easy. For example, I will take a snapshot of the Ubuntu20 VM and name the snapshot December 14th snapshot.

vboxmanage snapshot ubuntu20 take "December 14th snapshot"

Sample Output:

Manage-VirtualBox-VMs-From-Command-Line-using-VboxManage-7

You can later restore to the above snapshot. Ensure that the VM is stopped before you restore the snapshot.

vboxmanage snapshot ubuntu20 restore "December 14th snapshot"

8. Delete a VM using VboxManage

You can delete a VM if you no longer need it as below:

VBoxManage unregistervm --delete Fedora35

Sample Output:

Manage-VirtualBox-VMs-From-Command-Line-using-VboxManage-8

9. Get Help on VboxManage

In case you get stuck when using VboxManage, you can always get help. This will provide you will the options to use and navigate along.

$ vboxmanage
........
  natnetwork                add --netname 
                            --network 
                            [--enable|--disable]
                            [--dhcp on|off]
                            [--port-forward-4 ]
                            [--loopback-4 ]
                            [--ipv6 on|off]
                            [--port-forward-6 ]
                            [--loopback-6 ]

  natnetwork                remove --netname 

  natnetwork                modify --netname 
                            [--network ]
                            [--enable|--disable]
                            [--dhcp on|off]
                            [--port-forward-4 ]
                            [--loopback-4 ]
                            [--ipv6 on|off]
                            [--port-forward-6 ]
                            [--loopback-6 ]

  natnetwork                start --netname 

  natnetwork                stop --netname 

  natnetwork                list []

  hostonlyif                ipconfig 
                            [--dhcp |
                            --ip  [--netmask  (def:255.255.255.0)]|
                            --ipv6  [--netmasklengthv6  (def:64)]] |
                            create |
                            remove 

  usbdevsource              add 
                            --backend 
                            --address 
usbdevsource remove ........ Create a clone of an existing Oracle VM VirtualBox virtual machine: VBoxManage clonevm [--basefolder=basefolder] [--groups=group,...] [--mode=machine | --mode=machinechildren | --mode=all] [--name=name] [--options=option,...] [--register] [--snapshot=snapshot-name] [--uuid=uuid] ...........

Conclusion.

This was pretty informative! I hope you enjoyed this guide on how to Manage VirtualBox VMs From Command Line using VboxManage.

coffee

Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.