How to Generate an SSH Key on Mac

Create an ed25519 SSH key pair in Terminal, copy the public key, and add it to GitHub so you can push and pull without typing a password.

GitHub, a remote server, or a deployment tool wants an SSH key. You generate a key pair on your Mac, keep the private half, and give the public half to the service. Everything happens in Terminal and takes a few minutes.

Check for an existing key

Open Terminal from Applications, Utilities and run:

ls ~/.ssh

If you see id_ed25519 and id_ed25519.pub, a key already exists and you can skip to copying the public key. If the folder is empty or does not exist, continue.

Generate the key

Run the following, using the email address tied to your GitHub account:

ssh-keygen -t ed25519 -C "your@email.com"

Press Return to accept the default file location, ~/.ssh/id_ed25519. Enter a passphrase when asked, or press Return twice to leave it empty. A passphrase protects the key if someone gets hold of your Mac. The command creates two files: the private key with no extension, and the public key ending in .pub. Never share the private one.

Copy the public key

Copy the public key to the clipboard:

pbcopy < ~/.ssh/id_ed25519.pub

Nothing prints. The clipboard now holds one line that starts with ssh-ed25519 and ends with your email.

Add the key to GitHub

In a browser, open GitHub and go to Settings, SSH and GPG keys, then click New SSH key. Give it a title that identifies the Mac, leave the key type as Authentication Key, paste into the Key field, and click Add SSH key. Confirm your GitHub password if prompted.

Test the connection

Back in Terminal, run:

ssh -T git@github.com

The first time, type yes to trust GitHub's host key. A reply that greets you by username and says you have successfully authenticated means the key works, even though it also says GitHub does not provide shell access. Clone repositories with their SSH address, the one starting with git@github.com:, and pushes will no longer ask for a password.

If you set a passphrase and are tired of typing it. Store it in the macOS keychain once and it is remembered across restarts:

ssh-add --apple-use-keychain ~/.ssh/id_ed25519

If the test says "Permission denied (publickey)". The key on GitHub does not match the one on the Mac. Run the pbcopy command again, delete the key on GitHub, and add it fresh. Also confirm you are testing with git@github.com, not your own username.

More Mac how-tos