Remote Computing Quick Reference

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.

โฑ๏ธ 6 min ref๐ŸŸข Beginner Friendly๐Ÿ“… Updated: 2026

Command Anatomy: How an SSH Tunnel Reads

Background Local Port Forward
ssh -N -f -L 8080:localhost:5432 user@host
Flags -N -f No shell, run in background
Direction -L Listen on Local laptop
Port Mapping 8080:localhost:5432 Your 8080 โ†’ Server's 5432
SSH Server user@host Encrypted tunnel endpoint
๐Ÿ’ป YOUR LAPTOP ๐Ÿ” SSH SERVER ๐ŸŽฏ TARGET SERVICE -L LOCAL FORWARD Listen :8080 SSH Tunnel (22) Private DB :5432 -R REMOTE FORWARD Local App :3000 Listen :9000 Remote Client -D DYNAMIC SOCKS5 SOCKS5 :1080 SSH Proxy (22) Any Internal/Web
-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.

ssh -L 8080:localhost:5432 user@host
Local Port Forward (-L) Opens port 8080 on your laptop and forwards traffic to localhost:5432 (Postgres) on the remote server.
ssh -R 9000:localhost:3000 user@public-server
Remote Port Forward (-R) Opens port 9000 on the remote server and sends traffic back to port 3000 on your laptop (great for webhook testing).
ssh -D 1080 -N user@host
Dynamic SOCKS5 Proxy (-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.
ssh -N -f -L 8080:10.0.1.50:5432 user@bastion
Headless Background Tunnel (-N -f) -N skips opening a remote shell; -f forks SSH into the background after login so your terminal stays free.
๐Ÿง  Memory Trick: Look at the first letter to know where the listening port opens: -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.

Single-Hop Jump Host (-J) Connects to bastion first, then transparently tunnels your SSH session to private server 10.0.1.25.
ssh -J user@jump1,user@jump2 [email protected]
Multi-Hop Chained Bastions Separate multiple jump hosts with commas to chain through DMZ โ†’ management โ†’ isolated production subnets.
scp -J ops@bastion ./build.tar.gz [email protected]:/opt/
Copy Files Through a Bastion 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.

~/.ssh/config (Production Template)
# 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

eval "$(ssh-agent -s)" && ssh-add ~/.ssh/id_ed25519
Start Agent & Cache Private Key Prompts for your key passphrase once and keeps it unlocked in memory for your terminal session.
ssh-add -l
List Loaded Keys Verifies which SSH key fingerprints are currently active in 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.

tmux new -s deploy
Start a Named Session Creates a persistent session named deploy. To detach and leave it running, press Ctrl+b then d.
tmux ls
List Running Sessions Shows all active detached/attached tmux sessions on the server.
tmux attach -t deploy
Reattach After Disconnect Reconnects straight back to your deploy session with all output intact.
mosh user@host
Mobile Shell for Roaming (UDP 60000โ€“61000) Authenticates via SSH, then switches to UDP so your shell survives laptop sleep and IP changes (Wi-Fi โ†’ 5G).

5 The Danger Zone

โš ๏ธ Tunnel & Bastion Mistakes That Expose Internal Networks
1. Enabling 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.

2. Using 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.

3. Forgetting 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.