Cheat sheets /

SSH cheat sheet

Connect through a bastion, forward a database port to your laptop, copy files both ways, and set up keys properly, without rereading the man page.

Download all 25 for Snippet Keeper Get Snippet Keeper

In Snippet Keeper, click Import at the bottom of the sidebar and choose the file. They land in your ssh app, and ones you already have are skipped. The highlighted parts become fill-in fields: type a value (or pick one), and the finished command is on your clipboard.

Connecting

Connect to a server

ssh user@host

Connect on another port

ssh -p port user@host

Connect with a specific key

ssh -i key user@host

Run one command remotely

ssh user@host 'command'

Connect through a jump host

ssh -J jump_user@jump_host user@host

Test your GitHub key

ssh -T git@github.com

Close a frozen session

Press Return first, then type ~ and a period.

~.

Keys

Generate a key

ssh-keygen -t ed25519 -C "email"

Install your key on a server

ssh-copy-id user@host

Add a key to the agent and Keychain

macOS. On Linux: ssh-add <key>.

ssh-add --apple-use-keychain key

List keys in the agent

ssh-add -l

Copy your public key

pbcopy < key

Show a key’s fingerprint

ssh-keygen -lf key

Forget a server’s host key

After a server is rebuilt and you get the “host identification has changed” warning.

ssh-keygen -R host

Tunnels

Forward a remote port to your machine

Then connect to localhost:<local_port>. For example, a database only the server can reach.

ssh -N -L local_port:target_host:target_port user@host

Share a local port with the server

ssh -N -R remote_port:localhost:local_port user@host

SOCKS proxy through a server

ssh -N -D port user@host

Copying files

Copy a file to a server

scp file user@host:path

Copy a file from a server

scp user@host:path dest

Copy a folder to a server

scp -r dir user@host:path

Sync a folder to a server

A trailing slash on the source copies its contents rather than the folder itself.

rsync -avz --progress source user@host:dest

Preview a sync that deletes extras

Shows what would change. Drop the n to do it.

rsync -avzn --delete source user@host:dest

Config

Host shortcut for ~/.ssh/config

Then just: ssh <alias>

Host alias
  HostName host
  User user
  IdentityFile ~/.ssh/id_ed25519

Host behind a bastion

Host alias
  HostName host
  User user
  ProxyJump jump_host

Keep idle connections open

Host *
  ServerAliveInterval 60

Keep these one keystroke away.

Snippet Keeper is a Mac app for the commands you keep looking up. Press ⌥⇧Space in any app, type a few letters, fill in the blanks, and it’s on your clipboard.

Snippet Keeper asking for the values in a kubectl command before copying it