Skip to content

Instantly share code, notes, and snippets.

@amrox
Created April 1, 2026 14:10
Show Gist options
  • Select an option

  • Save amrox/be8d84468dbb3630dd7cf0b19dceee3c to your computer and use it in GitHub Desktop.

Select an option

Save amrox/be8d84468dbb3630dd7cf0b19dceee3c to your computer and use it in GitHub Desktop.
# Tailscale Subnet Router over OpenVPN

Use a Linux machine as a Tailscale subnet router to give your tailnet devices access to networks behind an OpenVPN tunnel.

Scenario

[ Phone/Tablet ]
       |
   (tailnet)
       |
[ Linux box ] ---- OpenVPN (tun0) ----> [ Remote network 10.x.x.x ]

Your Linux machine is connected to a remote network via OpenVPN. You want other devices on your Tailscale network (phone, laptop, etc.) to reach hosts on that remote network — without installing a VPN client on each device.

Prerequisites

  • Linux machine with both Tailscale and OpenVPN running
  • IP forwarding enabled: sysctl -w net.ipv4.ip_forward=1 (and persisted in /etc/sysctl.conf)

Step 1: Advertise routes via Tailscale

tailscale up --advertise-routes=10.0.1.0/24

Adjust the CIDR to match whichever subnets you need to reach. Then approve the route in the Tailscale admin console.

Step 2: Add a MASQUERADE rule

This is the non-obvious part. Forwarding alone isn't enough.

When your phone sends a packet to 10.0.1.50, it arrives on tailscale0 with a source address of 100.x.x.x (your phone's Tailscale IP). The packet gets forwarded out tun0 to the remote network. But the remote network has no route back to 100.x.x.x, so the reply is silently dropped.

The fix is to MASQUERADE outbound traffic on tun0:

iptables -t nat -A POSTROUTING -o tun0 -j MASQUERADE

This rewrites the source IP to the Linux machine's tun0 address, so the remote side knows how to reply. The Linux box then de-NATs the return traffic and forwards it back through Tailscale.

Note: If you have both iptables-nft and iptables-legacy on your system, make sure you're adding the rule to whichever backend your other rules (Docker, Tailscale, etc.) are using. Check with iptables-nft -L FORWARD -v -n vs iptables-legacy -L FORWARD -v -n.

Step 3: Persist the rule

The MASQUERADE rule won't survive a reboot. If your OpenVPN connection is managed by NetworkManager, a dispatcher script is a clean way to tie the rule to the tunnel lifecycle:

/etc/NetworkManager/dispatcher.d/50-tailscale-masquerade

#!/bin/bash
INTERFACE="$1"
ACTION="$2"

if [ "$INTERFACE" != "tun0" ]; then
    exit 0
fi

case "$ACTION" in
    up)
        iptables -t nat -A POSTROUTING -o tun0 -j MASQUERADE
        ;;
    down)
        iptables -t nat -D POSTROUTING -o tun0 -j MASQUERADE
        ;;
esac
sudo chmod 755 /etc/NetworkManager/dispatcher.d/50-tailscale-masquerade

If you manage OpenVPN differently (systemd, command line), you can use the --up / --down script options in your .ovpn config, or just add the iptables rule to /etc/rc.local.

Troubleshooting

  • Check forwarding: cat /proc/sys/net/ipv4/ip_forward — must be 1
  • Check routes: ip route — make sure the target subnet routes through tun0
  • Check Tailscale: tailscale status — confirm your devices are online and routes are advertised
  • Check iptables FORWARD chain: Docker, Calico, Multipass, etc. all add FORWARD rules. Make sure traffic between tailscale0 and tun0 isn't being dropped. Look at the ts-forward chain if it exists.
  • Check NAT rule is active: iptables -t nat -L POSTROUTING -v -n — you should see the MASQUERADE rule on tun0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment