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
How to Install and use Lsyncd on CentOS 7 / RHEL 7 rsync
Viewed 1801 times since Wed, Oct 31, 2018
LVM: Create a new Logical Volume / Filesystem
Viewed 1043 times since Sat, Jun 2, 2018
Improve security with polyinstantiation
Viewed 553 times since Fri, May 15, 2020
RHCS6: Reduce a Global Filesystem 2 (GFS2)
Viewed 841 times since Sun, Jun 3, 2018
Linux - How to monitor memory usage
Viewed 857 times since Fri, Jun 8, 2018
How to Clear RAM Memory Cache, Buffer and Swap Space on Linux
Viewed 371 times since Mon, Nov 23, 2020
RHEL: Remove existing SAN LUNs
Viewed 1257 times since Sat, Jun 2, 2018
RHEL7 slow ssh login
Viewed 1703 times since Tue, Aug 6, 2019
HowTo: Send Email from an SMTP Server using the Command Line
Viewed 707 times since Mon, Feb 18, 2019
CentOS / RHEL : How to move a Volume Group from one system to another
Viewed 1118 times since Mon, Jan 28, 2019