OpenSSH: Tunnels without Windows (A Linux and OSX perspective)

by Bryan Sutherland on July 9th, 2009
No CommentsComments

In this initial SSH tunneling tutorial you will learn how to create tunnels to allow access to systems that may not be directly accessible.  This can be handy for remote administration and securing your traffic while going over the internet.

For the sake of this tutorial we will assume that you have a functioning and accessible SSH server (Windows, Linux or Mac OSX) running on port 22.  I have setup a DynDNS.com domain at doingmorewith.dnsalias.com to make access easier.  You will need to consult with your router documentation on how to forward ports to your server.

Once everything is configured to allow access to your SSH server through your router or firewall you can open the terminal of your choice.  To create a local tunnel (redirecting a local port to a remote server) use the following command:

ssh -L 8080:192.168.1.2:80 doingmorewith.dnsalias.com
ssh -L localport:remoteIP:remoteport remote_server

The above command will create a tunnel that will forward any traffic going to Localhost on port 8080 to 192.168.1.2 on port 80 (the standard HTTP web traffic port).  Note that this will only allow traffic from the machine that created the tunnel.  In order to proxy the traffic and forward all traffic over the tunnel you only need to add the “-g” flag, ie:

ssh -g -L 8080:192.168.1.2:80 doingmorewith.dnsalias.com

When setting up these sessions, ssh will automatically try to send your current username.  To change this you can add the “-l” flag as “-l username“ or user the syntax username@remote_server, ie:

ssh -g -L 8080:192.168.1.2:80 username@doingmorewith.dnsalias.com

Once this connection has been established you will be able to access the tunneled resource using your local IP address or localhost.  In this instance we can verify that things are working by loading up Safari or Firefox and going to the URL http://localhost:8080.  If everything is running you will now have access to your remote web server.

You will probably prefer to watch this video on YouTube in the HD setting to see the commands.


Categories: Linux, Mac OS X, Tutorials

Leave Comment

Commenting Options

Alternatively, you can create an avatar that will appear whenever you leave a comment on a Gravatar-enabled blog.