SSH Local port forwarding
$ ssh -L 0.0.0.0:local_port:target:target_port username@bastion
SSH Reverse port forwarding
$ ssh -R local_ip:local_port:target_ip:target_port username@local_ip
SSH Dynamic Port Forwarding
$ ssh -D 127.0.0.1:local_port username@bastion
Useful options and tips in SSH port forwarding sessions
-f: This option requests SSH to go into the background just before it executes the command. This is commonly used when you want to set up port forwarding without opening an interactive shell session. The SSH connection will run in the background, allowing you to continue using the terminal for other tasks.
As the forwarded session will be running in the background, it would be needed to kill the process once it is not needed anymore (ps aux | grep ssh
will do the job done)
-N: This option tells SSH not to execute a remote command. It is often used in conjunction with port forwarding when you only want to establish the SSH connection for tunnelling purposes and do not need to execute any remote commands on the destination host.
Forward multiple sets of ports in a single ssh
command can also be used:
-L 0.0.0.0:local_port_1:target:target_port_1
-L 0.0.0.0:local_port_2:target:target_port_2