Tradam.fyi

How to set up SSH keys for Github

Theme: Guide

Created: November 14th, 2020 19:20
Updated: December 9th, 2020 22:46

Eyes laid on this: 32


I found 0 good guides online for how to make and use ssh keys with github. All of them failed in some way or another where either they didn't work or they just flat out left steps. Hopefully this guide will be more complete. If I am missing something or it doesn't work, please do leave a comment so I can make this guide as complete as possible. And yes I am aware at how long this guide is for something that should be as simple as just adding an ssh key. shrug. Sadly it is this needlessly complex way instead.

Note that this is a guide for linux users using artix/arch. Although it will probably work with your distro, there are no guarantees it will work(not that there were any guarantees for anything in the first place)


Creating the key

I took this information from the handy dandy arch wiki, you can create a key using the following format:

ssh-keygen -t ed25519 -C "yourname-$(date -I)"

where you set "yourname" to whatever unique name you want assigned to your key. The ssh-keygen program will then prompt you to set a filename(preferably use whatever you made "yourname" into) and a password(not required, although safer if anyone would ever manage to steal your key)

Explanation of the parameters:

-t ed25519

sets the algorithm used for the key is ed25519.

-C

sets what is written in the comment field of the key. This makes it easier to know the key is for.

$(date -i)

in the comment field prints out the day you made the key.

If you have multiple github accounts, you need a unique key for each account. Name them accordingly so that you don't get confused.


Configuring Github

Now that you have your key, you need to copy the contents of the key into github. Under

Settings > SSH and GPG keys

and then you click the green New SSH key button. Here you paste the contents of you key text file into the Key textbox. MAKE SURE TO PASTE YOUR .pub KEY AND NOT YOUR PRIVATE KEY. NEVER SHARE YOUR PRIVATE KEY WHICH GENERATES WITHOUT THE .pub EXTENTION Make the title whatever you like.


Configuring ssh-add

Whenever you open your terminal you will have to start the authentication agent and then load up the key.

To start the auth agent:

eval `ssh-agent`

and then to add they key(assuming you are using the default key location):

ssh-add ~/.ssh/yourname

If you have multiple keys, run this command for each key. Now you can use github with an ssh key. You can make changes and push them now without being prompted for a password. To clone a repo you will still be prompted for a password if you just use http/https or "url" for the repo. You need to instead use the SSH link. To get this link go to the repo, and click the green Code button and select SSH. For example to clone my musicSorter script you would use the following ssh link:

[email protected]:realtradam/musicSorter.git


Configuring ~/.bashrc to automate the above

At the moment, you will have to do the above step every time you open a terminal, which is very tedious. However if you add them to your ~/.bashrc file then the commands will be run automatically each time you open a terminal:

eval `ssh-agent` > /dev/null 2> /dev/null
ssh-add -q ~/.ssh/yourname

I added a little bit extra to the commands. The

 > /dev/null 2> /dev/null

Is so that there is no output created from the commands, so it doesn't look ugly each time you open a terminal. Optional but more visually pleasing. "-q" makes the ssh-add command suppress output, effectively doing the same thing.


Configuring ~/.ssh/config

This is mainly important if you want to use multiple ssh keys if you are using multiple github accounts. Although it is possible that your setup may not work without this config file so you should set it up anyway.

If the file doesnt exist, dont worry. Create it and open it up in your text editor of choice. For your default account use the following text:

Host github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/yourname

Where your IdentityFile points to the key you made for the appropriate github account. Nothing else needs to be changed for this block. For your second(or more if you use more accounts) you need to use this second block.

Host second.github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/yoursecondname

Similarly to the first block you change the IdentityFile to be whatever your second key is. However you also have to change the "Host" link on the first line. The bolded section of the link of the host can be any memorable name you like. You will need to remember this portion as you will need to add this prefix to the ssh url whenever you clone and want to use that specific account(or just check the ssh config file if you forget). Using the musicSorter example from before, the link would change from

[email protected]:realtradam/musicSorter.git

to

[email protected]second.github.com:realtradam/musicSorter.git

This will let ssh know which key to correctly use.


Converting your local repos

Once you have done this you might notice that any local repo you already have still doesn't work! This is because git saves the url that was used to clone it, and uses this url to push/pull. You can see what the current url is by using the following command in the directory of your repo:

git remote -v

If you want to change the url to use ssh, simple take the ssh url from github(as described above) and place it into the following command(I am using the musicSorter as an example):

git remote set-url origin [email protected]:realtradam/musicSorter.git

If you want to use an account different from your default, don't forget to add that prefix! Now everything should work as intended, with no more hassle of using passwords! Enjoy!


Back to Index