How To Set Up an SSL Tunnel Using Stunnel on Ubuntu

What’s Stunnel

The Stunnel program is designed to work as an SSL encryption wrapper between remote client and local (inetd-startable) or remote server. It can be used to add SSL functionality to commonly used inetd daemons like POP2, POP3, and IMAP servers without any changes in the program's code.

What Stunnel basically does is that it turns any insecure TCP port into a secure encrypted port using OpenSSL package for cryptography. It’s somehow like a small secure VPN that runs on specific ports.


Step 1: Create an Ubuntu Droplet

So far I have tested it on Ubuntu 12.04 x32/x64, Ubuntu 12.10 x32/x64, Ubuntu 13.04 x32/x64.


Step 2: Update and Upgrade Ubuntu

Using these commands update your Ubuntu’s package list and also upgrade the existing packages to the latest version:

apt-get update
apt-get upgrade

Step 3: Install Stunnel on your VPS

Install Stunnel package using the code below:

apt-get install stunnel4 -y

Step 4: Configure Stunnel on the VPS

Stunnel configures itself using a file named "stunnel.conf" which by default is located in "/etc/stunnel".

Create a "stunnel.conf" file in the "/etc/stunnel" directory:

nano /etc/stunnel/stunnel.conf

We’re going to be using a SSL certificate to identify ourselves to the server so we have to set the path to that certificate in "stunnel.conf" file using this line (We will create the certificate file in the next step):

cert = /etc/stunnel/stunnel.pem

Next we specify a service for use with Stunnel. It can be any of the services which use networking such as mail server, proxy server, etc.

Here as an example we’re going to secure traffics between Squid proxy server and a client using Stunnel. We'll explain how to install and configure Squid in Step 6.

After setting a name for the service you’re going to use, you must tell Stunnel to listen on which port for that service. This can be any of the 65535 ports, as long as it’s not blocked by another service or firewall:

accept = 8888

Then depending on the service you’re going to use the secure tunnel on, you must specify the port and IP address of that in the configuration file Basically Stunnel takes packets from a secure port and then forwards it to the port and IP address of the service you specified.

Squid proxy by default runs on localhost and port 3128 so we have to tell Stunnel to forward accepted connections to that port:
connect =

So overall the "stunnel.conf" file must contain the lines below:

client = no
accept = 8888
connect =
cert = /etc/stunnel/stunnel.pem

Note: The client = no part isn't necessary, Stunnel by default is set to server mode.


Step 5: Create SSL Certificates

Stunnel uses SSL certificate to secure its connections, which you can easily create using the OpenSSL package:

openssl genrsa -out key.pem 2048
openssl req -new -x509 -key key.pem -out cert.pem -days 1095
cat key.pem cert.pem >> /etc/stunnel/stunnel.pem

Basically, the commands above is for creating a private key, creating a certificate using that key and combining the two of them into one files named "stunnel.pem" to use with Stunnel.

Note: When creating the certificate, you will be asked for some information such as country and state, which you can enter whatever you like but when asked for "Common Name" you must enter the correct host name or IP address of your droplet (VPS).

Also, enable Stunnel automatic startup by configuring the "/etc/default/stunnel4" file, enter command below to open the file in text editor:

nano /etc/default/stunnel4

And change ENABLED to 1:


Finally, restart Stunnel for configuration to take effect, using this command:

/etc/init.d/stunnel4 restart

Step 6: Install Squid Proxy

Install Squid using the command below:

apt-get install squid3 -y

Step 7: Configure Stunnel in Client

Note: This explains the process of installing and configuration of Stunnel as a client in Windows, but Stunnel could also be installed in Linux and even Android and configuration still remains the same. The only difference would be placement of "stunnel.conf" file required for configuration of Stunnel.

In order for Stunnel to communicate with the server, the SSL certificate we created in Step 5 must be present at the client. There are many ways of obtaining the "stunnel.pem" file from server, but we’re going to use SFTP which is both easy and very secure.

Using a SFTP client such as Filezilla, connect to your server and download the "stunnel.pem" file located in "/etc/stunnel/" directory to the client.

There’s also a good tutorial on SFTP here:

How To Use SFTP to Securely Transfer Files with a Remote Server

Download Stunnel from their website.

Install Stunnel in any place you like. Then go to the Stunnel folder and move the downloaded certificate "stunnel.pem" to Stunnel folder.

Create a "stunnel.conf" file in the Stunnel’s folder if one does not exist. Open the file with a text editor such as Notepad.

First of all, we tell Stunnel our certificate’s path, which in Windows is in the Stunnel’s directory (reminder: in Ubuntu it is in "/etc/stunnel/" directory):

cert = stunnel.pem

Since we are going to set up a client, we have to tell Stunnel that this is a client. Put the line below in the configuration file:

client = yes

Then just like the server, we must specify configuration of the service we want to use.

First we specify the service’s name, then the IP address and port, which Stunnel should listen to on the client:

accept =

The accept port could be any port on the client computer, as long as it’s not occupied by another service or blocked by a firewall.

Next, we tell Stunnel to forward packets coming to this port to our Stunnel server’s IP address and port. The IP address is your server’s (droplet) public IP address, which is assigned to you when setting up a droplet, and port is the port you specified when configuring Stunnel in the server. In our case it was 8888 so we’re going to tell Stunnel to connect to that port:

connect = [Server’s Public IP]:8888

So the final "stunnel.conf" file in the client should look like this:

cert = stunnel.pem
client = yes
accept =
connect = [Server’s Public IP]:8888

Save and close the file and run "stunnel.exe".

That’s it. Now our client is configured to communicate securely with the virtual server using a secure SSL tunnel. From now on when trying to connect to any service on our VPS, instead of connecting directly to IP address of server, we must use the IP address and port specified in the Stunnel’s "accept" part of configuration for each service.

As an example, when we want to connect to Squid proxy on our cloud server, we must configure our client to connect to, and Stunnel automatically connects us through a secure tunnel to the service specified for that port. Here you can configure your web browser to use IP and port 8080 as a proxy to secure your web traffic.

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
ZFS: Grow/Shrink an existing zfs filesystem
Viewed 5984 times since Sun, Jun 3, 2018
List DNS records, nameservers of a domain from command line
Viewed 1733 times since Sun, Sep 30, 2018
RHEL: Services basic management - systemd
Viewed 18258 times since Sat, Jun 2, 2018
YUM How to use yum command on CentOS/RHEL
Viewed 6707 times since Thu, Oct 25, 2018
How to Synchronize Directories Using Lsyncd in Linux
Viewed 13221 times since Wed, Oct 31, 2018
OpenSSL: Check SSL Certificate Expiration Date and More
Viewed 6349 times since Mon, Feb 18, 2019
Check Detailed CPU Information In Linux With CoreFreq [Advanced]
Viewed 2077 times since Thu, Apr 18, 2019
RHEL : How to deal with “CLOSE_WAIT” and “TIME_WAIT” connection
Viewed 25162 times since Thu, Feb 14, 2019
Jak wygenerować silne hasła jednorazowe w Linuksie?
Viewed 2098 times since Thu, May 24, 2018
sed Delete / Remove ^M Carriage Return (Line Feed / CRLF) on Linux or Unix
Viewed 9864 times since Thu, Feb 7, 2019