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
- How to AirDrop from iPhone to Mac
- How to Back Up a Mac with Time Machine
- How to Change the Default App for a File Type on Mac
- How to Change the Default Browser on Mac
- How to Check Memory Usage on Mac
- How to Check Which macOS Version You Have
- How to Connect AirPods to a Mac
- How to Find Your IP Address on Mac
- How to Flush the DNS Cache on Mac
- How to Force Quit an App on Mac
- How to Free Up Storage on Mac
- How to Record Your Screen on Mac
- How to Rename Multiple Files at Once on Mac
- How to Show Battery Percentage on Mac
- How to Show Hidden Files on Mac
- How to Take a Screenshot of Part of the Screen on Mac
- How to Uninstall an App on Mac
- How to Use Split Screen on Mac
- How to Zip and Unzip Files on Mac
- How to Check If a Port Is in Use on Mac