To route traffic from VM1 to VM2 through the host in VirtualBox, you must enable IP forwarding on the Ubuntu host and configure static routes within both virtual machines. This setup transforms your host into a gateway, allowing isolated network segments to communicate by explicitly directing packets through the host’s interface rather than relying on direct VM-to-VM links.
Network Topology and Prerequisites
Before modifying configurations, ensure your VirtualBox environment is structured correctly. The most reliable method for this specific routing requirement involves using "Host-only Adapter" networks or bridged interfaces that allow the host to see both VMs. For this guide, we assume VM1 and VM2 are connected via Host-only adapters, which creates an internal network segment where the host acts as the central router.
First, verify that both VMs have valid IP addresses on the same subnet or that their subnets are known. You will need root access on the Ubuntu host to modify kernel parameters. Additionally, ensure that firewalls on both VMs and the host are configured to allow ICMP (ping) and TCP/UDP traffic between these specific IPs. Without proper firewall rules, even correct routing will fail silently because packets are dropped at the network layer.
Enable IP Forwarding on the Ubuntu Host
The core of this configuration lies in enabling the Linux kernel to forward packets between network interfaces. By default, Ubuntu disables IP forwarding for security reasons, treating the machine as an endpoint rather than a router. To change this behavior temporarily for testing, run the following command in the terminal:
sudo sysctl net.ipv4.ip_forward=1
This command immediately enables forwarding for the current session. However, this setting will revert after a reboot. To make it permanent, you must edit the sysctl configuration file. Open the file
/etc/sysctl.conf with your preferred text editor, such as nano or vim:
sudo nano /etc/sysctl.conf
Search for the line containing
net.ipv4.ip_forward. If it is commented out with a hash (
#), remove the hash. If the line does not exist, add it manually. Set the value to 1:
net.ipv4.ip_forward=1
Save the file and exit the editor. Apply the changes by running:
sudo sysctl -p
You can verify that forwarding is active by checking the output of
cat /proc/sys/net/ipv4/ip_forward; it should return
1. Note that if you are using NAT Network configurations instead of Host-only, you may also need to adjust NAT settings in the VirtualBox manager, but Host-only is the standard approach for host-mediated routing.
Configure Static Routes in VM1 and VM2
Once the host is ready to forward packets, the virtual machines must know how to reach each other. They cannot rely on ARP alone if they are on different logical subnets or if the host needs to intercept the traffic. You need to add static routes that point to the host’s IP address on the host-only network interface.
Identify the IP address of the host’s interface that connects to the VMs. Let’s assume the host’s interface IP is
192.168.56.1. In VM1, open a terminal and add a route to the subnet of VM2 via the host:
sudo ip route add 192.168.56.0/24 via 192.168.56.1
Replace
192.168.56.0/24 with the actual network CIDR block of VM2. Repeat this process in VM2, adding a route to VM1’s subnet via the same host IP. These commands establish the path for incoming and outgoing traffic.
For persistent routes across reboots, the method depends on your Ubuntu version. In modern Ubuntu systems using Netplan, you might need to edit the YAML configuration files in
/etc/netplan/. Alternatively, you can use the
/etc/network/interfaces file if you are using traditional ifupdown. Ensure that the route is defined so that it survives restarts. If you are managing multiple VMs, consider scripting this addition to avoid manual errors.
Verify Connectivity and Troubleshooting
After configuring the routes, test the connection by pinging VM2 from VM1. Use the
ping command followed by VM2’s IP address. If the ping succeeds, the routing is working correctly. If it fails, check the following common issues.
First, verify the firewall settings on the Ubuntu host. UFW (Uncomplicated Firewall) might be blocking forwarded traffic. Check the status with
sudo ufw status. If it is active, ensure that forwarding is allowed. You may need to set
DEFAULT_FORWARD_POLICY="ACCEPT" in
/etc/default/ufw and reload the service.
Second, check for asymmetric routing or NAT conflicts. If one VM is on a NAT network and the other on a Host-only network, the host must perform NAT translation. In this case, simple routing is insufficient; you need MASQUERADE rules in iptables or nftables. However, for pure Host-only setups, no NAT is required.
Finally, inspect the routing tables in both VMs using
ip route show to ensure the default gateway or specific routes are correctly pointing to the host. If you encounter intermittent connectivity, check for MTU mismatches, although this is rare in virtualized environments unless you are tunneling traffic.
For more details on managing virtual network interfaces, you might find
Can't Connect to VM Remotely: Troubleshooting Guide helpful, as it covers similar connectivity diagnostics. Additionally, understanding how to manage disk space efficiently can prevent performance issues during heavy network loads, as discussed in
How to Configure Linux Guest with Dynamic Space in VirtualBox.
Frequently Asked Questions
Why can I ping the host but not VM2 from VM1?
This usually indicates that the route in VM1 is incorrect or missing. Verify that the destination IP belongs to the subnet specified in your static route. Also, ensure that VM2 has a route back to VM1, as routing is bidirectional. If VM1 sends the packet to the host, the host forwards it to VM2, but VM2 must know how to send the reply back through the host.
Does enabling IP forwarding expose my host to security risks?
Yes, enabling IP forwarding allows your host to act as a router, which can potentially expose internal networks if not properly segmented. Always ensure that your host firewall (UFW or iptables) is configured to restrict which traffic is allowed to pass between the VMs. Do not enable forwarding on public-facing interfaces without strict filtering rules.
How do I make static routes persistent in Ubuntu VMs?
In modern Ubuntu versions using Netplan, you should define static routes in the Netplan YAML configuration files located in
/etc/netplan/. For older versions or specific distributions, you can add routes to
/etc/network/interfaces using the
post-up ip route add... directive. After editing, apply the changes with
netplan apply or by restarting the networking service.
What if my VMs are on different subnets?
If VMs are on different subnets, you must configure static routes on both VMs pointing to the host’s IP as the next hop for the other’s subnet. The host itself does not need special routing if it is directly connected to both subnets via separate interfaces, but IP forwarding must still be enabled. Ensure that the host’s firewall allows forwarding between these distinct interfaces.