How to create one ssh key for all github repos

There are few ways to manage private and public keys for github repositories. When a user has multiple github accounts/repositories using multiple personal and work computers, it’s easier to create keys for different usage, so the correct key is used for the associated repositories.

To learn about creating multiple keys and configure to use them for different repositories, go to https://soisos.com/?p=235

If you are interested in having just one key for all your repositories in your account, this is the post to read.

on MacOS, the default ssh key directory is

/Users/lisajohnson/.ssh/

Let’s back up all the keys that were previously created

cp -r ~/.ssh ~/.ssh_backup

Go to the default ssh key directory

cd ~/.ssh

Generate a new key (master key). Replace “lisajohnson@mail.com” with the account email associated with your github account. You may be prompted to enter a key file name. Just leave it as a blank for a default file name (github by default looks for default filename generated by keygen). Since our example uses rsa to generate a ssh key, the default file name will look like id_rsa and id_rsa.pub, private and public keys respectively.

ssh-keygen -t rsa -b 4096 -C "lisajohnson@mail.com"

Add the newly generated key to ssh agent (id_rsa)

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa

Move config file from the key directory. If there is the config file in the .ssh directory, github first looks for specific instructions to match keys with repositories. This is not what we want. We want github to look for one key. not config file. You can either move config file elsewhere or change the name to something else. in this example, the name config was changed to config_OLD

mv ~/.ssh/config ~/.ssh/config_OLD. # changed the name to config_OLD

Deploy the public key to github account

  1. Click on your Account Avatar on the right upper corner of the github page
  2. Click on the Settings and you will see Profile menu on the left
  3. Click on SSH and GPG Keys on the menu
  4. Click on NEW SSH Key
  5. Title can be anything but you want to make it informative (I usually put my pc info so I know what the key is for.
  6. Key type: Authentication key
  7. Copy the public key into the Key section. The following code will display the content of the file. Copy and paste it into the Key section of the github page. and Save
cat ~/.ssh/id_rsa.pub

Test. If the ssh key is correctly set up, you should be able to clone the repository you have read/write access to.

  1. Go to github repository where you want to clone (download).
  2. Click on the Green Button CODE
  3. Click SSH and copy the URL for ssh. Go to the directory where you want to download and store the repository folder, and paste it
cd myproject_folder
git clone git@github.com:myaccount/repository.git

If downloaded, your ssh key is correctly set up. Congratulations.