Set up a VirtualBox VM with 4 VBoxManage commands
How to create an IP-connectable virtual machine through the CLI
Oracle VM VirtualBox is used for several IT workloads. It provides an easy-to-use GUI that allows users to set up their VMs in multiple host operating systems. Nowadays, IT teams must think on automation of a large sort of tasks, including VirtualBox VMs setup. Wait, there’s a problem here! When talking about automation, GUI is usually not an option. The good news is VirtualBox also provides a powerful CLI, VBoxManage, which helps to get the automation job done.
Putting the right gears to work, in this article I’ll demonstrate how to use VBoxManage to set up an IP-connectable VirtualBox virtual machine — from import to start — using only 4 mandatory commands.
For the sake of simplicity, I’m considering a fresh VirtualBox instance to describe the core steps — all of them will be executed in the host machine, by the way. Source code is available on GitHub: https://github.com/ricardolsmendes/vboxmanage-ova-setup.
Create a host-only network
VirtualBox comes with a bundled DHCP server. To make sure it’s up and running, type VBoxManage list dhcpservers
in the host machine terminal. The expected result is:
NetworkName: HostInterfaceNetworking-vboxnet0
IP: 192.168.56.100
NetworkMask: 255.255.255.0
lowerIPAddress: 192.168.56.101
upperIPAddress: 192.168.56.254
Enabled: Yes
All information is relevant, but please notice the network name: vboxnet0. This network is created automatically when VirtualBox is installed. In the next steps, it will be used to connect the host machine to its guest VMs (and may also be used to interconnect guest VMs).
VirtualBox provides 5 networking modes:
According to the above table, Host-only networking brings a communication channel between the host machine and the guest VMs, and so, is suitable for the connectivity we’re looking for.
Now, type VBoxManage list hostonlyifs
in the host machine’s terminal. Empty result… This happens because a host-only interface was not created yet. To create one, run VBoxManage hostonlyif create
. After that, running VBoxManage list hostonlyifs
again should result in the below content:
Name: vboxnet0
GUID: 786f6276–656e-4074–8000–0a0027000000
DHCP: Disabled
IPAddress: 192.168.56.1
NetworkMask: 255.255.255.0
IPV6Address: fe80:0000:0000:0000:0800:27ff:fe00:0000
IPV6NetworkMaskPrefixLength: 64
HardwareAddress: 0a:00:27:00:00:00
MediumType: Ethernet
Status: Up
VBoxNetworkName: HostInterfaceNetworking-vboxnet0
That’s all we need to do for the host machine.
Import, set up, and start a guest VM
VirtualBox appliances are distributed as OVA files. VBoxManage import
will be used to import them. It provides a dry-run mode which allows us to evaluate the virtual machine requirements. Please run VBoxManage import $OVA_FILE --dry-run
to see the result.
Next, run VBoxManage import $OVA_FILE --vsys 0 --vmname $VM_NAME --eula accept
. This only sets the VM name and accepts a license agreement to avoid the “Cannot import until the license agreement is accepted” error, but many other options can be used — https://www.virtualbox.org/manual/ch08.html#vboxmanage-import for reference.
In the next step, a network adapter is added to the VM: VBoxManage modifyvm $VM_NAME --nic2 hostonly --hostonlyadapter2 vboxnet0
. Based on what we’ve seen so far, I bet you understood what it does. You're right! It configures the network adapter to work on host-only mode, connected to the vboxnet0 network :).
By default the VM has one interface, which is using NAT. Leave this alone as it provides access to the internet.
Finally, the VM may be started: VBoxManage startvm $VM_NAME -- type=headless
. The headless option is used to avoid problems when there’s no UI available for the VM.
With this small set of steps, you’ll be able to import, set up, and start a VM using VBoxManage. Pretty straightforward, isn’t it?
Connect to the guest VM
Establishing connections to the guest VM is also straightforward when its IP address is known. Please keep in mind its dynamic vboxnet0 network IP is assigned by the DHCP server we saw at the beginning of the article. But this simple task may become painful (particularly for automation) in case The Oracle VM VirtualBox Guest Additions is not installed in the guest VM.
Starting from the simplest case — Guest Additions installed, one command is enough to get the dynamic IP address: VBoxManage guestproperty get $VM_NAME "/VirtualBox/GuestInfo/Net/0/V4/IP" | awk '{ print $2 }'
.
In case it fails,
VBoxManage guestproperty enumerate $VM_NAME
might be useful for troubleshooting, since it lists all guest VM properties.
Unfortunately, things are not so easy when Guest Additions is not installed. As far as I know, there’s no simple & safe way to automatically get the guest VM IP. I would love to be wrong, so please let me know in case I am!
When there’s only one guest VM in the network, there’s a chance the lower IP address in the DHCP range — 192.168.56.101 — is assigned to it.
Another option is to use arp-scan
to list all IP addresses from a given network and look for which one was assigned to the VM:
sudo apt-get install arp-scan
sudo arp-scan --interface=vboxnet0 --localnet
Both options would require more complex scripts to automatically get the correct IP.
Conclusion
Well, before finishing: what are the 4 mandatory commands I mentioned in the beginning? I wrote more than this to express my line of reasoning, but only 4 are mandatory:
And that’s all, folks! Hope it helps :).
References
- Controlling VirtualBox from the Command Line Using Oracle VM VBoxManage: https://www.oracle.com/technical-resources/articles/it-infrastructure/admin-manage-vbox-cli.html
- Oracle® VM VirtualBox® User Manual — Host-Only Networking: https://www.virtualbox.org/manual/ch06.html#network_hostonly
- Oracle® VM VirtualBox® User Manual — VBoxManage modifyvm: https://www.virtualbox.org/manual/ch08.html#vboxmanage-modifyvm
- Setting up Virtualbox ‘host-only’ networking using CLI in Linux: https://zarsec2.wordpress.com/2018/05/15/vbox-cli/