how to do multihop vpn using openvpn on linux.
- User
how to do multihop vpn using openvpn on linux.
Assistant
how to do multihop vpn using openvpn on linux.
Setting up a multi-hop OpenVPN configuration on Linux involves chaining multiple OpenVPN servers together. This provides an additional layer of anonymity and security by making it harder to trace your traffic back to your origin.
Here's how a two-hop setup would typically work:
Diagram:
[Client PC]
| (OpenVPN Client)
| Encrypted Tunnel 1
V
[Server A] (Entry Node)
| (OpenVPN Server for Client PC)
| (OpenVPN Client for Server B)
| Encrypted Tunnel 2
V
[Server B] (Exit Node)
| (OpenVPN Server for Server A)
| (NAT to Internet)
V
[Internet]
Assumptions:
Client PC, Server A, Server B.openvpn and easy-rsa installed.Server A and Server B have public IP addresses (or are reachable by each other).tun devices for routing.Step 0: Prerequisites on ALL Machines
sudo apt update && sudo apt upgrade -y # Debian/Ubuntu
sudo dnf update -y # Fedora/RHEL/CentOS
sudo apt install openvpn easy-rsa -y # Debian/Ubuntu
sudo dnf install openvpn easy-rsa -y # Fedora/RHEL/CentOS
sudo sysctl -w net.ipv4.ip_forward=1
/etc/sysctl.conf and uncomment/add:
net.ipv4.ip_forward=1
sudo sysctl -p.Step 1: Certificate Authority (CA) and Key Generation (On a secure machine, e.g., your local PC or a dedicated CA server)
We'll generate all certificates and keys from one central location and then securely copy them to the respective machines.
# Create a directory for easy-rsa
mkdir -p ~/openvpn-ca
cd ~/openvpn-ca
# Make a copy of the easy-rsa installation
cp -r /usr/share/easy-rsa/ .
cd easy-rsa/
# Initialize the PKI
./easyrsa init-pki
# Build the CA (Common Name can be anything, e.g., "MyVPN CA")
./easyrsa build-ca nopass
# Generate Diffie-Hellman parameters (this can take a while)
./easyrsa gen-dh
# Generate the TLS Auth key (for HMAC authentication)
openvpn --genkey --secret pki/ta.key
# Generate server certificates/keys
# Server B (Exit Node) will be the "server" for the Server A client
./easyrsa gen-req server_B_exit_node nopass
./easyrsa sign-req server server_B_exit_node
# Server A (Entry Node) will be the "server" for the Client PC
./easyrsa gen-req server_A_entry_node nopass
./easyrsa sign-req server server_A_entry_node
# Generate client certificates/keys
# Server A will act as a client to Server B
./easyrsa gen-req client_A_to_B nopass
./easyrsa sign-req client client_A_to_B
# Your Client PC will act as a client to Server A
./easyrsa gen-req client_PC_to_A nopass
./easyrsa sign-req client client_PC_to_A
Distribute Keys Securely:
Now, copy the necessary files to their respective machines. Replace SERVER_A_IP, SERVER_B_IP, CLIENT_PC_IP with actual IPs. Use scp or a similar secure method.
Files to Distribute:
Server B (Exit Node):
pki/ca.crtpki/issued/server_B_exit_node.crtpki/private/server_B_exit_node.keypki/dh.pempki/ta.keyServer A (Entry Node):
pki/ca.crtpki/issued/server_A_entry_node.crtpki/private/server_A_entry_node.keypki/issued/client_A_to_B.crtpki/private/client_A_to_B.keypki/dh.pempki/ta.keyClient PC:
pki/ca.crtpki/issued/client_PC_to_A.crtpki/private/client_PC_to_A.keypki/ta.keyRecommended location on target machines: /etc/openvpn/ for server files, /etc/openvpn/client/ or ~/ for client files. Create these directories if they don't exist.
Step 2: Server B (Exit Node) Setup
This server will accept connections from Server A and route them to the Internet.
Place Key Files:
Copy the files for Server B to /etc/openvpn/ on Server B.
ca.crtserver_B_exit_node.crt (rename to server.crt)server_B_exit_node.key (rename to server.key)dh.pemta.keyCreate server.conf for Server B:
Create /etc/openvpn/server.conf with the following content.
10.9.0.0/24port 1194
proto udp
dev tun
ca ca.crt
cert server.crt
key server.key
dh dh.pem
tls-auth ta.key 0 # This is the server side of tls-auth
server 10.9.0.0 255.255.255.0 # VPN network for Server A -> Server B
push "redirect-gateway def1 bypass-dhcp" # Crucial for internet access
push "dhcp-option DNS 8.8.8.8" # Google DNS (or your preferred DNS)
push "dhcp-option DNS 8.8.4.4"
keepalive 10 120
cipher AES-256-CBC
auth SHA256
user nobody
group nogroup
persist-key
persist-tun
status /var/log/openvpn/openvpn-status.log
log /var/log/openvpn/openvpn.log
verb 3
explicit-exit-notify 1
Firewall (iptables) on Server B:
You need to allow OpenVPN traffic and enable NAT.
Replace eth0 with your actual public network interface on Server B (e.g., ens1, enp0s3).
# Allow OpenVPN port
sudo iptables -A INPUT -i eth0 -p udp --dport 1194 -j ACCEPT
# Allow traffic from VPN to Internet
sudo iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -s 10.9.0.0/24 -o eth0 -j ACCEPT
# Enable NAT (Masquerading)
sudo iptables -t nat -A POSTROUTING -s 10.9.0.0/24 -o eth0 -j MASQUERADE
Step 3: Server A (Entry Node) Setup
This server acts as an OpenVPN server for your Client PC and an OpenVPN client for Server B.
Place Key Files:
ca.crt, server_A_entry_node.crt (rename to server_A.crt), server_A_entry_node.key (rename to server_A.key), dh.pem, ta.key to /etc/openvpn/server/.ca.crt, client_A_to_B.crt (rename to client_A.crt), client_A_to_B.key (rename to client_A.key), ta.key to /etc/openvpn/client/.Create server.conf for Server A:
Create /etc/openvpn/server/server.conf.
10.8.0.0/24port 1194
proto udp
dev tun0 # Use tun0 for the server connection
ca /etc/openvpn/server/ca.crt
cert /etc/openvpn/server/server_A.crt
key /etc/openvpn/server/server_A.key
dh /etc/openvpn/server/dh.pem
tls-auth /etc/openvpn/server/ta.key 0
server 10.8.0.0 255.255.255.0 # VPN network for Client PC -> Server A
# DO NOT push "redirect-gateway" here. Server A routes traffic to Server B.
# Push the route to Server B's VPN network so clients know how to reach it
# No, actually, the client will send *all* traffic to Server A anyway due to redirect-gateway on client.
# So no special routes needed here for the client.
keepalive 10 120
cipher AES-256-CBC
auth SHA256
user nobody
group nogroup
persist-key
persist-tun
status /var/log/openvpn/server_A_status.log
log /var/log/openvpn/server_A.log
verb 3
Create client.conf for Server A (to connect to Server B):
Create /etc/openvpn/client/client.conf.
Step 4: Client PC Setup
Your personal computer will connect to Server A.
Place Key Files:
Copy ca.crt, client_PC_to_A.crt (rename to client.crt), client_PC_to_A.key (rename to client.key), ta.key to /etc/openvpn/client/ (or a more user-friendly location like ~/.openvpn/).
Create client.conf for Client PC:
Create /etc/openvpn/client/client.conf.
SERVER_A_IP with the actual public IP of Server A.client
dev tun
proto udp
remote SERVER_A_IP 1194 # Connect to Server A's public IP
ca client/ca.crt # Adjust path if stored elsewhere
cert client/client.crt
key client/client.key
tls-auth client/ta.key 1
resolv-retry infinite
nobind
persist-key
persist-tun
cipher AES-256-CBC
auth SHA256
remote-cert-tls server # Ensure you're connecting to a server cert
# This is crucial: pushes all traffic through the VPN
redirect-gateway def1 bypass-dhcp
# DNS servers pushed from Server B (via Server A) should ideally work,
# but you can explicitly set them here if needed.
# dhcp-option DNS 8.8.8.8
# dhcp-option DNS 8.8.4.4
verb 3
Start OpenVPN on Client PC:
sudo systemctl start openvpn@client # Or openvpn /path/to/client.conf
sudo systemctl enable openvpn@client
sudo systemctl status openvpn@client
If you're managing it manually:
sudo openvpn --config /etc/openvpn/client/client.conf
Testing and Verification
On Client PC:
whatismyip.com. It should show the public IP address of Server B (Exit Node).traceroute google.com
On Server A:
ip a to see tun0 (from client PC) and tun1 (to Server B).sudo tail -f /var/log/openvpn/server_A.log to see client connections.sudo tail -f /var/log/openvpn/client_A.log (if you named the client log like this) to see connection to Server B.On Server B:
ip a to see tun0 (from Server A).sudo tail -f /var/log/openvpn/openvpn.log to see connections from Server A.Important Considerations:
AES-256-CBC, SHA256).tls-auth (HMAC firewall) is crucial for preventing DoS attacks and UDP flooding.user nobody and group nogroup.10.8.0.0/24, 10.9.0.0/24) don't conflict with your home network or the actual public IP addresses of your servers.push "dhcp-option DNS..." command helps with this. You can verify at dnsleaktest.com./var/log/openvpn/) if something isn't working. The verb 3 setting provides a good level of detail.systemctl enable) so they start automatically on boot. For multiple instances on one machine (like Server A), use the @ syntax (openvpn@server, openvpn@client) or create separate service files.This comprehensive guide should get your multi-hop OpenVPN setup running. Remember to adjust IP addresses, interface names, and paths according to your specific environment.
Save these iptables rules so they persist after reboot:
sudo apt install iptables-persistent -y
sudo netfilter-persistent save
firewalld, you'll need to configure it:
sudo firewall-cmd --permanent --add-port=1194/udp
sudo firewall-cmd --permanent --add-masquerade
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.9.0.0/24" masquerade'
sudo firewall-cmd --reload
firewalld disabled (not recommended for beginners):
sudo systemctl disable firewalld --now
sudo dnf install iptables-services -y
sudo systemctl enable iptables --now
sudo iptables-save | sudo tee /etc/sysconfig/iptables
Start OpenVPN on Server B:
sudo mkdir -p /var/log/openvpn/
sudo systemctl start openvpn@server
sudo systemctl enable openvpn@server
sudo systemctl status openvpn@server
SERVER_B_IPclient
dev tun1 # Use tun1 for the client connection (distinct from tun0)
proto udp
remote SERVER_B_IP 1194
ca /etc/openvpn/client/ca.crt
cert /etc/openvpn/client/client_A.crt
key /etc/openvpn/client/client_A.key
tls-auth /etc/openvpn/client/ta.key 1 # This is the client side of tls-auth
resolv-retry infinite
nobind
persist-key
persist-tun
cipher AES-256-CBC
auth SHA256
user nobody
group nogroup
verb 3
Firewall (iptables) on Server A:
This server only needs to forward traffic between its two tun interfaces and allow incoming OpenVPN connections. No NAT is needed here.
Replace eth0 with your actual public network interface on Server A.
# Allow OpenVPN port from Client PC
sudo iptables -A INPUT -i eth0 -p udp --dport 1194 -j ACCEPT
# Allow forwarding between tun0 (server) and tun1 (client)
sudo iptables -A FORWARD -i tun0 -o tun1 -j ACCEPT
sudo iptables -A FORWARD -i tun1 -o tun0 -j ACCEPT
# Allow established/related connections
sudo iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
Save these iptables rules (refer to Step 2 for instructions based on your distro).
Start OpenVPN on Server A: Start both the server and client instances.
sudo mkdir -p /var/log/openvpn/
sudo systemctl start openvpn@server # for the server instance
sudo systemctl start openvpn@client # for the client instance
sudo systemctl enable openvpn@server
sudo systemctl enable openvpn@client
sudo systemctl status openvpn@server
sudo systemctl status openvpn@client