At some time or another, as Xamarin developers, we all encounter the need to generate an SSH key for accessing a Git repository. Whether it’s a new GitHub user that an employer or client has asked us to setup (GitHub only allows one unique SSH globally, as opposed to per user) or simply switching from https to SSH, in my experience, it comes up.

Every time it does I find myself falling into the same trap: generating the new key, adding it to the keychain and copying the details to the git hosting repository and moving on. Then I find a few days later I can no longer connect and I get the permission denied message.

This is because I always forget to add the key to the config file on my local development machine. The simplest way I’ve found to add and permanently store a new SSH key for Git (or anything else for that matter) is by doing the following.

Generate the key

Open a new terminal window and navigate to the default user store for SSH keys and generate a new key by entering the following commands

Choose a name for this key and optionally add a passphrase to it.

Add that key to the key chain

Next we want to add the key to the keychain.

Enter the following to do this:

Add or edit the macOS .ssh config file so it always picks up the SSH key even across restarts

Whether you already have a config file for your .ssh folder or not, enter the following to create it or edit it:

Then add the following, replacing the key name with your own, to the file:

Then press Control + O to write the file and then Control + X to exit nano.

Add the key to the host (e.g. Microsoft Azure DevOp’s, GitHub etc)

Next you just need to copy the text from the key’s .pub file and paste it into the relevant area of your hosting provider. You can either open this in your favourite editor such as Visual Studio Code by pressing ⌘ Command + P and entering the location:

Or do it straight from the terminal by entering:

And that’s it. Whilst not the most exciting of articles, this is something that has tripped me up so many times and every time seems to be so far apart (usually because it’s between different projects and clients) that I forget the steps required to get things working nicely.

I hope this simple how-to helps you get your project up and running quickly and easily so you can focus on the job at hand; working on developing a (hopefully) cool app or product.