VirtualBox, 16 practical examples

VirtualBox is a powerful virtualization product for x86 and AMD64/Intel64 architectures; It is features rich and performs very well on old hardware. Its source code is released under GNU General Public License (GPL v2). It can be installed on Windows, GNU/Linux and UNIX: Solaris, Macintosh, FreeBSD and allows a large number of client operating systems such as: Windows, GNU/Linux, Solaris and OpenSolaris, OpenBSD/FreeBSD/NetBSD.

VirtualBox is in active development with frequent releases that include new features, new client operating systems and new platforms on which it runs.

With VirtualBox you can install and run as many virtual machines as possible as disk space and memory allow, remember that both the space and memory allocated to the virtual machine are taken from the host operating system.

VirtualBox is extremely simple but also powerful. It can be run in embedded systems, desktops, internet servers, data centers, and cloud environments.

The command to manage the virtual machines is VBoxManage, however in GNU/Linux and *BSD the lowercase version: vboxmanage can be used.

Crear/Configurar la máquina virtual

1. List all known guest operating systems

List all known guest operating systems, along with the identifiers used to refer to them with the modifyvm command.

$ vboxmanage list ostypes
...
ID:          Debian
Description: Debian (32-bit)
Family ID:   Linux
Family Desc: Linux
64 bit:      false

ID:          Debian_64
Description: Debian (64-bit)
Family ID:   Linux
Family Desc: Linux
64 bit:      true 
...

2. Create the VM

$ vboxmanage createvm --name DebianTest --ostype Debian_64 --register
Virtual machine 'DebianTest' is created and registered.
UUID: 309219c5-0c68-4567-a7c0-ee89127389a2
Settings file: '~/VirtualBox VMs/DebianTest/DebianTest.vbox'

With the above command we create the XML definition file: ~/VirtualBox VMs/DebianTest/DebianTest.vbox, it has the default configuration of the virtual machine, it worth noting that at this point we have not installed any guest operating system, nor we are still ready to install it because at least one storage medium needs to be added.

The –name option is required and specifies the name of the virtual machine.

The –register option means that the virtual machine has been registered and is now visible to perform certain operations on it.

You can inspect the properties of the virtual machine using showvminfo and use the modifyvm command to change the desired property, in this case we will only modify the RAM.

3. Increase VM RAM

$ vboxmanage modifyvm DebianTest --memory 1024

With the above command we increase the RAM of the virtual machine to 1GB, see the vboxmanage showvminfo manual for the properties/options you can modify.

4. Configure network adapter

$ vboxmanage modifyvm DebianTest --nic1 bridged --bridgeadapter1 wlp2s0

VirtualBox by default configures the network adapter in NAT mode which means that the virtual machine has access to the Internet but it cannot be accessed from other devices on the network, therefore the above command guarantees that the virtual machine is accessible from others devices in the LAN, wlp2s0 is the network (wireless) interface of the host operating system.

See the User Manual for more information on how to configure the virtual machine network.

5. Adding storage device

Just like a computer or physical device, the virtual machine needs a storage medium to host the guest operating system and other applications.

Creating the virtual hard disk

When creating the storage medium we can create it as a dynamic size or as a fixed size. The difference between these two variants is that a dynamically sized image initially takes up little storage space on the real hard disk, that is, on the host operating system, growing as the amount of space used by the guest operating system increases. On the other hand, when creating a fixed-size image, the amount of space specified by the –size parameter is allocated at once, which can lead to inefficient use of disk space but requires less processing and therefore may be slightly faster than a dynamically sized image.

$ vboxmanage createmedium disk  --filename VirtualBox\ VMs/DebianTest/DebianTest.vdi --size 8192
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Medium created. UUID: c777db09-95ff-40e4-8ca7-2cb87ee73905

With the previous command we create a 8GB dynamic sized virtual hard disk in VDI format.

See User Manual for more information on the different formats and features of storage media.

Now we need to add the storage controller to be used with the virtual disk created earlier.

Add the storage controller

$ vboxmanage storagectl DebianTest --name "SATA Controller" --add sata --bootable on

With the above command we add a bootable SATA controller to our virtual machine, you can consult the Virtual Box manual for more information.

Attach the virtual hard disk to the storage controller

With the following command we connect the virtual hard disk to the storage controller.

$ vboxmanage storageattach DebianTest --storagectl "SATA Controller" 
--port 0 --device 0 --type hdd 
--medium VirtualBox\ VMs/DebianTest/DebianTest.vdi

6. Installing the guest operating system

You must configure the virtual machine to boot from the installation media, so that the installation begins when the virtual machine starts up for the first time. Whether you are using an installation DVD or an ISO image, you must create a virtual CD/DVD drive and connect it to the installation media. Like virtual hard drives, virtual CD/DVD drives require a storage controller. While this can be done using a SATA controller for many operating systems, the following example illustrates the addition of an IDE controller for the CD/DVD drive.

Add storage controller

$ vboxmanage storagectl DebianTest --name "IDE Controller" --add ide

Attach the ISO image

$ vboxmanage storageattach DebianTest --storagectl "IDE Controller" --port 0  --device 0 --type dvddrive --medium Downloads/debian-10.1.0-amd64-netinst.iso

Now you can start the virtual machine and start the client operating system installation using the following command.

$ vboxmanage startvm DebianTest

7. List all VMs

$ vboxmanage list vms

8. List running VMs

$ vboxmanage list runningvms

9. Start the VM

$ vboxmanage startvm DebianTest

10. Allows VirtualBox access privileged port

Ir order to allow VirtualBox redirect/access priveleges port (<= 1024) you need to set this enviroment variable

export VBOX_HARD_CAP_NET_BIND_SERVICE=1

11. Power off the VM

$ vboxmanage controlvm DebianTest poweroff

12. Show info about a VM

With this command we show detailed information about the virtual machine, for example client operating system, allocated memory, network configuration, …

$ vboxmanage showvminfo DebianTest

13. Register a VM

With the following command we register the virtual machine and now it is visible to execute certain operations on it.

$ vboxmanage registervm 'VirtualBox VMs/DebianTest/DebianTest.vbox'

If you have an error executing the above command then specify the absolute path to the .vbox file

14. Unregister a VM

With the following command we mark the VM as unavailable for VirtualBox therefore actions cannot be performed on it.

This command does not delete the definition and the files associated with the virtual machine.

To register the virtual machine again you must run the above command.

$ vboxmanage unregistervm DebianTest

15. Clone a VM

With this command we create a virtual machine from an existing virtual machine. The cloned virtual machine has the same characteristics as the base virtual machine.

$ vboxmanage clonevm DebianTest --name DebianTestNuevaMV --register

16. Delete a VM

With this command we delete the virtual machine as well as its associated files.

$ vboxmanage unregistervm --delete DebianTest

Futher readings


YouTube video

YouTube video

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.