env.dev

SSH Cheat Sheet

Quick reference for SSH: connecting, key generation, agent, port forwarding, jump hosts, SCP, SFTP, config file, and permissions.

Last updated:

A quick reference for SSH — from connecting to remote hosts and managing keys to tunneling, agent forwarding, and the config file.

Connecting

CommandDescription
ssh user@hostConnect to a remote host
ssh -p 2222 user@hostConnect on a non-default port
ssh -i ~/.ssh/id_ed25519 user@hostConnect using a specific key
ssh -v user@hostVerbose output (debug connection issues)
ssh -vvv user@hostMaximum verbosity
ssh user@host "command"Run a single command without a shell

Key Generation

CommandDescription
ssh-keygen -t ed25519 -C "email@example.com"Generate an Ed25519 key (recommended)
ssh-keygen -t rsa -b 4096 -C "email@example.com"Generate a 4096-bit RSA key
ssh-keygen -f ~/.ssh/mykeySpecify output file
ssh-keygen -p -f ~/.ssh/id_ed25519Change passphrase of an existing key
ssh-keygen -l -f ~/.ssh/id_ed25519.pubShow fingerprint of a public key
ssh-keygen -R hostnameRemove a host from known_hosts

Copying Keys

CommandDescription
ssh-copy-id user@hostCopy default public key to remote host
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@hostCopy a specific public key
ssh-copy-id -p 2222 user@hostCopy key to host on non-default port

SSH Agent

CommandDescription
eval "$(ssh-agent -s)"Start the SSH agent
ssh-addAdd default key (~/.ssh/id_ed25519, id_rsa)
ssh-add ~/.ssh/mykeyAdd a specific key
ssh-add -lList loaded keys (fingerprints)
ssh-add -LList loaded keys (public keys)
ssh-add -d ~/.ssh/mykeyRemove a key from the agent
ssh-add -DRemove all keys from the agent

Port Forwarding & Tunneling

CommandDescription
ssh -L 8080:localhost:80 user@hostLocal forward: localhost:8080 → remote:80
ssh -L 5432:db-host:5432 user@jumpLocal forward through a jump host to another host
ssh -R 8080:localhost:3000 user@hostRemote forward: remote:8080 → local:3000
ssh -D 1080 user@hostDynamic SOCKS5 proxy on local port 1080
ssh -N -f -L 8080:localhost:80 user@host-N: no shell, -f: background; useful for tunnels

Jump Hosts (ProxyJump)

CommandDescription
ssh -J jump-host user@targetConnect to target via a jump host
ssh -J user@jump:22 user@targetJump host with explicit user and port
ssh -J jump1,jump2 user@targetChain multiple jump hosts

SCP — Secure Copy

CommandDescription
scp file.txt user@host:/remote/path/Copy local file to remote
scp user@host:/remote/file.txt .Copy remote file to local
scp -r ./dir user@host:/remote/path/Recursively copy a directory
scp -P 2222 file.txt user@host:~/Copy using non-default port
scp -i ~/.ssh/mykey file.txt user@host:~/Copy using a specific key

SFTP

CommandDescription
sftp user@hostOpen an interactive SFTP session
sftp -P 2222 user@hostConnect on a non-default port
put file.txtUpload a file (inside sftp session)
get remote-file.txtDownload a file (inside sftp session)
ls / pwd / cdNavigate remote filesystem (inside sftp session)
lls / lpwd / lcdNavigate local filesystem (inside sftp session)

SSH Config File (~/.ssh/config)

DirectiveDescription
Host <alias>Define a host alias
HostName <hostname>Actual hostname or IP
User <username>Default username for the host
Port <port>Default port (default: 22)
IdentityFile <path>Path to private key
ForwardAgent yesEnable agent forwarding
ServerAliveInterval 60Send keepalive every 60 seconds
ProxyJump <jump-host>Route connection through a jump host
AddKeysToAgent yesAuto-add keys to agent on first use

Common Config Example

Config snippetPurpose
Host myserverAlias used as: ssh myserver
HostName 203.0.113.10Actual IP or domain
User deployLogin as "deploy"
IdentityFile ~/.ssh/id_ed25519Use this key
Port 2222Non-standard port
Host *Defaults applied to all hosts
AddKeysToAgent yesAuto-load keys into agent
ServerAliveInterval 60Keep connections alive

Permissions

PathRequired permission
~/.ssh/700 (drwx------)
~/.ssh/authorized_keys600 (-rw-------)
~/.ssh/id_ed25519 (private key)600 (-rw-------)
~/.ssh/id_ed25519.pub (public key)644 (-rw-r--r--)
~/.ssh/config600 (-rw-------)
~/.ssh/known_hosts600 (-rw-------)