14.5.2. Port Forwarding
SSH can secure otherwise insecure
TCP/IPprotocols via port forwarding. When using this technique, the SSH server becomes an encrypted conduit to the SSH client.
Port forwarding works by mapping a local port on the client to a remote port on the server. SSH can map any port from the server to any port on the client. Port numbers do not need to match for this technique to work.
If you want to use reserved port numbers, please note that setting up port forwarding to listen on ports below 1024 requires
To create a TCP/IP port forwarding channel which listens for connections on the
localhost, use a command in the following form:
ssh -L local-port:remote-hostname:remote-port username@hostname
For example, to check email on a server called
POP3through an encrypted connection, use the following command:
ssh -L 1100:mail.example.com:110 mail.example.com
Once the port forwarding channel is in place between the client machine and the mail server, direct a POP3 mail client to use port
localhostto check for new email. Any requests sent to port
1100on the client system will be directed securely to the
mail.example.comis 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 1100:mail.example.com:110 other.example.com
In this example, POP3 requests from port
1100on the client machine are forwarded through the SSH connection on port
22to the SSH server,
other.example.comconnects to port
mail.example.comto check for new email. Note that when using this technique, only the connection between the client system and
other.example.comSSH server is secure.
Port forwarding can also be used to get information securely through network firewalls. If the firewall is configured to allow SSH traffic via its standard port (that is, port 22) but blocks access to other ports, a connection between two hosts using the blocked ports is still possible by redirecting their communication over an established SSH connection.
The connection is only as secure as the client system because forwarding connections in this way allows any user on the client system to connect to that service. If the client system becomes compromised, an attacker can also access the forwarded services.
If preferred, disable this functionality on the server by specifying a
Noparameter for the
AllowTcpForwardingline in the
/etc/ssh/sshd_configfile and restarting the