Tunneling

https://linuxize.com/post/how-to-setup-ssh-tunneling/ (opens in a new tab) https://www.metahackers.pro/ssh-tunnel-as-socks5-proxy/ (opens in a new tab)

Local Port Forwarding

Use Case

Connect to Prod DB through a bastion server from local.

Local - SSH server(e.g. Proxy) - Destination server(e.g. Prod DB)

ssh -L [LOCAL_IP:]LOCAL_PORT:DESTINATION:DESTINATION_PORT [USER@]SSH_SERVER
 
ssh -L 3336:db001.host:3306 user@pub001.host      # Different ssh and destination server
ssh -L 5901:127.0.0.1:5901 -N -f user@remote.host # Same ssh and destination server

Remote Port Forwarding

Use Case

Expose local web app to outer world (e.g. ngrok, localtunnel)

ssh -R [REMOTE:]REMOTE_PORT:DESTINATION:DESTINATION_PORT [USER@]SSH_SERVER
 
ssh -R 8080:127.0.0.1:3000 -N -f user@remote.host # Expose the_ssh_server_ip:8080

Dynamic Port Forwarding

Use Case

Http Tunneling (e.g. web proxy). This creates socket on local machine, acts as SOCKS proxy.

ssh -D [LOCAL_IP:]LOCAL_PORT [USER@]SSH_SERVER
 
ssh -D 9090 -N -f user@remote.host