GitHub SSH access
Set up SSH keys on your machine and GitHub profile
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.
Related
- GitHub cheatsheet in my Dev Cheatsheets.
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
- Follow the Install SSH recipe to install SSH tool on your machine.
- 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
- Login to GitHub account.
- Go to SSH keys page
- Click to go to github.com/settings/keys
- Or with UI:
- Go Settings.
- Go to SSH and GPG keys.
- Create a new SSH key.
- Give a title as description of your name (like
Michael
ormcurrin
) and host (likeMac
orDell
). e.g.Michael Dell
. This can be renamed later easily. - Paste the public key copied from the previous section and save.
That is based on this tutorial in the GitHub docs.
4. Use it
- Go to a repo on GitHub.
- Find the clone URL on the repo. Make sure to pick the SSH option.
- 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:
- 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
. - Copy the value of the public key.
- Add the public key to your GitHub account.
- Configure Git to use the new key.
Host github.com IdentityFile ~/.ssh/id_rsa_abc
- 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
.