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
RHCS6: Luci - the cluster management console
Viewed 810 times since Sun, Jun 3, 2018
Viewed 524 times since Fri, Jun 8, 2018
ZPOOL: Verify/change properties of a zpool
Viewed 598 times since Sun, Jun 3, 2018
20 Linux YUM (Yellowdog Updater, Modified) Commands for Package Management YUM
Viewed 2240 times since Thu, Oct 25, 2018
Linux - How to monitor CPU usage
Viewed 528 times since Fri, Jun 8, 2018
ZPOOL: Grow a zpool by adding new device(s)
Viewed 632 times since Sun, Jun 3, 2018
LVM: Extend an existing Volume Group by adding a new disk
Viewed 624 times since Sat, Jun 2, 2018
Split and Reassemble files
Viewed 913 times since Mon, May 28, 2018
LVM: Recovering Physical Volume Metadata
Viewed 673 times since Sat, Jun 2, 2018
ZFS: Verify/change properties of a zfs filesystem
Viewed 636 times since Sun, Jun 3, 2018