Post

SSH cheatsheet

Generate a key

1
ssh-keygen -t ed25519 -C "label-for-this-key"
  • -t ed25519 — modern algorithm, prefer over RSA
  • -C — just a label, use anything descriptive (e.g. work-laptop, github)
  • Set a passphrase or leave blank

When prompted Enter file in which to save the key, hit Enter to accept the default (~/.ssh/id_ed25519). If you’re creating keys for multiple services, give each one a name:

1
2
3
/Users/yourname/.ssh/github
/Users/yourname/.ssh/wpengine
/Users/yourname/.ssh/my-vps

This creates two files — e.g. ~/.ssh/github (private, never share) and ~/.ssh/github.pub (public, paste into services).

Check what keys you have:

1
ls ~/.ssh

How many keys do you need?

One per machine is fine for most use cases. One key per service (github, wpengine, vps) gives you more control — you can revoke individual keys without affecting others.

Connect

1
2
ssh user@hostname
ssh -p 2222 user@hostname   # custom port

Use a key

Copy your public key to a server:

1
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server-ip

Or manually append it:

1
cat ~/.ssh/id_ed25519.pub | ssh user@server-ip "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

Connect using a specific key:

1
ssh -i ~/.ssh/my_key user@server-ip

SSH config file

~/.ssh/config lets you define shortcuts so you don’t need to specify keys or usernames every time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Host myserver
  HostName 123.456.789.0
  User ubuntu
  Port 2222
  IdentityFile ~/.ssh/my_key

Host github
  HostName github.com
  User git
  IdentityFile ~/.ssh/github_key

Host wpengine
  HostName git.wpengine.com
  User git
  IdentityFile ~/.ssh/wpengine_key
  IdentitiesOnly yes

Then connect with just:

1
ssh myserver

Add key to SSH agent

Avoids re-entering your passphrase each session:

1
2
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/my_key

Copy public key (to paste into GitHub, WP Engine, etc.)

1
2
cat ~/.ssh/id_ed25519.pub | pbcopy   # macOS
cat ~/.ssh/id_ed25519.pub            # or just print it

Test a connection

1
2
ssh -T git@github.com
ssh git@git.wpengine.com info
This post is licensed under CC BY 4.0 by the author.