30

Assume I have access to an SSH server that supports tunneling, and I am on a PC that is running *ubuntu, how do I set up a local SOCKS proxy that tunnels the traffic through that SSH server before reaching the Internet?

1 Answer 1

47

The ssh binary that you use when you connect to a server running ssh supports running a SOCKS proxy out of the box, with the -D flag. Example:

ssh -D 1337 -f -C -q -N user@remote -p 22
  • -D 1337 tells ssh to launch a SOCKS server on port 1337 locally.
  • -f forks the process into the background.
  • -C Turns on compression.
  • -q enables "Quiet mode", since the purpose here is only to tunnel we don't really care about error output and such.
  • -N tells ssh that no commands will be sent (-f complains if we don’t specify this).
  • -p specifies what port to use; obviously this is defaulted to 22 so the statement above is pointless, but included for clarity.

When your SOCKS server is up and running, you simply need to make sure that your application (usually a web browser) is trying to connect to the local SOCKS proxy, and not the regular Internet.

In Firefox 29 (explained here as an example), this is achieved by going to the menu (the three sausages in the top right), followed by Preferences > Advanced > Network > (Connection) > Settings... - make sure you only fill out the SOCKS field! Since your proxy is on the same system, you can use localhost or 127.0.0.1 to point back at whatever port you set it to on the same system.

If you need your DNS requests to be tunneled as well (if they are not, your DNS lookups will reveal what websites you are trying to visit), you can just check on "Remote DNS" or as well do this entire configuration in about:config. In the last case, open it up and set these values:

network.proxy.socks : 127.0.0.1
network.proxy.socks_port : 1337
network.proxy.socks.remote_dns : true
network.proxy.socks_version : 5
network.proxy.type : 1

You may also need this setting to exclude certain domains or sites from tunneling:

network.proxy.no_proxies_on : localhost, 127.0.0.1, 192.168.0.0/24, .yourcompany.com

This answer was written from France, but tunneled via Sweden :)

8
  • This only would work on firefox.
    – Braiam
    May 20, 2014 at 23:47
  • 4
    It is in no way Firefox-specific, I included the Firefox section as a service since that is usually what people use SSH tunneling for, it would work in any application that supports SOCKS proxies (ssh doesn't care).
    – pzkpfw
    May 21, 2014 at 7:55
  • 2
    Speaking of setting up the browser to use the proxy. If you use google chrome you will need declare the proxy in the network-settings of ubuntu, as this is what google chrome will be using. Unofficial CLI options have been available to declare the proxy without this. But as of some weeks ago this not possible anymore.
    – MadMike
    Oct 15, 2014 at 6:27
  • Thanks, you got me out of hell. And I only got there because I didn't read through your answer properly about 10 hours ago. Should have paid attention to -D and -p part. Aug 22, 2015 at 1:17
  • Also easier to remember: ssh -D 1337 user@host
    – Alcalyn
    Nov 5, 2019 at 10:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .