SSH Cheatsheet

Keys, remote shells, port forwarding, config file.

1 credit

Keys

4 items
Generate ed25519 key
ssh-keygen -t ed25519 -C "you@host"
Copy to server
ssh-copy-id user@host
Test agent
ssh-add -l
Add key to agent
ssh-add ~/.ssh/id_ed25519

Connecting

5 items
Basic
ssh user@host
Specific key
ssh -i ~/.ssh/custom user@host
Non-default port
ssh -p 2222 user@host
Run remote command
ssh user@host "ls -la /var/log"
Persistent connection
ssh -o ServerAliveInterval=60 user@host

Port forwarding

3 items
Local forward (localhost:5432 → remote DB)
ssh -L 5432:localhost:5432 user@host
Remote forward (expose local to server)
ssh -R 9000:localhost:3000 user@host
SOCKS proxy
ssh -D 1080 user@host

~/.ssh/config

text
Host prod
  HostName 203.0.113.10
  User deploy
  Port 2222
  IdentityFile ~/.ssh/prod_ed25519
  ForwardAgent yes

Host *
  ServerAliveInterval 60
  ControlMaster auto
  ControlPath ~/.ssh/cm-%r@%h:%p
  ControlPersist 10m

ControlMaster reuses a single TCP connection for multiple sessions — noticeably faster.

File transfer

3 items
scp single file
scp local.txt user@host:/path/
scp recursive
scp -r ./dir user@host:/path/
rsync (better)
rsync -avz --progress ./dir user@host:/path/

Further reading