Split Gateway Tutorial
4,684 Total views across this article’s versions; includes the historical baseline where available.
Thinking of creating split routes or split gateways? This article introduces the building blocks of directing different traffic through different gateways, using a simple VPN and direct Internet connection as the intended example.
Why Split the Traffic?
The basic idea is to choose which traffic uses which gateway. For example, I might want certain Internet traffic to go through a VPN and other traffic to use my regular Internet connection directly. I could also choose between two VPNs, or use more than two gateways. There are many possibilities.
Those choices might be made on the host itself, for its own connections, or on a dedicated gateway that routes traffic for other devices. A gateway can, in turn, send selected traffic toward other gateways. Privacy and data protection are important reasons for doing this, but the routing decision depends on what you want each connection to do.
An unnecessarily all-or-nothing VPN setup is short-sighted, in my opinion. I want control over which traffic uses the VPN and which traffic doesn’t. Some VPN clients already offer split-tunneling or local-network access settings, so check what is available before adding your own routes.
Let’s keep this article relatively simple. The introductory example focuses on one host choosing between a VPN path and a direct Internet path according to the destination IP address or network. Access to a LAN printer is another useful example of traffic that should stay local, though a printer on the same subnet may already have a direct route and need no additional gateway.
Multiple VPNs, routing for an entire LAN, and more advanced gateway policies are topics for a possible follow-up article. That could include a pfSense router selecting different VPN or direct Internet gateways according to the incoming interface or the external destination address.
Routing and protection are related, but they are not the same thing. For this example, traffic designated for the VPN should be blocked if that VPN becomes unavailable. Traffic deliberately assigned to the direct connection should remain usable. The firewall policy must enforce that distinction; routing alone is not a kill switch.
What this article is:
- A basic introduction to selecting different paths for different destinations
- A simple VPN/direct-Internet use case, with recovery and connection testing
What this article isn’t:
- A complete multi-VPN or pfSense configuration guide
- A detailed treatment of load balancing or automatic failover
For broader background, see Introduction to Split Gateways.
One Host, Two Paths
Let’s start with the host itself. It already has a working Internet connection through its LAN router. It also has a working VPN tunnel. We want selected destinations to use that tunnel, while other Internet destinations continue to use the regular connection.
There is no second physical Internet connection in this example. The VPN’s encrypted connection travels through the same LAN router, but the traffic inside that tunnel exits through the remote VPN gateway.
| Item | Example Value | Purpose |
|---|---|---|
| Host’s LAN interface and address | eth0, 192.168.50.10/24 |
Connects this host to its LAN |
| LAN router | 192.168.50.1 |
Existing direct Internet gateway |
| Host’s VPN interface and address | tun0, 10.8.0.2, peer 10.8.0.1 |
Already configured point-to-point IPv4 tunnel |
| VPN server’s public address | 198.51.100.20 |
Encrypted VPN transport stays on the direct path |
| Destinations assigned to the VPN | 203.0.113.0/24 |
Only this selected network is routed into the tunnel |
| Example direct Internet destination | 198.51.100.80 |
Uses the existing default route |
| Optional LAN printer | 192.168.50.25 |
Stays on the connected LAN |
The public-looking addresses above are documentation addresses, not public test servers. Replace them with your actual VPN endpoint and destinations you can test. Replace the interface names and private addresses too, wherever they appear. Keep the selected VPN network separate from the LAN, tunnel addresses, and VPN server’s public address.
How the Host Chooses a Path
For this example, the host uses its normal routing rules and the main routing table. Within that table, the most specific matching destination prefix wins. A route for our selected /24 network takes precedence over the default /0 route. The existing connected route handles the LAN printer. Other existing, more specific routes still matter; inspect them before choosing your destinations.
We don’t need additional routing tables or ip rule changes for this destination-only example. The rt_tables file maps table names to numbers; it isn’t a master list that executes those tables in order. Policy rules determine which tables are consulted. See the routing-policy manual for that distinction.
This is selective routing, not load balancing. Even dividing the IPv4 address space into two /1 networks would divide addresses, not guarantee equal traffic volume. The first /1 covers only the lower half of that address space.
Prerequisites
This walkthrough assumes a Linux test host with iproute2, iptables, and administrative access. The host originates the connections; it is not forwarding traffic for other devices.
- The VPN is already configured, authenticated, and able to carry traffic to the selected destinations. Here,
tun0is a point-to-point TUN interface with the local and peer addresses shown above. This is not a VPN installation guide or a command set for every tunnel type. - The LAN router already provides Internet access. The remote VPN gateway already provides the required onward routing, return path, and any necessary NAT. No forwarding or masquerading rules are added to this host.
- The VPN client leaves the direct default route in place. It does not install a full-tunnel redirect, overriding policy rules, or a kill switch that blocks the intended direct traffic. If your client manages these policies, configure its supported split-tunneling feature instead of layering this example over it.
- iptables is the firewall interface used for this exercise. No other firewall manager or VPN client is replacing these rules. Existing firewall policy must permit the intended connections; the narrow restriction below does not grant access through an otherwise blocking firewall.
- This is an IPv4-only exercise. Use a test host with IPv6 disabled or separately blocked under its existing configuration. These IPv4 commands do not protect IPv6, and forcing one test to use IPv4 does not prevent other applications from using IPv6.
Keep the recovery access described above available. Stop applications using the selected destinations before changing their path, and use fresh connections for testing. Changing a route does not safely migrate an existing connection between public addresses.
For the initial path checks, use numeric destination addresses so DNS is a separate test. This example leaves the existing resolver configuration in place; requests to a resolver outside the selected network normally take the direct path. If you require DNS through the VPN, configure and protect that resolver path separately before everyday use.
Step 1: Inspect the Existing Configuration
The addresses in the table describe an already configured host. Don’t assign them to your interfaces just to make the example match.
Inspect the actual configuration first:
ip -4 address show dev eth0
ip -4 address show dev tun0
ip -4 rule show
ip -4 route show table main
ip -4 route get 198.51.100.20
sudo iptables -w -S OUTPUT
The normal IPv4 policy rules look up local, then main, then default. If additional rules select another table for this traffic, stop and reconcile that configuration first. The VPN server’s public address must resolve through 192.168.50.1 on eth0, so the tunnel doesn’t try to carry its own transport connection.
Your main table should already include routes equivalent to these, alongside the VPN’s own route entries:
default via 192.168.50.1 dev eth0
192.168.50.0/24 dev eth0 proto kernel scope link src 192.168.50.10
These are illustrative output lines, not commands. Leave the existing default and LAN routes in place. If the selected VPN route or the marked firewall rule below already exists, inspect it before proceeding. This is a one-time walkthrough, not a script to run repeatedly.
Step 2: Block Direct Fallback with iptables
If the tunnel disappears, its route may disappear too. The direct default route would then provide a possible path to our VPN destinations. Let’s block that fallback before adding the route.
Run this command once, and stop if it fails:
sudo iptables -w -I OUTPUT 1 -d 203.0.113.0/24 ! -o tun0 -m comment --comment split-vpn-guard -j DROP
This drops locally generated IPv4 packets addressed to the selected network when their outgoing interface is anything other than tun0. Placing it first in OUTPUT keeps an earlier general accept rule from bypassing it. It does not block our deliberately direct destinations or the VPN server’s encrypted transport, whose destination is outside the selected network.
Confirm the rule exists and is first in the chain before continuing:
sudo iptables -w -C OUTPUT -d 203.0.113.0/24 ! -o tun0 -m comment --comment split-vpn-guard -j DROP
sudo iptables -w -S OUTPUT
The -C check normally succeeds without output. Any error needs investigation. Inserting the rule again would create a duplicate; a successful presence check also doesn’t establish its position, which is why we inspect the chain. These operations are described in the iptables manual.
Packets blocked by DROP are discarded without a rejection response, so a connection attempt may time out.
Keep this guard in place during VPN outages and reconnects. It depends on the protected destination list and tunnel name remaining correct, and on other software leaving it in force. It is not a complete DNS, IPv6, application, or system-wide privacy policy.
The selection is by IP destination, not by website name or application. A website may use several changing addresses, redirects, or external services. Anything outside the selected network follows the remaining routing policy, usually the direct default. An application-wide VPN requirement needs a different design.
Step 3: Add the Selected VPN Route
Now send the selected network through the existing point-to-point tunnel:
sudo ip -4 route add 203.0.113.0/24 dev tun0 src 10.8.0.2
Here, src is the preferred local source address for new connections using this route. It is not a gateway address. The route sends packets into the point-to-point tunnel; we don’t supply a LAN-style next-hop address for this topology. The VPN configuration is responsible for carrying them to the remote side. See the ip route manual for the route fields.
Stop if the command fails. If it reports an existing route, inspect that route instead of deleting or replacing it blindly. The command adds to the running configuration; it doesn’t make the change persistent.
Step 4: Check Both Paths
Check the routing decisions for a destination on each path, the VPN server itself, and the optional printer:
ip -4 route get 203.0.113.10
ip -4 route get 198.51.100.80
ip -4 route get 198.51.100.20
ip -4 route get 192.168.50.25
sudo iptables -w -L OUTPUT -n -v --line-numbers
The selected VPN destination should use tun0 with source 10.8.0.2. The direct Internet destination and VPN server should use 192.168.50.1 through eth0, with source 192.168.50.10. The printer should use eth0 directly, without a gateway.
Those lookups don’t send test traffic or prove a connection works. Use real, permitted test destinations in place of the documentation addresses and make new application connections through both paths. Confirm the expected public exit addresses where the destination supports that check. A public-IP test only tests the route to that particular service; it must fall inside the selected prefix to test this VPN route.
Then test VPN failure and reconnection. If the VPN route disappears, a lookup may show the direct default route; the guard must block actual packets to the selected network. If the tunnel remains present but cannot carry traffic, connections may simply time out. Either way, those connections must not succeed through the direct path. Check firewall counters and capture traffic on the physical interface as needed to distinguish blocked attempts from actual direct leakage. The encrypted connection to the VPN server is expected on that interface.
After reconnection, check that the selected route is present again. A manually added route may need to be reapplied after the tunnel is recreated; don’t assume the VPN client restores it. Recheck the guard and both paths, then complete the connection tests below.
Step 5: Back Out of the Routing Change
To remove the route added in this exercise, first stop the test applications, then remove that specific route while keeping the guard in place:
sudo ip -4 route del 203.0.113.0/24 dev tun0
Only if you are deliberately abandoning the VPN-only requirement for these destinations, remove the exact guard too:
sudo iptables -w -D OUTPUT -d 203.0.113.0/24 ! -o tun0 -m comment --comment split-vpn-guard -j DROP
Removing that guard permits direct fallback under the remaining firewall policy. It is not a VPN-disconnection step. Check the resulting routes and rules before restarting applications.
Step 6: Plan Persistence After Testing
Once the running configuration passes the tests below, make the same policy persistent through the services that actually manage this host. There is no universal /etc/network/interfaces recipe for every Linux system.
Keep the firewall and route stages separate. The guard must be restored before networking and applications can send traffic. The selected route belongs to the VPN’s ready/reconnect stage, after the tunnel and its address exist. A tunnel-down event may remove the route, but must leave the guard in force. Do not put both operations into a disconnect cleanup script.
Use the split startup design and firewall-failure dependency described in Persistence: Making iptables Changes Stick as the broader pattern. A network-online target alone doesn’t establish that this VPN is ready. The exact route hook depends on the VPN client and network manager; it needs its own failure and reboot test before deployment.
Test the Intended Connections Before Everyday Use
A successful connection is only part of the test. For a privacy-oriented setup, you also need to establish which path the connection uses and what happens when that path is unavailable. Being able to reach a destination doesn’t, by itself, prove that the connection used the gateway you intended.
Write down the expected behavior before applying changes. For the VPN/direct-Internet example, use the following checks in a controlled test environment before relying on the configuration for everyday use.
| Test | Expected Result |
|---|---|
| Destinations assigned to the VPN | Traffic intended for the VPN uses its tunnel and expected public exit address. Check IPv4 and IPv6 separately; IPv6 must either follow the intended VPN path or be blocked if it is outside the supported design. |
| Destinations assigned to the direct connection | These connections use the regular Internet gateway and expected public exit address, with the VPN both connected and unavailable. They are deliberate exceptions to VPN routing. Check IPv4 and IPv6 according to the same policy. |
| Access to a LAN printer, if used | A test page reaches the intended printer over the trusted LAN, without using the VPN gateway. Test direct-address access and, if needed, local name resolution and printer discovery separately. |
| DNS resolution | DNS requests follow the resolver and path policy you chose; deliberately direct application traffic does not automatically define a separate DNS policy. Local name resolution works where intended. Check for unintended resolver fallback when the VPN fails. |
| VPN failure, disconnection, and reconnection | Internet traffic intended for the VPN is blocked while the tunnel is unavailable, including during reconnection. Deliberately direct Internet traffic and intended LAN connections remain available; VPN-designated traffic must not silently fall back to that direct path. Test an unexpected failure and a deliberate disconnect separately; client behavior may differ. |
| New administrative connection | A new SSH session succeeds through the intended management path. Keep the independent recovery console available throughout the test. |
Inspect the selected routes, routing rules, and firewall behavior as well as the application results. Where necessary, confirm the actual path with a packet capture on the relevant interfaces. A public-IP check is useful, but it does not establish how every application, DNS request, or IPv6 connection is handled. The VPN’s own encrypted transport must still reach its server outside the tunnel; that is different from VPN-designated application traffic escaping the VPN.
Keep the direct-Internet and LAN exceptions explicit. Successful direct traffic is an expected result when you chose that path; it is an unintended bypass when the traffic was supposed to stay inside the VPN.
Apply, Verify, and Then Test Persistence
Before applying the saved configuration, confirm which service manages these interfaces and keep your recovery console available. Use the application or reload procedure appropriate to that service. The commands above change the running configuration only; their persistent equivalents must match the firewall manager, network manager, and VPN client in use.
After applying the changes, repeat the connection and failure tests above. An existing SSH session alone doesn’t establish that new connections will work. Record the results before making the configuration part of everyday use.
Once the running configuration passes, test its persistent startup configuration with your recovery access available. Reboot, inspect the routes, routing rules, and firewall state before manually repairing anything, and repeat the same tests. If something fails, inspect the configuration and service logs before making further changes. Restore the working configuration when necessary.
For the broader testing and startup approach, see Testing Your iptables Rules and Persistence: Making iptables Changes Stick.
Technical References
-
Ubuntu: configuring networks — temporary versus persistent settings.
-
Netplan: testing and recovery caveats — confirm rollback and the configuration on disk before relying on a reboot.
-
OpenVPN manual — route redirection and local-network policy options; not a prescribed client version for this tutorial.
-
Mullvad app documentation — an example of local-network access, kill-switch, and disconnection behavior differing by setting.
-
iproute2: route management and routing-policy rules — destination routes, source addresses, route lookups, and table selection.
-
iptables and its extensions — rule placement, interface matching, comments, and rejection.
-
OpenVPN 2.6 manual — point-to-point versus subnet topology and route lifecycle; a topology reference, not a requirement to select that version.
-
RFC 4632 and RFC 5737 — CIDR prefixes and documentation addresses.