Setting up encrypted tunnel using stunnel

Setting up encrypted tunnel using stunnel

To implement encrypted communication between Redis masters and slaves, we recommend using stunnel. Stunnel works as TLS encryption wrapper between client and server.

This step-by-step tutorial will explain how to install and configure stunnel proxies on FreeBSD client and server. Configuration procedures for other operating systems are very similar. For simplicity, this tutorial only covers replication to one client host as this configuration does not require individual preshared keys for each of the clients.

Assuming we have 3 Redis instances on both server and client, listening sockets on the server (master side):

instanceRedis socketstunnel socket
redis localhost:6379 -
bayes localhost:6378
fuzzy localhost:6377

As the instance named redis should not be mirrored, we will replicate fuzzy and bayes instances. So we need to setup 2 TLS tunnels.


First install the security/stunnel package:

# pkg install stunnel

Create pid-file directory:

# mkdir /var/run/stunnel && chown stunnel:stunnel /var/run/stunnel

To enable stunnel add the following lines to the /etc/rc.conf:


Server configuration (master side)


setuid = stunnel
setgid = nogroup

pid = /var/run/stunnel/

accept  = 6478
connect = 6378
ciphers = PSK
PSKsecrets = /usr/local/etc/stunnel/psk.txt

accept  = 6477
connect = 6377
ciphers = PSK
PSKsecrets = /usr/local/etc/stunnel/psk.txt

Client configuration (slave side)


setuid = stunnel
setgid = nogroup

pid = /var/run/stunnel/

client = yes
accept  = localhost:6478
connect =
ciphers = PSK
PSKsecrets = /usr/local/etc/stunnel/psk.txt

client = yes
accept  = localhost:6477
connect =
ciphers = PSK
PSKsecrets = /usr/local/etc/stunnel/psk.txt

Preshared keys

Create /usr/local/etc/stunnel/psk.txt . The psk.txt file contains one line for each client:


Do not use example passwords.

As both bayes and fuzzy Redis instances located at the same host we can share the same key between them.

Since this file should be kept secret set secure permissions on it:

# chmod 600 /usr/local/etc/stunnel/psk.txt

Starting stunnel

# service stunnel start


From the client host use the redis-cli utility to connect to the remote instances:

0 (0)
Article Rating (No Votes)
Rate this article
There are no attachments for this article.
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
linux ssh How to Hide the OpenSSH Version Details when Telnet to Port 22
Viewed 134 times since Wed, Apr 22, 2020
Linux / UNIX: Run Command a Number of Times In a Row
Viewed 642 times since Tue, Aug 6, 2019
20 Practical Examples of RPM Commands in Linux rpm
Viewed 973 times since Mon, Feb 18, 2019
Top 10 darmowych i publicznych serwerów DNS
Viewed 819 times since Tue, May 22, 2018
logrotate Understanding logrotate utility
Viewed 254 times since Sun, Jan 12, 2020
RHEL: udev rules basics
Viewed 1006 times since Sat, Jun 2, 2018
Tip: SSD and Linux. Enable TRIM and check if it works
Viewed 103 times since Fri, May 15, 2020
Using stunnel to Encrypt Database Connections
Viewed 993 times since Fri, Sep 28, 2018
10 Linux DIG Command Examples for DNS Lookup
Viewed 394 times since Sun, Sep 30, 2018
RHEL: Services basic management - systemd
Viewed 9247 times since Sat, Jun 2, 2018