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
Open up the Terminal (or Git bash on Windows)
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 "email@example.com" -f "id_rsa_my-keyname"
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'.
> Enter a file in which to save the key (/Users/you/.ssh/id_rsa_my-keyname): [Press enter]
You now can specify a passphrase for your new SSH key.
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.
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.
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.
Adding your new key to your Github Account
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
On your Github page navigate to Settings > SSH and GPC keys
Click the New SSH key button.
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
Start by entering the following command to clear out the manager
Now you can re-add your key
Now you need to make a configuration file to hold your github information
touch ~/.ssh/config nano ~/.ssh/config
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
Save the file and close.
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 "firstname.lastname@example.org" // your github email
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 email@example.com:your_github_username/repo_name.git
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).