How to set up SSH on your machine from scratch and configure SSH access on your GitHub account. So you can do a clone, push and pull action using SSH repo URLs.

This guide can be used for BitBucket or GitLab too, if you follow the interface on those platforms.

Why

You need access to a repo to do clone, push, or pull it.

  • For the HTTP URL of a private repos or altering a public repo, you would have to enter your GitHub password each time and also your OTP if you use that. The downside is that this password can be used on other machines (such as by a stranger). And you have to enter your password each time (unless you opt to have it remembered for say 5 min). e.g.
      $ git clone https://github.com/MichaelCurrin/code-cookbook.git
    
  • If you set up GitHub SSH access and use an SSH URL instead, you’ll have something more secure as they key will only work on your machine (provided you don’t share your private key on other machines). e.g.
      $ git clone git@github.com:MichaelCurrin/code-cookbook.git
    

So using an HTTP URL is less secure as it needs a password or token. Using an SSH URL means you allow an SSH key-pair for a specific device.

Steps

For Linux or macOS.

1. Generate keys

  1. Follow the Install SSH recipe to install SSH tool on your machine.
  2. Follow the SSH keys recipe to generate a pair of SSH keys.

2. View keys

View your public key - the one with .pub extension. Copy it, as you’ll need to paste it in the next section.

$ view ~/.ssh/id_rsa.pub

You will also have a private key as ~/.ssh/id_rsa, though you don’t need to access this directly and this should never be shared.

3. Add public key to GitHub

  1. Login to GitHub account.
  2. Go to SSH keys page
  3. Create a new SSH key.
  4. Give a title as description of your name (like Michael or mcurrin) and host (like Mac or Dell). e.g. Michael Dell. This can be renamed later easily.
  5. Paste the public key copied from the previous section and save.

That is based on this tutorial in the GitHub docs.

4. Use it

  1. Go to a repo on GitHub.
  2. Find the clone URL on the repo. Make sure to pick the SSH option.
  3. Clone the URL locally. e.g.
     $ git clone git@github.com:MichaelCurrin/code-cookbook.git
    

Use multiple SSH keys

Note each GitHub account must have its own SSH key. I tried using the same SSH key on two GitHub accounts and got an error that it was still in use.

If you want to start using a second GitHub account, do the following:

  1. Generate a new SSH key pair. Use the same command as before in SSH keys but follow the prompts to choose a new name e.g. id_rsa_abc.
  2. Copy the value of the public key.
  3. Add the public key to your GitHub account.
  4. Configure Git to use the new key.
     Host github.com
         IdentityFile ~/.ssh/id_rsa_abc
    
  5. Run clone or other Git commands.

This will stop using the old one until you switch it back. Alternatively you can try Add key to SSH agent in SSH keys guide and see if you can get the agent to recognize both keys but that might not be possible. Unless maybe the HostName or Host are setup for the org e.g. github.com/my-org.