Set Up SSH Tunneling on a Linux / Unix / BSD Server To Bypass NAT

I‘m a new Linux / Unix system user. How can I set encrypted tunnel between my desktop/laptop computer and server in a remote data center to bypass the limits in a network? How do I create a reverse SSH tunnel on Unix-like systems?

SSH tunnelling can be thought as a poor-man’s-VPN. It is handy in situations where you would like to hide your traffic from any body who might be listening on the wire or eavsdropping.[donotprint]
Tutorial details
Difficulty level Advanced
Root privileges Yes/No
Requirements OpenSSHD server
Est. reading time 10m
[/donotprint] You can use such tunnel between your computer and your Unix/BSD/Linux server to bypass limits placed by a network or to bypass NAT, and more.

More about the Internet protocol, ports, tcp and udp

The Internet protocol is nothing but a set of rules for sending information between your desktop and the server on the Internet (or WAN or Lan). Each computer at least has one IP address. Each rule is like a language and in computer science you call it as a protocol. One can use protocols for communication over the Internet.

Common application protocol

For example, you can use HTTP (Hypertext Transfer Protocol) or HTTPS (Hypertext Transfer Protocol Secure) protocol to view images or download files from the Internet. You can use DNS (Domain Name System) protocol translates domain names such as, which can be easily memorized by humans, to the numerical IP addresses such as and vice versa. You can use ssh (Secure Shell) for secure data communication, remote command-line login, remote command execution, and more.

Our sample setup

     +---------------+      |       +-------------+
     | Unix/Linux    |      |       | Linux/Unix  | 
     | Server with   +------+-------+ OSX/*BSD    |
     | OpenSSH SSHD  |              | Client      |
     +---------------+              +-------------+        
                               (loopback interface


  • – You have an accounts on this Linux/Unix based server.
  • – Your private desktop/laptop computer that you use to connect to server. You need to use loopback interface with the IP address Only apps installed on the desktop such as browser, irc client, email client and more have access to

Example: SSH tunnel for an IRC client

A tunnel between local port 8888 on the local interface (IP and the IRC server at, bound to a remote machine’s port 6667. You are going connect to it using the loopback interface:

     +---------------+      |       +-------------+
     | Unix/Linux    |      |       | Linux/Unix  | 
     | Server with   +------+-------+ OSX/*BSD    |
+----+ OpenSSH SSHD  |              | Client      |
|    +---------------+              +-------------+
|                              (loopback interface <=== SSH client
|       +-----------------------+
+-------+ |

If you login to your shell account with: ssh for SSH tunnelling you have to add additional arguments. It goes like this:

ssh -L

If the server/shell account you are using to tunnel through is listening on a different port, for example 2745, it would be written like this:

ssh -p2745

This will open a tunnel between your computer to through your shell account on All traffic will go through your shell account box as encrypted SSH traffic before reaching Your computer now acts like a irc server listening to port 8888. Replace it with any ports you want above 1024 to avoid conflict.

To connect to your local port as if it's irc server. On irssi this would be:

		/server 8888

This will also apply for any other irc clients such as X-Chat. Use for server name and you are good to go. Other fields remain the same.

Howto setup a reverse SSH tunnel

In a scenario where a machine is behind NAT or company firewall a normal SSH tunnel won't work. To overcome this, we have to make use of reverse SSH tunnel. To achieve this, you need an internet reachable box along with the machine behind NAT/firewall. During this guide we will call the machine behind NAT/firewall a NATbox and internet reachable machine an OPENbox.

For reverse SSH Tunnel, there are basically three ports involved. One is the SSH port of workstation, we use it forward the reverse tunnel port into it. The second, is the reverse tunnel port which gets forwarded to workstation's SSH port. The third, is the SSH port of the public box, we need that port to SSH into public box.

From outside if you use public box's SSH port, you log in to that box. If you use reverse tunnel port you get forwarded to workstation. You must be careful about usernames when doing this.

public box ip + public box SSH port + public box username -> access to public box 
public box ip + reverse tunnel port + workstation username -> access to workstation

To establish reverse tunnel from workstation, you will use:

public box ip + public box SSH port + public box username 

And setup the reverse tunnel on a different random port and forward it to local SSH port.

To further protect the SSH daemon, you can implement port knocking where the SSH port is blocked by default and when you send special packets the port is opened for you. You can have the SSH port opened for certain IPs.


So, let us consider, OPENbox is listening to SSH on port 1234. Type following on NATbox:

ssh -p1234 -R 5555:localhost:22

This will initiate a connection from behind the NATd/firewalled box to the publicly reachable box listeing to SSH on port 1234. Once the connection is established it will create a reverse tunnel at port 22 for the remote party to connect and get in. Type the following command on OPENbox:

ssh  -p5555 natboxuser@localhost

Since the NATd/firewalled box has an established connection to OPENbox, the tunnel will go through the same channel. In addition, type the following from anywhere else to access NATbox which will tunnel the traffic through OPENbox:

ssh -p5555 natboxuser@OPENbox

This requires an additional setup on the OpenSSHD server, add the lines to /etc/ssh/sshd_config

 GatewayPorts yes

Save and close the file. Make sure you restart/reload the SSHD on the remote server.


The syntax is as follows to access remote server port without modifying firewall settings:

 ## Syntax ##
ssh -f -L {local-port}:localhost:{remote-server-port}
# Use port 8888 on the localhost to connect to port 4444 on host foo for user bar and run in the background#
# Use port 8888 for your apps such as firefox, xchat, and more #
ssh -D 8888 -f -C -q -N -p 4444 foo@bar &


  1. -f : Requests ssh to go to background just before command execution.
  2. -L port : Specifies that the given port on the local (client) host is to be forwarded to the given host and port on the remote side.
  3. -p port : Port to connect to on the remote host.
  4. -R : Specifies that the given port on the remote (server) host is to be forwarded to the given host and port on the local side.
  5. -D port: Specifies a local "dynamic" application-level port forwarding.
  6. -C : Requests compression of all data. This is useful for speeding up connection.
  7. -q : Quiet mode. Causes most warning and diagnostic messages to be suppressed.
  8. -N: Do not execute a remote command. This is useful for just for warding ports.

For more information see man pages: ssh_config(5).

This quick tutorial was contributed by Wendy Michele. You can too contribute to nixCraft.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 5 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
5 comments… add one
  • Jalal Hajigholamali Dec 3, 2014 @ 5:29

    Thanks for very useful article.

  • stef Dec 4, 2014 @ 4:45

    I was not aware of the reverse tunnel is a nice trick. That is cool as long as your sysadmin does not catch you doing that :-)

    Another very common case is when you are forced to log on a ssh proxy in order to access other ssh hosts behind a firewall. The trick to automate the process is to open a tunnel via the use the proxycommand in your .ssh/config file

    This is explained in several pages. For example

  • tim Dec 4, 2014 @ 7:16

    Understand your company’s corporate policy before attempting “bypass limits” of the network. Tunnelling out through a corporate firewall is a fireable offense in many companies. I have walked people out the door for less.

  • Dinesh Jan 14, 2015 @ 7:02

    I’m looking for something like this but i have not being to able to find out

    I have a server which has torrent client running i can create a tunnel on another server and get the tunnel working properly but what i want is let this tunnelling and torrenting happen on same device i,e eliminate the additional device, is this even possible to do so ? If yes what should be the commands ?

  • sam Jan 29, 2015 @ 22:03

    Nice trick :)

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum