SSH Tunnels (Port Forwarding)

It is surprisingly easy to set up SSH tunnels. All you need is SSH access to a box with Internet connection that will act as the port forwarder. Something as simple as the following command with set up an encrypted tunnel that will forward the local_port on the ssh box to the remote_port on the remote_host.

ssh -N -L local_port:remote_host:remote_port user@local_host

And that’s it. The “-N” argument tells SSH not to run any commands, which is the behaviour required when setting up tunnels. The “-L” argument sets up the port forwarding stuff.

That command will run in the foreground, or background if you suffix an ampersand to it, and will gladly service requests destinted for local_port on the ssh box (localhost, here).

It is important that TCP-Forwarding be enabled for this to work. In order to enable that, the directive

AllowTcpForwarding yes

must be set in the SSH daemon’s config file (man sshd_config(5)).

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s