Skip to content

Instantly share code, notes, and snippets.

@zackcreach
Last active January 24, 2022 18:01
Show Gist options
  • Save zackcreach/6c9d054f9c17810897e1369d6ba09f5a to your computer and use it in GitHub Desktop.
Save zackcreach/6c9d054f9c17810897e1369d6ba09f5a to your computer and use it in GitHub Desktop.
Create/add/remember a new Github SSH key on client & server

Create/add/remember a new Github SSH key on client & server

  1. Create a new .ssh directory (if it doesn't exist) in your home directory, cd in and generate new ssh key:
$ ssh-keygen -t rsa -C "email@address.com"
  1. Print and copy the contents of the public key you just made:
$ cat ~/.ssh/NEW_KEY_NAME.pub | pbcopy
  1. Head over to Github > Settings > SSH and GPG Keys > New SSH Key and paste in what you just copied in the Key section. Title should be the the NEW_KEY_NAME exactly as you generated it.

  2. Add this key to your identities list (-K adds to keychain):

$ ssh-add --apple-use-keychain ~/.ssh/NEW_KEY_NAME
  1. Create new config file (if it doesn't already exist) and edit using vi/nano/etc.:
$ vi ~/.ssh/config
  1. Now add an entry for your new key:
#github
  Host github-USERNAME
  HostName github.com
  User git
  IdentityFile ~/.ssh/NEW_KEY_NAME
  UseKeychain yes
  AddKeysToAgent yes
  1. While you're in there, you can also "forward" this key to your server upon connecting, which lets you push/pull/clone etc. In that case, tack this on to config as well:
#server
  Host DOMAIN.COM
  ForwardAgent yes

To test that your key is working on your own machine (or your server should you decide to forward the key), run:

$ ssh -T git@github.com

If all goes well, you'll see (on both client/server):

Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.

Protip: you can continue steps 1-6 for multiple profiles (make sure git config is set up appropriately first).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment