Connecting to github with SSH in macOS Monterey

Checking for existing SSH Keys1 2

1
2
$ ls -al ~/.ssh
# Lists the files in your .ssh directory, if they exist
If you don't already have an SSH key, generating a new SSH key and adding it to the ssh-agent
1
2
3
4
5
6
7
8
cd ~/
mkdir .ssh ; cd .ssh

# Make sure that the file permissions are set to read/write/execute only for the user
chmod go-rwx .ssh

# substituting in your GitHub email address.
ssh-keygen -t ed25519 -C "your_email@example.com"

Check:

1
2
3
4
5
6
ls -al
total 16
drwxr-xr-x 4 luohanjie staff 128 12 13 16:28 .
drwxr-x---+ 28 luohanjie staff 896 12 13 16:25 ..
-rw------- 1 luohanjie staff 411 12 13 16:28 id_ed25519
-rw-r--r-- 1 luohanjie staff 101 12 13 16:28 id_ed25519.pub

Adding your SSH key to the ssh-agent

Start the ssh-agent in the background:

1
2
eval "$(ssh-agent -s)"
Agent pid 2421

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:

1
2
3
4
5
open ~/.ssh/config
The file /Users/luohanjie/.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 to contain the following lines. If your SSH key file has a different name or path than the example code, modify the filename or path to match your current setup.

Note: If you chose not to add a passphrase to your key, you should omit the UseKeychain line.

1
2
3
4
Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519

Add your SSH private key to the ssh-agent and store your passphrase in the keychain. If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_ed25519 in the command with the name of your private key file.

Note: The -K option is Apple's standard version of ssh-add, which stores the passphrase in your keychain for you when you add an SSH key to the ssh-agent. If you chose not to add a passphrase to your key, run the command without the -K option.

If you don't have Apple's standard version installed, you may receive an error. For more information on resolving this error, see "Error: ssh-add: illegal option -- K."

In MacOS Monterey (12.0), the -K and -A flags are deprecated and have been replaced by the --apple-use-keychain and --apple-load-keychain flags, respectively.

1
ssh-add -K ~/.ssh/id_ed25519

Adding a new SSH key to your GitHub account

Copy the SSH public key to your clipboard:

1
pbcopy < ~/.ssh/id_ed25519.pub

In the upper-right corner of any page, click your profile photo, then click Settings.

Setting

In the user settings sidebar, click SSH and GPG keys. Click New SSH key or Add SSH key. Paste your key into the "Key" field. Click Add SSH key. If prompted, confirm your GitHub password.


  1. https://docs.github.com/en/authentication/connecting-to-github-with-ssh↩︎

  2. https://wpbeaches.com/generate-ssh-private-and-public-keys-in-macos-big-sur/↩︎