Written by Eli Bates 02-08-2021

Getting started with SSH keys for Github

What is SSH and why do I need it?

SSH was first introduced in 1995 as a way to securely login and issue commands to remote computer systems.

It offers you a secure way to authenticate your Github account without providing a personal access token each time you push to the site.

Sounds great, how do I get started?

Firstly, if you are a Windows user then you do not natively have the ability to generate SSH keys. Mac/Linux users you may skip to the following section to get started.

You are going to want to get git bash setup and running on your machine. When this is installed you may launch it from the start menu and continue on to the next step.

Generating your first SSH key

  1. Open up the Terminal (or Git bash on Windows)

  2. Next we will use the ssh-keygen tool to generate our key. We will use the RSA algorithm to accomplish this.

    Edit the following command below...

    Replace your github email inside the first " ", name your key inside "id_rsa_my-keyname" and run the command.

    $ ssh-keygen -t rsa -C "my_github_email@example.com" -f "id_rsa_my-keyname"
  3. You will be prompted to Enter a file where you should save this key (something similar to the following). Just accept the default location by pressing 'enter'.

  4.  > Enter a file in which to save the key (/Users/you/.ssh/id_rsa_my-keyname): [Press enter]
  5. You now can specify a passphrase for your new SSH key.

  6. Finally we will add this new key to the systems ssh-agent.
    In order to do this we will first check if the ssh-agent program is actually running on our system.

  7. You can do this by running the following command:

    eval `ssh-agent -s`

    If all is well you should be greeted with the pid (process id) for the program, an indication that it is running.

  8. Now we will add the new SSH key to the agent. Run the following command, replacing the "my-keyname" with the name you chose in step 2.

    ssh-add ~/.ssh/id_rsa_my-keyname

    Adding your new key to your Github Account

  9. Next you can add the newly created SSH key to your Github Account.
    Run the following command to copy your key to your clipboard. (Be sure to change your key name below!)

    pbcopy < ~.ssh/id_rsa_my-keyname.pub
  10. On your Github page navigate to Settings > SSH and GPC keys

  11. Click the New SSH key button.

  12. Paste your key in the popup window, and hit Add Key. If prompted for your password, confirm.

Pushing changes with your new key

We will now ensure that we have only one key on our SSH manager

  1. Start by entering the following command to clear out the manager

    ssh-add -D
  2. Now you can re-add your key

    ssh-add ~/.ssh/id_rsa_my-keyname
  3. Now you need to make a configuration file to hold your github information

    touch ~/.ssh/config
    nano ~/.ssh/config
  4. Here you can edit your config for your GitHub accounts similar to the one below (replace keyname with your keyname)

    # Personal account, - the default config
    Host github.com-my-keyname
       HostName github.com
       User git
       IdentityFile ~/.ssh/id_rsa_my-keyname
  5. Save the file and close.

  6. Next we will set our config to ensure that we use the correct key Start by entering your github information

    git config user.name "emb2065"   // your github username
    git config user.email "my_github_email@email.com"  // your github email
  7. Now assuming you already have a git project initialized, in the root of that project type the following command (replacwe my-keyname with your keyname, replace github username and repo name)

    git remote add origin git@github.com-my-keyname:your_github_username/repo_name.git
  8. now that your origin is set for your project you may push the project as normal

    git push -u origin master


Using multiple accounts

You might have come across a need to use multiple accounts at some point.

To do this you simply follow the guide again, but use a different key name.

Then in step 4 of the previous section you can add another Host below to your config

    # Personal account, - the default config
    Host github.com-my-keyname
        HostName github.com
        User git
        IdentityFile ~/.ssh/id_rsa_my-keyname

    # Second account
    Host github.com-my-other-keyname
        HostName github.com
        User git
        IdentityFile ~/.ssh/id_rsa_my-other-keyname

Note: Make sure you clear out your SSH manager and re-add your key you wish to use when switching accounts. (Step 1 of the previous section).