Step-by-step guide on how to set up OpenVPN over Stunnel on Ubuntu 22.04
openssl req -new -x509 -days 365 -nodes
: Generates a new X.509 certificate valid for 365 days without a passphrase (-nodes
).-out stunnel.pem -keyout stunnel.pem
: Outputs both the certificate and the private key to stunnel.pem
.output
: Specifies where to write log output.[openvpn]
: Defines a service named “openvpn”.client = no
: Runs Stunnel in server mode.accept = 443
: Listens on port 443 (common HTTPS port, often allowed through firewalls).connect = 127.0.0.1:1194
: Forwards incoming connections to the local OpenVPN service.cert
: Specifies the SSL certificate file.enable
: Ensures Stunnel starts on system boot.restart
: Starts or restarts the Stunnel service.status
: Checks if Stunnel is running correctly.local
directive:
local 0.0.0.0
ensures OpenVPN listens on all network interfaces.
client = yes
: Runs Stunnel in client mode.accept = 127.0.0.1:1194
: Listens locally on port 1194.connect = VPS_IP_ADDRESS:443
: Connects to the VPS server’s Stunnel service.VPS_IP_ADDRESS
with your actual VPS server IP.
client.ovpn
to your home directory.
Note: Replace VPS_IP_ADDRESS
with your VPS IP.
remote
directive to point to the local Stunnel endpoint:remote 127.0.0.1 1194
: Connects OpenVPN to the local Stunnel service.route-nopull
: Prevents automatic route changes.script-security 2
: Allows the execution of scripts.route-up
: Executes a script after connection is established.SERVER_IP
: Your VPS server IP.GATEWAY
: Determines the default gateway to route traffic to the VPS server outside the VPN tunnel.ip route add
commands adjust the routing table.VPS_IP_ADDRESS
with your VPS IP.
Make the script executable:
sudo systemctl status stunnel4
.routing.sh
script has the correct server IP.journalctl -xe
or specific service logs.