SSH Port Forwarding (tunnel) in Linux for Proxy etc.

Dynamic:

Dynamic port forwarding turns your SSH client into a SOCKS proxy server. To use dynamic port forwarding with the default SOCKS port:

ssh -C -D 1080 yourRemoteServer:port
ssh -C -D 1080 yourRemoteServer (when you use the default port 22)
ssh -C -D 1080 -i yourKeyFile.pem ec2-user@yourEC2
  • The -D option specifies dynamic port forwarding. 1080 is the standard SOCKS port. Although you can use any port number, some programs will only work if you use 1080. 
  • The -C option enables compression, which speeds the tunnel up when proxying mainly text-based information (like web browsing), but can slow it down when proxying binary information (like downloading files).
Now you have a SOCKS proxy server: localhost:1080

Static:

For instance, to connect to mysql.i88.ca:3306 through tennel.i88.ca from your local port 3306

ssh -L 3306:mysql.i88.ca:3306 [email protected]

Both:

You could add:
 -N      Do not execute a remote command.  This is useful for just forwarding ports (protocol version 2 only).

See also:

How to connect to remote MySQL server by SSH tunnel


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