Mastodon

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!

How to generate a SSH key on Mac and use it for GitHub
Using SSH keys with GitHub is quite easy and better than a plain password (Unsplash)

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.

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/macjournalist/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/macjournalist/.ssh/id_rsa
Your public key has been saved in /Users/macjournalist/.ssh/id_rsa.pub

Generating a new SSH using bash

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.

  1. 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).
Screenshot showing the GitHub SSH key overview
Step 1: Github SSH Key Overview
  1. Click on "New SSH key", give it a title, select "Authentication Key" and paste your public key.
Screenshot showing adding a SSH key to GitHub
Step 2: Add SSH Key to GitHub
  1. 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?
Screenshot showing the GitHub SSH key management where a SSH key is registered
Step 3: Your key should be visible in the overview

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.