There are several modes one can use for networking in VirtualBox. In this article I presented one way for network setup. VMs talk each other using internal network separated from the host machine. Additionaly, I created OpnSense router to access VMs in internal network from the host.

To quickly spin desired number of VMs I used VBoxManage CLI utility and Ubuntu VBox cloud image.

Virtual Machines - wooden boxes on the ship

Networking in VirtualBox - Photo by Pixabay on Pexels

Configuration steps Link to heading

Install OpnSense Link to heading

Follow guide in the referenced article.

After starting OpnSense and configuring both WAN and LAN interfaces, enable web ui to be accessed from WAN (host network).

Follow the steps:

  1. Start OpnSense VM
  2. Enter OpnSense shell (option no. #8 in the menu)
  3. Disable packet filtering pfctl -d
  4. Create firewall rule for WAN interface and port 80 or 443

Prepare cloud-init data Link to heading

echo "instance-id: $(uuidgen)" > meta-data
echo "local-hostname: my-hostname" >> meta-data

cloud-localds seed.iso user-data meta-data

To prepare seed.iso image, first user-data config file has to be created. user-data typically contains information about user accounts to be created and/or packages to be installed. For more information check cloud-init docs.

cloud-localds tool can be installed with apt install cloud-image-utils on Ubuntu.

By default cloud-init configures first available network interface in VM to obtain IP address from DHCP. OpnSense provides DHCP and DNS services. To distinguish VMs in the internal local network, change my-hostname in meta-data to unique name accross the internal network.

Create VM from recent Ubuntu cloud image Link to heading

VBoxManage import jammy-server-cloudimg-amd64.ova --vsys 0 --vmname my-vm --cpus 2 --memory 2048 --unit 9 --ignore

This command imports a new virtual machine from VirtualBox compatible image. The OVA Ubuntu image is available to download from Ubuntu cloud images website.

Provide cloud-init configuration to VM Link to heading

VBoxManage storageattach my-vm --storagectl IDE --port 0 --device 0 --type dvddrive --medium seed.iso

The could-init configuration is read from previously created ISO image mounted as DVD-ROM in VM.

Attach VM to the internal network Link to heading

VBoxManage modifyvm my-vm --nic1 intnet --intnet1 my-intnet --nictype1 virtio --nic-promisc1 allow-vms

VirtIO network interface is selected, which gives more performant network communication in virtualized environment.

Summary Link to heading

There are bash scripts available on my github, which can simplify the process of creating the VMs.

References Link to heading