Set up SSH keys for CLI GitHub on Mac.

haomin
5 min readMar 23, 2021

I haven’t contributed to my GitHub repository for almost a year. Recently I decided to pick up coding and start contributing again. So I did the following:

$ git add .
$ git commit -m "first commit"
$ git push

Push works since I previously set up an HTTPS connection to my repository and have the login info in the CLI. However, within seconds of my last push. I received an email from GitHub:

Deprecation notice from GitHub

Setting up SSH:

Since I use SSH connection to Git at work, so I will also be using SSH for my personal GitHub account. Thus, we will be creating a new key pair. let’s follow this guide from the official GitHub Docs: https://docs.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent

$ ssh-keygen -t ed25519 -C "your_email@example.com"
> Generating public/private ed25519 key pair.
> Enter a file in which to save the key (/Users/you/.ssh/id_ed25519): [Press enter]
// I decided to save it @ /Users/XXX_MY_USER_NAME/.ssh
// Since this is where you typically config ssh
// now continue with the prompt below
// Write down your passphrase
> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]

Remember your passphrase for the keys! After the generation, go to:

$ cd ~/.ssh

You should be able to see the keys you generated via keygen:

You can see the public and private key here

Now we can add the keys to the config file present in the .ssh folder. However, if you do not have the file, you can continue with the docs provided by GitHub:

$ eval "$(ssh-agent -s)"
> Agent pid 59566

And as mentioned in the GitHub docs:

If you’re using macOS Sierra 10.12.2 or later, you will need to modify your ~/.ssh/config file to automatically load keys into the ssh-agent and store passphrases in your keychain.

First, check to see if your ~/.ssh/config file exists in the default location.

$ open ~/.ssh/config
> The file /Users/you/.ssh/config does not exist.

If the file doesn’t exist, create the file.

$ touch ~/.ssh/config

Open your ~/.ssh/config file, then modify the file, replacing ~/.ssh/id_ed25519 if you are not using the default location and name for your id_ed25519 key.

Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519

Since we already have the public and private key in the ~/.ssh location. We just need to upload the public key to GitHub, and then config our ssh config file to use the private key as the IdentifyFile for GitHub.

Adding keys to GitHub:

So first, lets add the public key on GitHub: Following the sample code on GitHub docs

$ pbcopy < ~/.ssh/XXX_YOUR_PUBLIC_KEY.pub
# Copies the contents of the id_ed25519.pub file to your clipboard

Go to:

Go to Settings — pic from GitHub Docs
Click SSH and CPG keys — pic from GitHub Docs
Click the New SSH key option on the right — pic from GitHub Docs
Then just cmd + v to paste the copied key into the text box here — pic from GitHub Docs

And then click the green Add SSH key button, now you have your public key added.

Now we config our private key in ~/.ssh/config file:

$ cd ~/.ssh/
$ vim config
You will need to add the Host github.com, and it’s Identity file

Once you open the config file, you can add the Host info and identity file for the GitHub connection. The identity file is your private key.

After this configuration is complete. You can use the SSH key to access GitHub repositories.

Remember to re-clone your repository, since it is originally cloned with an HTTPS connection:

// First go to your work repos folder
$ cd ~/your_git_repo_folders/
// Delete it, but remember to push your work to remote
$ rm -rf your_old_repo
Re-clone the code via SSH

Now copy this SSH IP then execute this command:

// First go to your work repos folder
$ cd ~/your_git_repo_folders/
git clone git@github.com:XXXX

There should be a prompt asking you to input your Passphrase for your key, now use the passphrase you previously created for the keys here:

Cloning the GitHub repository

With that, you should be all set. Just in case something went wrong, you can follow the step below to verify if the key was used:

GitHub -> Top right click your profile pic -> settings -> SSH and GPG keys -> Checkout your keys on the right.

SSH key access management and history

You should be able to see the green text saying that your key has recently been used.

Some issues:

[1] Some times when you initially clone the repo: you could encounter authenticity of host ‘github.com can’t be established issue. To fix this, you can use the following command to reslove it:

ssh-keyscan github.com >> ~/.ssh/known_hosts

source: https://github.com/ome/devspace/issues/38

Source:

Source [1] : https://docs.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent

Source [2]: https://docs.github.com/en/github/authenticating-to-github/adding-a-new-ssh-key-to-your-github-account

--

--