mac setupssh keys

How to Generate an SSH Key on macOS

Learn how to generate an SSH key on macOS for secure authentication with GitHub, GitLab, Bitbucket and remote servers. This step-by-step guide covers creating, configuring and verifying your SSH key.

Jul 10, 20262 min readUpdated Jul 17, 2026
How to Generate an SSH Key on macOS

1. Check for existing SSH keys

You should check for existing SSH keys on your MacOS

Open Terminal and run the following:

code
cd ~/.ssh
  • If you see "No such file or directory", then there aren't any existing keys. Go to step 3.
  • Check to see if you have a key already:
code
ls id_*
  • If there are existing keys, you can retrieve the ssh-key via open these files and use them to wherever you want (GitHub, GitLab, BitBucket,...)

2. Back up old SSH keys

In case you've already have the key, but you would like to have a new one instead of use the old one, you should back those up.

Go back to your Terminal, at .ssh folder:

code
mkdir key_backups
cp id_* key_backups

3. Creating a new key

3.1. Open your Terminal and enter the following:

code
ssh-keygen -t ed25519 -C "your_email@example.com"

Note: If you're using a legacy system that doesn't support the ED25519 algorithm, use the below:

code
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Associating the key with your email address helps you to identify the key later on.

You'll see a response similar to this:

code
trung.nguyen@trungtmnguyen ~ % ssh-keygen -t ed25519 -C trungntm.97@gmail.com
Generating public/private ed25519 key pair.
Enter file in which to save the key (/Users/trung.nguyen/.ssh/id_ed25519):

3.2. Just press Enter to accept the default location and file name. If the .ssh directory doesn't exist, the system creates one for you.

3.3. Enter, and re-enter, a passphrase when prompted.

The whole interaction will look similar to this:

code
trung.nguyen@trungtmnguyen ~ % ssh-keygen -t ed25519 -C fperez@email.com
Generating public/private ed25519 key pair.
Enter file in which to save the key (/Users/trung.nguyen/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/trung.nguyen/.ssh/id_ed25519.
Your public key has been saved in /Users/trung.nguyen/.ssh/id_ed25519.pub.
The key fingerprint is:
SHA256:gTVWKbn41z6JgBNu3wYjLC4abcdefghijklmnopqrstuvwxy trungntm.97@gmail.com
The key's randomart image is:
+--[ED25519 256]--+
|==+.    +o..     |
|.oE.   +o..      |
|    . ...o       |
|     .o...       |
|     oo+S  .     |
|  + ..B = . .    |
|.+.+.oo+ * o .   |
|o++.o+  . + +    |
|B+ o.    .   .   |
+----[SHA256]-----+
trung.nguyen@trungtmnguyen ~ %

3.4. You're done. You can use new ssh-key to configure on Git system, or remote server.

References:

Comments

0/2000

Loading comments…

Trung Nguyen

Trung Nguyen

Software engineer building simple, reliable systems. I write about architecture, Java, and things I learn along the way.

More about me