Amazon.co.uk Widgets

Log in

X

Using an SSH key, you can connect and authenticate to remote git servers without supplying your username and personal access token at each visit. Inevitably, you'll end up using more than one provider, for example for public projects and for internal private one, or for a particular client who requires a particular Git repository provider be used. SSh keys are more than just a convenient reduction in admin. They are essential in a devops environment where scripts and hooks will be working with code and multiple accounts and servers.

It is surprisingly easy to set up SSH access to Git repositories, but there are important things to think through which means you need to think about what you are doing. Most providers ofhave good instructions, and you could do no better than to start by reading one, such as this page Connecting to GitHub with SSH. The principles will be the same whatever the Git repository hosting provider.

TL:DR — SSH access to multiple Git repositories is easy to set up, convenient and secure but be careful not to accidentally overwrite other existing keys.

Create an SSH Config file — Step by Step instructions

  1. Open a terminal (this example was in Ubuntu Linux).
  2. Navigate to the .ssh hidden folder in your home directory
    $ cd .ssh
  3. Create or Edit the config file
    $ vi config
  4. Set out your different Git providers in the config file. I use both 'Assembla' and 'GitHub' for Git repository hosting. Replace the HostName and User with the right ones for your provider. I use a meaningful key file filename as these can get confusing quickly.
    Host git.assembla.com
     HostName git.assembla.com
     User Git
     IdentityFile ~/.ssh/id_rsa_username_domain_for_assembla
    
    Host github.com
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_rsa_username_domain_for_github
    
    So for this example if my username was harry and my domain name was example.com my key for github would be called id_rsa_harry_example_for_github

Create the SSH keys — Step by Step instructions

  1. Use ssh-keygen to create a new key associated with your email adddress used with the Git repository you are using. This example is for GitHub. Make sure you use the filename you used in your config file earlier. Dont use the defaults, as you could easily overwrite an existing key file. Remember, you are working in a hidden folder, the convention is that you know what you are doing here!
     $ ssh-keygen -t ed25519 -C "This email address is being protected from spambots. You need JavaScript enabled to view it."
    Generating public/private ed25519 key pair.
    Enter file in which to save the key (/home/me/.ssh/id_ed25519): id_rsa_harry_example_for_github
    Enter passphrase (empty for no passphrase): 
    Enter same passphrase again: 
    Your identification has been saved in id_rsa_harry_example_for_github
    Your public key has been saved in id_rsa_harry_example_for_github.pub
  2. Add they key to your user account.
    $ ssh-add ~/.ssh/id_rsa_harry_example_for_github
  3. To use your new SSH key, you'll also need to add it to your Git repository account. In Github, click your profile photo, then click 'Settings', then click SSH and GPG keys and then click New SSH key.
  4. Copy the ssh key to the clipboard
    $ cat id_rsa_harry_example_for_github
    for example copy it from the terminal to the clipboard after listing the file with cat
  5. Paste it into GitHub 'key' field and give it a sensible name in the 'title' field.
  6. Save and thats it done.

Now you can use git commands against this repository without usernames and passwords. They key will be used instead.