ssh

SSH Port Forwarding allows a Local resource behind a NAT o Firewall, to be accessed through a Remote host visible from Internet (a cheap VPS for example). The only requirement for the Remote machine is to have the SSH daemon running and the exposed port open.

Edit /etc/ssh/sshd_config and set GatewayPorts and AllowTcpForwarding to yes in the remote host.

Restart sshd after the change with sudo systemctl restart sshd if the change is done remotely is recommended to validate the configuration before with sudo sshd -t.

Example Usage

Forward incoming connections on port 8888 in the Remote host

to

Port 7777 listening in the Local machine

Run the following command in the Local machine:

ssh -f -N -T -R 8888:localhost:7777 {user}@{remote}
Argument Description
-f Background execution
-N Don't execute remote command
-T Disable terminal allocation
-R Connections to the given TCP port on the remote host will be forwarded to the local machine.

Note: Using lower ports can cause permission error, because root is needed.

To keep connection reliable, use autossh, the following example uses private key authentication that is highly recommended (to avoid password prompt to restart the connection).

autossh -M 20000 -f -R -i path/to/cert 7777:localhost:8888 {user}@{remote}

linux command ssh net

Back