How to Configure a Traffic Route from VM1 through Host to VM2

Abstract: Enable IP forwarding and configure static routes on Ubuntu to route traffic between VM1 and VM2 through the host.

Created: 2026-10-21 by
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.

Latest News

How to Display Output of xterm Command in Terminal Screen using Cron

Resolve the inability to display xterm output in a terminal screen from cron by redirecting logs or using wall instead of insecure X11 workarounds.

Read More
Expose Local Server on VPS Preserving Real Client IPs (OS Level)

Resolve VPS logging showing only gateway IPs by configuring reverse proxies to forward real client addresses via X-Forwarded-For headers.

Read More
How to Clear Recent Files History in Linux without Deleting Actual Files?

Clear recent files history in Linux by deleting ~/.local/share/recently-used.xbel without removing the actual files.

Read More
Best HDD Copy Strategy: Manually Syncing and Rewriting Scratch

Resolve data drift between two 4TB hard drives by using rsync with the --delete flag to manually sync and rewrite scratch data for an exact mirror.

Read More
Understanding Bash Prompt Abbreviations: An Overview

Resolve repetitive typing errors by creating and persisting Bash aliases in the .bashrc file for improved command-line efficiency.

Read More
Free Tools and Methods to Log Real-Time Performance Details in Task Manager

Log real-time CPU and memory metrics to CSV files using Windows Performance Monitor Data Collector Sets to troubleshoot system slowness.

Read More
Contact Qatar Airways Customer Service: Phone Number, Help Center, and More

Resolve urgent Qatar Airways reservation issues like flight changes and baggage claims using the US phone number +1801-855-5905 or online self-service tools.

Read More
Windows 11: Alt Codes Displaying Wrong Characters - Solutions

Resolve Windows 11 Alt codes showing wrong characters by verifying the US keyboard layout and enabling Num Lock.

Read More
Run Programs with Elevated Privileges: A Comprehensive Guide

Eliminates the need to type sudo for frequent administrative tasks by configuring .bashrc aliases or functions.

Read More
Dynamically Changing Dates in Excel Formulas Using Cells

Eliminate manual date updates in Excel by using cell references to create dynamic formulas that automatically adjust calculations when input dates change.

Read More
Granting Edit Access to a Specific Excel Sheet in an Office 365 User Email ID-Based Login

Excel Online lacks native sheet-level permissions, so users must split workbooks or use worksheet protection to isolate edit access for specific tabs.

Read More
Creating Network Load on Local Wi-Fi from One PC

Generate sustained Wi-Fi bandwidth load from a single PC using a VM SFTP server to test router stability and throughput.

Read More
Changing the Default Editor for Batch Files in Windows 11's File Explorer

Resolve the issue of batch files opening in Notepad by changing the default editor to a preferred application like VS Code.

Read More
Gain WiFi Connection Minutes Before Losing it by Changing MAC Address

Bypass MAC address filtering to restore WiFi connectivity temporarily when a device is blocked by network administrators.

Read More
Photos Folders Turned Black: Troubleshooting Picture Applications on Windows

Resolve black screens in Windows Photos by resetting the app, updating graphics drivers, and repairing system files.

Read More
Troubleshooting High CPU Temperature Values: A Step-by-Step Guide

Resolve high CPU temperatures and thermal throttling by monitoring with Speedfan, cleaning dust from cooling components, and replacing degraded thermal paste.

Read More
Make cURL establish TLS connection with CA unknown

Resolve cURL TLS connection failures with unknown or self-signed certificates by using the --insecure flag to bypass CA validation.

Read More
Troubleshooting Incorrect Network Metrics Values in Grafana, Prometheus, InfluxDB, and Telegraf

Fix incorrect network metrics in Grafana, Prometheus, InfluxDB, and Telegraf by verifying interface selection, resolving unit mismatches, and handling counter resets.

Read More
Start Another Terminal Window (cmd.exe) Windows Repair/Troubleshoot

Provides step-by-step instructions to access the Windows Command Prompt via bootable media for advanced system repair and troubleshooting.

Read More
Thinkpad P1 Gen 6: Sudden Thunderbolt 3 Dock USB-C Charging Stopped Working

Resolve sudden Thunderbolt 3 dock USB-C charging and internet failure on ThinkPad P1 Gen 6 by updating firmware, resetting the dock, and adjusting power settings.

Read More
ASUS Windows 11 Laptop: Internet Connection Issue with Hotspot

Resolve ASUS Windows 11 laptop spinning wheel issue when connecting to mobile hotspot by resetting network stack, disabling IPv6, and adjusting power settings.

Read More
Developing iOS Project with Firebase: Logging in and Sending Push Notifications

Integrates Firebase to enable secure user authentication and push notifications in iOS applications.

Read More
Troubleshooting Bash Script with Suspected Issues of Spaces, Dashes, Filenames and Quoting

Resolve Bash script failures caused by word splitting, globbing, and special characters in filenames by applying proper quoting techniques.

Read More
Populating OneDrive Folder for Local Backup without Affecting Remote Files

Prevent OneDrive from deleting remote files during Windows reinstallation by unlinking the account or using symbolic links to isolate local data.

Read More
Force First Frame as Keyframe Without Re-encoding: A Quick Solution

Resolve inaccurate seeking and playback glitches by re-encoding video to ensure the first frame is a valid keyframe.

Read More
Find Information in Database Using WSL (Ubuntu) on Windows-10

Resolve database connection issues from WSL Ubuntu on Windows 10 by configuring client tools and network access to query remote or local servers.

Read More
Configuring a New Router with Different Networks: One Family, One Guest

Resolve IP conflicts and isolate guest traffic by configuring a second router with a distinct subnet via its WAN port.

Read More
Quickly Open Multiple Stock Websites with a Single Command

Solve the problem of manually opening multiple stock research websites by creating a single command to launch them simultaneously on Windows, macOS, or Linux.

Read More
Improved Adobe Acrobat Search Experience: Access Advanced Search with Ease

Access advanced search filters like case sensitivity and regex in Adobe Acrobat by clicking More Options in the Find bar.

Read More
Port Forwarding VM via OpenVPN: A Comprehensive Guide

Configure OpenVPN server and iptables to forward specific ports from VPN clients to a virtual machine's internal IP address.

Read More
Understanding Partitions in the 1980s: A Look Back at MS-DOS and Master Boot Record

Explains how the Master Boot Record and partition table enabled MS-DOS to manage multiple disk volumes on a single physical drive.

Read More
Windows 10 Enterprise: Keyboard Not Detected in Managed Organization with Admin Privileges

Resolve Windows 10 Enterprise keyboard detection failures caused by Group Policy restrictions, driver corruption, or USB enumeration errors.

Read More
Can't Find x86_64 Versions of Applications on MacOS arm64 (M1)

Resolve missing x86_64 app issues on M1 Macs by installing Rosetta 2 and reinstalling apps as universal binaries.

Read More
How to Hide Installation Source for an Android App

Manage Android app installation source visibility by using USB transfers or file managers to reduce browser traces and isolate data via work profiles.

Read More
Connecting Momentus 4200.2 ST9808210A Hard Drive via USB Cable

Resolve inability to access Seagate Momentus 4200.2 ST9808210A hard drive by connecting it via USB using a compatible SATA-to-USB adapter and configuring the disk in Windows.

Read More