Below is a practical “recipe” for building a two-hop (cascade) VPN chain with OpenVPN on Linux.
• Hop 1 = you → VPN-A
• Hop 2 = VPN-A → VPN-B → Internet
All of your applications will exit through Hop 2, while Hop 2 itself is reached inside Hop 1’s tunnel. The same pattern can be extended to 3-, 4-, … hops, but start with two so you clearly see what is going on.
────────────────────────────────────────────────────────
- Prerequisites
────────────────────────────────────────────────────────
• Any recent Linux distro (systemd is assumed, but not required).
• openvpn (≥ 2.4) installed:
sudo apt install openvpn # Debian/Ubuntu
sudo dnf install openvpn # Fedora/RHEL
• Two working .ovpn files from your provider, e.g.
/etc/openvpn/client/vpn-a.ovpn
/etc/openvpn/client/vpn-b.ovpn
Test that each one can connect by itself first:
sudo openvpn --config vpn-a.ovpn
• Root privileges (for ip tables/routing).
────────────────────────────────────────────────────────
2. High-level idea
────────────────────────────────────────────────────────
- Bring up VPN-A (creates tun0).
- Add a static route for VPN-B’s server-IP so traffic to it is forced through tun0.
- Bring up VPN-B with route-nopull so it does not immediately hijack the default route.
- When tun1 is up, manually change the default route to go via tun1.
- Optionally lock everything down with iptables.
Graphically:
┌───────────┐ tun0 ┌────────────┐ tun1 ┌────────────┐
│ Laptop ├────────────▶│ VPN-A GW ├─────────▶│ VPN-B GW │──▶ Internet
└───────────┘ └────────────┘ └────────────┘
────────────────────────────────────────────────────────
3. Minimal edits to the two .ovpn files
────────────────────────────────────────────────────────
vpn-a.ovpn (first hop) – leave as provided.
vpn-b.ovpn (second hop) – add a couple of lines so it plays nice inside a chain:
--- start additions/overrides ---
; Avoid provider’s own redir-gw push
pull-filter ignore "redirect-gateway"
; We will add routes ourselves
route-nopull
Bind to a second tunnel name so it doesn’t reuse tun0
dev tun1
--- end additions ---
IMPORTANT: Figure out the real public IP of VPN-B’s server (e.g. 198.51.100.23).
OpenVPN normally resolves DNS itself, but we can’t rely on that once hop1 kills clearnet. Use dig once beforehand:
dig +short vpn-b.example.com
198.51.100.23
Put that IP into the helper script below.
────────────────────────────────────────────────────────
4. Helper script: ovpn-multihop.sh
────────────────────────────────────────────────────────
Make it executable: chmod +x /usr/local/bin/ovpn-multihop.sh
#!/usr/bin/env bash
#
# ovpn-multihop.sh – Bring up a 2-hop OpenVPN chain
#
# Hop1 → tun0
# Hop2 → tun1
#
# ———— config ————
HOP1_CFG="/etc/openvpn/client/vpn-a.ovpn"
HOP2_CFG="/etc/openvpn/client/vpn-b.ovpn"
HOP2_SERVER_IP="198.51.100.23" # ← real IPv4 of VPN-B server
# If your provider has IPv6 servers, adjust accordingly.
# Where to log/PID
LOGDIR="/var/log/openvpn-multihop"
mkdir -p "$LOGDIR"
# ———— helper ————
wait_for_if () {
IF=$1
printf "Waiting for %s to come up …" "$IF"
for i in {1..20}; do
ip link show "$IF" &>/dev/null && { echo " done."; return 0; }
sleep 1
done
echo
echo "ERROR: $IF did not appear!" >&2
exit 1
}
# ———— 1. start hop-1 ————
echo "[*] Starting Hop-1 (vpn-a) …"
openvpn --daemon \
--config "$HOP1_CFG" \
--writepid "$LOGDIR/hop1.pid" \
--log "$LOGDIR/hop1.log"
wait_for_if tun0
# Determine hop-1 gateway (point inside provider’s net)
HOP1_GW=$(ip route show dev tun0 default | awk '{print $3}')
echo "[*] Hop-1 gateway is $HOP1_GW"
# ———— 2. route VPN-B server via tun0 ————
echo "[*] Adding static route for VPN-B server ($HOP2_SERVER_IP) via tun0"
ip route replace "$HOP2_SERVER_IP" via "$HOP1_GW" dev tun0
# ———— 3. start hop-2 ————
echo "[*] Starting Hop-2 (vpn-b) …"
openvpn --daemon \
--config "$HOP2_CFG" \
--writepid "$LOGDIR/hop2.pid" \
--log "$LOGDIR/hop2.log"
wait_for_if tun1
# Grab hop-2 gateway
HOP2_GW=$(ip route show dev tun1 default | awk '{print $3}')
echo "[*] Hop-2 gateway is $HOP2_GW"
# ———— 4. Switch default route to tun1 ————
echo "[*] Routing ALL traffic through hop-2"
ip route del default 2>/dev/null || true
ip route add default via "$HOP2_GW" dev tun1
echo
echo "✅ Two-hop VPN chain established!"
ip -br a show tun0 tun1
ip route show | grep -E 'default|198\.51\.100\.23'
echo
echo "Use 'curl ifconfig.me' to confirm exit IP."
Run it as root (or via sudo):
sudo ovpn-multihop.sh
────────────────────────────────────────────────────────
5. Tear-down
────────────────────────────────────────────────────────
sudo kill "$(cat /var/log/openvpn-multihop/hop2.pid)"
sudo kill "$(cat /var/log/openvpn-multihop/hop1.pid)"
sudo ip route del 198.51.100.23 2>/dev/null || true
sudo ip route del default 2>/dev/null || true
# Re-enable your normal default gw (DHCP will usually restore it,
# or use `dhclient -r && dhclient IFACE`).
────────────────────────────────────────────────────────
6. Lock it down (optional but recommended)
────────────────────────────────────────────────────────
Add a basic killswitch so nothing leaks outside either tunnel:
iptables -P OUTPUT DROP
iptables -A OUTPUT -o lo -j ACCEPT
iptables -A OUTPUT -o tun0 -j ACCEPT
iptables -A OUTPUT -o tun1 -j ACCEPT
# If you still need DNS over hop-1 before hop-2 starts:
iptables -A OUTPUT -d "$HOP2_SERVER_IP" -o tun0 -j ACCEPT
Persist with iptables-save.
────────────────────────────────────────────────────────
7. Going beyond two hops
────────────────────────────────────────────────────────
• Repeat the exact same pattern:
‑ Make ovpn-C use dev tun2 + route-nopull
‑ Route its server IP through hop-2 (via tun1)
‑ After tun2 is up, switch default route to tun2
• Complexity grows fast; scripting is your friend.
• Test latency/throughput—three or more hops can get sluggish.
────────────────────────────────────────────────────────
8. Troubleshooting tips
────────────────────────────────────────────────────────
ip route get <IP> – verifies which interface/gw the kernel will pick.
tcpdump -ni tun0 / tun1 – see that hop-2 TLS handshake is really inside hop-1.
- If hop-2 never connects, likely the static route is missing or wrong IP used.
────────────────────────────────────────────────────────
🎉 That’s it! You now have an automatically routed OpenVPN multihop on Linux. Feel free to adapt the script (systemd units, WireGuard, more hops), and happy tunneling!