Github SSH Key

Problem

Password authentication with Github is being deprecated. The tools you use make problems for you? You have multiple GitHub Accounts that you need to configure. Let´s do it with Github SSH Keys.

Solution

The Solution are using SSH Keys with Github. Let´s do it correct from the beginning, so that you can have multiple Keys and accounts on your System:

  • Create SSH Key
  • Announce your public SSH Key to Github
  • Configure SSH
  • Configure your local repository

Create SSH Key

To create an SSH Key, you use ssh-keygen -t rsa -f $path.

$path is the path were to save the key and it´s name, so e.g. ~/.ssh/myPrivateGithubSSHKey.

Announce your public SSH Key to Github

After the key was created, you need to announce the key to Github. To do that go to Github.com and click on the profile icon on the top right:

Github Profile Menu

Click on Settings. In the opening window click on SSH and GPG Keys:

SSH Key Menu point

You should see this:

New SSH Key

Click on “New SSH Key”. You should see this:

New SSH Key Form

In the Title put a description that reminds you where your GitHub SSH Key is coming from (especially in case there was a breach on one of your Systems and you need to disable/remove it)

In the Key field you put the copied content of the key you generated, from the .pub file. So if you created a key myPrivateGithubSSHKey, you copy the content of myPrivateGithubSSHKey.pub.

That looks like this:

SSH Key Content

The exact content will differ, but you can compare that the format matches. If the key is 5x as long you got your private key, don´t share that!

Copy the content of your public key and paste it into the Keyfield of GitHub:

Github SSH Key Added Content

Press Add SSH Key and it´s done. Github might request to confirm the action with your GitHub Password, in that case provide it.

Finally you should see this screen:

Github SSH Key Successfully added

I added the first user before already, but you see the My User, which is the new key.

With this the server side is configured. Let´s move on to the client:

Configure SSH

The SSH Key is known on GitHub. To configure SSH locally we will create an entry, that you can later replicate for the next user you need to do. Add this entry to your ssh config (on Linux and Mac OS ~/.ssh/config):

Host github.com-mpgsk
HostName github.com
User myPrivateGithubSSHKey
IdentityFile ~/.ssh/myPrivateGithubSSHKey

Replace myPrivateGithubSSHKey with the name for your Github user on the User part and the filename for IdentityFile.

Remember the Host github.com-mpgsk, we will need that to configure your repository.

Configure your local repository

For SSH Keys on GitHub to work, you need to configure the repository settings correctly.

Configuration to use SSH Key on GitHub

These are the configuration parts you need. Open your git config in .git/config.

First search for [core].

There you need to add sshCommand=ssh -i ~/.ssh/keyname.

The Second step is to search for [user] . configure your name and email that you used on GitHub. This is important, they need to match.

The third step is to configure the remote. Usually origin.

It´s important to use the git@ in the front and the github.com-mpgsk (or however you named it in your ssh config) as the Host part.

:myPrivateGithubSSHKey is the user you use on GitHub (or if you are working on a repository of someone else, it´s they name or Organization) and then your repository name.

You can also find the url to copy on the Repository on Github. Look for the code button on the Repository Mainpage:

GitHub Code Button

If you click it, you can select the SSH option and get the url:

GitHub SSH Url

Copy the url and adjust only the host part (github.com) and it´s done.

For an existing repository that´s it.

Clone a remote repository

If you need o clone a new repository with the ssh config done earlier you just use the url for ssh they provide and replace the host part as you do for an locally existing repository.

For more details about cloning a repository look here.

Let me know when it helped you.

Best,

Frank

Sources:

https://stackoverflow.com/questions/30068298/git-fatal-could-not-read-from-remote-repository-please-make-sure-you-have-th/40049051

https://medium.com/@dustinfarris/managing-multiple-github-deploy-keys-on-a-single-server-f81f8f23e473

Leave a Reply