SSH dynamic port forwarding with SOCKS

Posted by JoshTriplett on Mon 23 Oct 2006 at 12:35

SSH has numerous uses beyond just logging into a remote system. In particular, SSH allows you to forward ports from one machine to another, tunnelling traffic through the secure SSH connection. This provides a convenient means of accessing a service hosted behind a firewall, or one blocked by an outgoing firewall.

However, forwarding an individual port still requires you to change where your program connects, telling it to use a non-standard port on localhost rather than the standard port on the remote machine, and it requires a separate port forward for each machine you want to access. Dynamic port forwarding via SOCKS provides a more convenient alternative.

The examples in this article assume that you reside behind a restrictive firewall which does not allow outgoing SMTP connections except to a designated mail server. You want to connect to a different mail server,, on port 25. You have an SSH account on a machine, which does not reside within the restrictive firewall and can thus access port 25 on

With standard SSH port forwarding, you could enter the command:

ssh -L
This will forward port 2525 on your machine to port 25 on, by way of You will then need to configure your mailer to send mail to localhost, port 2525, and use the authentication information for your mail account on For example, in Thunderbird^WIcedove, you could add an additional outgoing mail server via Edit->Preferences, "Outgoing Mail Server (SMTP)", "Add...", and either set it as the default or explicitly set your mail account to use that server. You can then send your mail, which will potentially (if you use secure authentication with give you a security warning about localhost presenting a certificate for, and then prompt you for your account password. After you have finished sending all the mails you want to send, you can then change your outgoing mail server back to the previous setting, and exit SSH.

To avoid all this hassle, SSH also supports dynamic port forwarding via SOCKS. SOCKS defines a standard mechanism for a client to connect to a server by way of a proxy. SSH can serve as the proxy, allowing you to connect to and make connections from there to an arbitrary server such as Simply run:

ssh -D 1080
to make the connection to and start a SOCKS proxy on localhost port 1080.

In order to make use of the SOCKS proxy, you can either use applications which can speak SOCKS natively, or you can use a socksifier program like tsocks. tsocks provides a library used with LD_PRELOAD, which replaces the standard sockets functions like socket, connect, and sendto with functions that make use of a designated SOCKS proxy. The tsocks script runs a program with this library loaded. The library will read /etc/tsocks.conf to find out what SOCKS proxy to use. To configure tsocks to work with an SSH SOCKS proxy on localhost, edit the default /etc/tsocks.conf, change the server variable to, and comment out the path example.

Now that you have tsocks configured, you can run the following whenever you want to send mail via

ssh -D 1080
tsocks thunderbird
This will open the SSH-tunnelled SOCKS proxy to and run thunderbird. You can then send mail normally, without changing the outgoing server configuration, and without seeing any authentication mismatch warnings.



Posted by dominic (142.58.xx.xx) on Mon 23 Oct 2006 at 17:31
Take a look at other SSH options including -f and -N which will background the process and prevent running any remote command (respectively). So I usually do:

ssh -fND3125

[ Parent | Reply to this comment ]

Posted by Anonymous (67.167.xx.xx) on Thu 10 Jan 2008 at 01:09
How do you verify packets are actually being sent through the SOCKS proxy?

[ Parent | Reply to this comment ]

Posted by Steve (82.32.xx.xx) on Thu 10 Jan 2008 at 18:58
[ View Steve's Scratchpad | View Weblogs ]



[ Parent | Reply to this comment ]

Posted by Anonymous (202.12.xx.xx) on Mon 13 Dec 2010 at 07:01
hey, how to do dynamic port-forwarding using freesshd and backtrack? i tried but my configuration wasn't correct. im stuck. really hope to get some help or step by step tutorial if possible? i have search the net but was confusing. i'm a noob here though =x thank so much. appreciate your help.

[ Parent | Reply to this comment ]

Posted by Anonymous (12.151.xx.xx) on Mon 6 May 2013 at 21:32
How about windows? Is there a similar thing like tsocks?

[ Parent | Reply to this comment ]

Posted by Anonymous (185.5.xx.xx) on Fri 7 Mar 2014 at 13:53
I think that you can use PuTTY for SOCKS forwarding on Windows.

[ Parent | Reply to this comment ]

Sign In







Current Poll

Will you stick to systemd as the default in Debian?

( 895 votes ~ 35 comments )