Darrin Bliler
Part 1: Use Cases
VirtualBox Ver: 7.0
Setting up one or more of your own Network Address Translation (NAT) services enables you to control how your VirtualBox Virtual Machines (VMs) interact with your internal network.
By utilizing the VBoxManage CLI and shell scripting you can create a custom NAT network with your own parameters, membership, and rules with VirtualBox’s built-in DHCP server.
VirtualBox provides several networking virtualization modes. If one were to briefly scan the man pages or documentation, it might be easy to miss a bit of nuance regarding implementation of NAT within VirtualBox. The default networking mode for VirtualBox is Network Address Translation (NAT). However, this default mode has several limitations that stand in the way of customization and automation. To gain more control over our VMs networking, we will instead utilize VirtualBox’s Network Address Translation (NAT) service to attach to a network we designate including its CIDR.
By utilizing NAT Network Service as our VirtualBox networking mode, we can programmatically manage our internal networks using the VBoxManage natnetwork command (versus GUI alone). Numerous command options allow us to create as many NAT services / networks as we need, disable / enable the service, and configure DHCP servers, IPv4, IPv6, loopback interfaces, and port forwarding.
Below is an example of how this might be useful in the event you need to set up two virtual development environments with 3 VirtualBox guest VMs running attached to 2 separate VirtualBox NAT services. Of course, you could add as many guest VMs up to the maximum size of the subnet that you created or until you run out of system resources. Additionally, you could forward custom ports on your host machine to the NAT Network service using VBoxManage. Perhaps you would like to set up a development environment or simulate a production environment. Utilizing NAT service within VirtualBox provides an additional layer of control.

Part 2: Scripting
Tasks:
Our script will perform the following tasks:
- Check for existing VirtualBox NAT Networks
- If VirtualBox NAT Network(s) exist, the script will notify the user that there are existing NAT networks found.
- Add the VirtualBox NAT Network using command line parameters for network name and CIDR.
- $1 = network name
- $2 = CIDR
Code:
#!/usr/bin/env bash
# check to see whether natnetwork exists
vbox_nat_network_check() {
local check_output=$(VBoxManage natnetwork list | grep -ic "0 networks found")
if [[ $check_output -eq 1 ]]; then
echo "No NAT networks found. Creating NAT network: $(echo $1)"
elif [[ $check_output -ne 1 ]]; then
echo "NAT networks found. Creating NAT network: $1"
fi
}
echo "Checking for existing VBoxManage NAT Networks"
vbox_nat_network_check
# add natnetwork with command line parameters if no existing nat networks are found
vbox_nat_network_add() {
# Check the number of command line parameters
if test $# -ne 3; then
# [[ ${!#} -ne 2 ]]; then
echo "Usage: vbox_nat_network_add "
echo ${$#}
exit 1
fi
# Add the NAT network with the given name and network settings
VBoxManage natnetwork add --netname "$1" --network "$2" --enable --dhcp on
# Check for errors in the command
if [[ $? -ne 0 ]]; then
echo "Error adding NAT network. Exit code: $?"
exit $?
fi
}
echo "Starting..."
vbox_nat_network_add "$1" "$2"