How to set up SSH port forwarding in Linux

What is "Port Forwarding" or "Tunneling"?

In addition to the direct access provided by the "ssh" and "scp" commands, the SSH protocol includes a feature called "Port Forwarding", or "Tunneling". This can be used to provide secure access to other services that do not normally encrypt data during transmission.

For example, to receive email, many email programs use the (unencrypted) POP3 protocol to connect to a mail server on port 110. If we can SSH to the mail server before downloading our mail, we can configure the SSH session to "forward" port 110 through the encrypted SSH link. Now, if we configure our email client to connect to port 110 on our local machine it will in fact be connecting to the remote mailserver, but all the data, (i.e. our incoming email), will be transferred over the encrypted SSH link.

If we also forward port 25, (which normally handles SMTP, the outgoing email protocol), then both incoming and outgoing email will be encrypted. Any number of ports can be forwarded in a single SSH session. For example, we could also forward port 80, (HTTP), to provide secure access to a corporate web server.

In effect, it is usually possible to create a pretty good approximation to a VPN, (Virtual Private Network), just by forwarding a handful of common ports, and using scp, (or WinSCP), to transfer files. In fact, a VPN is roughly equivalent to an SSH session where everything is being forwarded over a single secure channel.

To create a TCP/IP port forwarding channel which listens for connections on the localhost, use the following command:

ssh -L local-port:remote-hostname:remote-port   [email protected]

ssh -L [email protected]mySSHServer -N 
ssh -L -L \ [email protected]mySSHServer -N 

To check email on a server called using POP3 through an encrypted connection, use the following command:

ssh -L

Once the port forwarding channel is in place between the client machine and the mail server, direct a POP3 mail client to use port 1100 on the localhost to check for new mail. Any requests sent to port 1100 on the client system are directed securely to the server.

If is not running an SSH server, but another machine on the same network is, SSH can still be used to secure part of the connection. However, a slightly different command is necessary:

ssh -L

In this example, POP3 requests from port 1100 on the client machine are forwarded through the SSH connection on port 22 to the SSH server, Then, connects to port 110 on to check for new mail. Note, when using this technique only the connection between the client system and SSH server is secure.

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 -D1080

to make the connection to and start a SOCKS proxy on localhost port 1080.
(There is no space between "-D" and "1080", at least it is what it should work in my fedora 18 system)

In fedora, configured the proxy settings as follows: clicked on System, Preferences, Internet and Network, Network Proxy

Featured Post

When you copy an AWS EC2 snapshot, you can choose to encrypt the copy

Encrypted Snapshots When you copy a snapshot, you can choose to encrypt the copy (if the original snapshot was not encrypted) or you can s...