SSH Tunneling (-L, -R, -D), Bastion & ~/.ssh/config
Reach private databases without opening firewall ports, hop cleanly through Bastion jump hosts, and keep terminal sessions alive across dropped Wi-Fi. Every command is copy-ready and works across Linux, macOS, and Windows OpenSSH.
Command Anatomy: How an SSH Tunnel Reads
ssh -N -f -L 8080:localhost:5432 user@host
-L pulls a remote port onto your laptop; -R
pushes your local port onto a remote server; -D turns SSH into a full SOCKS5 proxy.
1 3 SSH Tunnel Types & Background Flags
Instead of exposing databases or admin panels to the public internet, wrap them inside an encrypted
SSH connection on port 22.
-L)
Opens port 8080 on your laptop and forwards traffic to
localhost:5432 (Postgres) on the remote server.
-R)
Opens port 9000 on the remote server and sends traffic
back to port 3000 on your laptop (great for webhook testing).
-D)
Starts a SOCKS5 proxy at 127.0.0.1:1080. Point your
browser or curl --socks5 127.0.0.1:1080 through it to browse internal
apps.
-N -f)
-N skips opening a remote shell; -f forks
SSH into the background after login so your terminal stays free.
-L = Local laptop listens first.
-R = Remote server listens first.
-D = Dynamic SOCKS5 proxy (no fixed target port needed).
2 Bastion & Jump Host Patterns (-J / ProxyJump)
Production servers usually sit in a private subnet with no public IP. You hop through a hardened
Bastion (jump host) using -J, which keeps your private key safely on
your laptop.
-J)
Connects to bastion first, then transparently tunnels
your SSH session to private server 10.0.1.25.
scp -J and sftp -J support the exact same
jump syntaxโno need to copy files twice.
3 Production ~/.ssh/config & ssh-agent
Stop typing long IP addresses and -J flags. Store your hosts once in
~/.ssh/config (on Windows: C:\Users\You\.ssh\config) and run
ssh prod-app.
# Global defaults for all hosts
Host *
ServerAliveInterval 60
ServerAliveCountMax 3
AddKeysToAgent yes
IdentitiesOnly yes
# 1. Public Bastion (Entry Point)
Host bastion
HostName 203.0.113.10
User ops
Port 22
IdentityFile ~/.ssh/id_ed25519
# 2. Private App Server (Auto-hops via Bastion)
Host prod-app
HostName 10.0.1.25
User deploy
ProxyJump bastion
IdentityFile ~/.ssh/id_ed25519
# 3. Private Postgres Tunnel (Auto-forwards port 5432)
Host prod-db-tunnel
HostName 10.0.1.25
User deploy
ProxyJump bastion
LocalForward 5432 10.0.1.50:5432
RequestTTY no
Unlocking Passphrase Keys Once with ssh-agent
ssh-agent.
4 Persistent Remote Sessions (tmux & mosh)
If your Wi-Fi drops mid-upgrade, a normal SSH shell dies and kills your running command. Use tmux on the server to keep processes alive, or mosh when roaming between networks.
deploy. To detach and
leave it running, press Ctrl+b then d.
deploy session with all
output intact.
5 The Danger Zone
GatewayPorts yes on Public Servers
By default, SSH binds forwarded ports to
127.0.0.1 (localhost only). Setting GatewayPorts yes in
sshd_config binds -R tunnels to 0.0.0.0, exposing
your private laptop service to the entire public internet.
ForwardAgent yes (ssh -A) on Untrusted Jump Hosts
Agent forwarding creates a socket on the jump host. Any
root user or compromised process on that bastion can use your socket to log
into your other servers as you. Always prefer ProxyJump (-J)
instead.
chmod 600 ~/.ssh/config
If your config file is group- or world-writable, OpenSSH
refuses to run with Bad owner or permissions on ~/.ssh/config. Lock it down
with chmod 700 ~/.ssh && chmod 600 ~/.ssh/config.