Since 2016 · 50,000+ customers · ★ 5.0 on Google Live chatWhatsAppTelegramOpen a ticket
Get started
Linux

How to Set Up stunnel on Ubuntu (TLS Tunnel, Users and PSK Keys)

How to set up stunnel on Ubuntu guide by 99RDP

stunnel adds TLS encryption to services that don't have it, such as an old mail server, Redis, or a custom TCP application. It accepts encrypted connections and forwards them to the plain service (server mode), or encrypts a local plain connection before sending it out (client mode). This guide installs stunnel on Ubuntu, sets up a server and a client, runs it with systemd, and manages per-user access with pre-shared keys.

Quick answer

sudo apt install stunnel4
sudo nano /etc/stunnel/redis.conf          # your tunnel config, with foreground = yes
sudo systemctl enable --now stunnel@redis   # runs /etc/stunnel/redis.conf
systemctl status stunnel@redis
stunnel server configuration file on Ubuntu wrapping Redis in TLS on port 6380
A stunnel server configuration

How stunnel works

  • Server mode: listens on a TLS port (for example 6380) and forwards decrypted traffic to the local service (for example Redis on 127.0.0.1:6379).
  • Client mode: listens on a local plain port and sends the traffic encrypted to a remote stunnel server.

With both ends, two plain-text programs can talk securely across the internet.

stunnel: the client app talks to local stunnel, which wraps it in TLS to the stunnel server in front of Redis
How stunnel wraps traffic

Step 1: Install stunnel

sudo apt update
sudo apt install stunnel4
stunnel -version

The package is called stunnel4; configuration files go in /etc/stunnel/.

Step 2: Create a certificate (server side)

For internal use, a self-signed certificate is fine. For public services, use a certificate from Let's Encrypt or your CA.

sudo openssl req -new -x509 -days 825 -nodes \
  -subj "/CN=tunnel.example.com" \
  -out /etc/stunnel/stunnel.pem -keyout /etc/stunnel/stunnel.pem
sudo chmod 600 /etc/stunnel/stunnel.pem

Step 3: Configure the server

Create /etc/stunnel/redis.conf:

foreground = yes
setuid = stunnel4
setgid = stunnel4

[redis-tls]
accept = 0.0.0.0:6380
connect = 127.0.0.1:6379
cert = /etc/stunnel/stunnel.pem

foreground = yes is required when stunnel runs under the per-config systemd service used below.

Step 4: Run it with systemd

Ubuntu's package includes a template service, stunnel@.service, that runs one config file per instance: stunnel@redis runs /etc/stunnel/redis.conf.

sudo systemctl enable --now stunnel@redis
systemctl status stunnel@redis
sudo ss -tlnp | grep 6380
sudo ufw allow 6380/tcp

Don't run the older all-in-one stunnel4 service for the same config at the same time.

Step 5: Configure a client

On the client machine, create /etc/stunnel/redis-client.conf:

foreground = yes

[redis-client]
client = yes
accept = 127.0.0.1:6379
connect = tunnel.example.com:6380
verifyChain = yes
CAfile = /etc/stunnel/server-ca.pem
checkHost = tunnel.example.com

Copy the server's certificate (or your CA) to /etc/stunnel/server-ca.pem on the client, then sudo systemctl enable --now stunnel@redis-client. Applications on the client connect to 127.0.0.1:6379 as usual, and stunnel encrypts the traffic.

Managing users with pre-shared keys (PSK)

Instead of certificates, stunnel can authenticate each client with its own key. On the server, create /etc/stunnel/psk.txt with one line per user, in the form identity:key (the key must be at least 16 characters):

alice:Q6m2Hk9vX4pT8rLz3WfN7bYd
bob:R7nU2cE5sJ9wK3qM8hV4xGt6
sudo chmod 600 /etc/stunnel/psk.txt

In the server's service section, replace cert with PSKsecrets = /etc/stunnel/psk.txt. Each client uses PSKsecrets with only its own line. To revoke a user, delete their line and restart the service: sudo systemctl restart stunnel@redis.

Generate strong keys with openssl rand -base64 24.

stunnel psk.txt with one identity per user, 600 permissions and the keys masked
Per-user pre-shared keys for stunnel

Logs and troubleshooting

journalctl -u stunnel@redis -f
  • Service starts and immediately stops: add foreground = yes to the config.
  • Certificate verify failed on the client: the CA file or checkHost doesn't match the server's certificate.
  • Connection refused: the backend service isn't listening on the connect address, or the firewall blocks the accept port.

Frequently asked questions

What is stunnel used for?

Adding TLS encryption to TCP services that don't support it, without changing the service itself.

How do I start stunnel on Ubuntu?

Put your config in /etc/stunnel/NAME.conf with foreground = yes, then run sudo systemctl enable --now stunnel@NAME.

How do I add users to stunnel?

Use PSK authentication: add an identity:key line per user to the PSKsecrets file and restart the service. Delete the line to revoke access.

Is stunnel the same as an SSH tunnel?

Both encrypt traffic, but stunnel uses TLS and runs as a service, while an SSH tunnel uses an SSH login. stunnel is better for permanent, unattended tunnels.

Nishant Kumar, Sr. Server Engineer at 99RDP
Written byNishant KumarSr. Server Engineer, 99RDP. Part of the team that runs our servers and answers support.
Keep reading

More Linux guides

All Linux guides

Need a server for this?

✓ Setup in minutes✓ Full admin access✓ 7-day refund on new services