How to generate a SSH key on Mac and use it for GitHub
You want to step up your security and use SSH keys? That's a good choice and it's quite easy to do. Learn how to create/generate and use a SSH key to authenticate with GitHub today!
You want to step up your security and start using SSH keys for GitHub? Firstly that’s a good choice and secondly they’re very easy to use, maybe even more convenient than plain old passwords. SSH keys are very easy to setup and in this short tutorial I’ll show you how:
First open up the terminal and use ssh-keygen
to generate the key itself which will prompt you for a save location as well as a password. If this is the first key you generate, the default save location will be fine. A password is optional.
This will generate two keys: One is the public key (default name: id_rsa.pub
) which you will share with people or services that want to verify your identity while the second one (id_rsa
) is your private key and should absolutely never be shared. This would be the same as giving away your password.
If you want to learn more about public key cryptography, I highly recommending reading up on how it works. Cloudflare gives a very good description (How does public key cryptography work? | Cloudflare)
You should now have two keys in the ~/.ssh
directory:
$ ls ~/.ssh
id_rsa id_rsa.pub
Now that your keys are generated, you still need to upload your public key to GitHub.
- You can either navigate there manually by clicking your profile picture in the top right, going to settings and clicking on "SSH and GPG keys" or go there directly (GitHub Settings Keys).
- Click on "New SSH key", give it a title, select "Authentication Key" and paste your public key.
- Finally click on "Add SSH key". Now you should see your key in the overview and are done. It was quite easy, wasn’t it?
One more thing: To actually use your key, you need clone repositories using SSH ([email protected]:…
) instead of HTTP or if you already have local repos, you can edit the origin to be SSH:
git remote set-url origin [email protected]:user/repo.git
.