How to Generate an SSH Key on macOS

In this tutorial, you will learn how to generate an SSH key on macOS.

macOS Generate SSH Keys

SSH keys are a secure alternative to passwords when connecting to an SSH server.

These keys come in a public and private pair. The private key is the one you use when connecting to another server. You should never share this key.

A public key, however, is what is used to verify the private key. You will use the public key anywhere where you want your private key to authenticate your client.

The advantage of using SSH keys on your macOS compared with a password is that they are cryptographically secure.

Luckily, generating SSH keys on macOS is a straightforward process. This is largely thanks to SSH being one of the packages installed on Mac by default.

Generating an SSH Key on macOS

Over the next couple of sections, we will walk you through the process of generating an SSH Key Pair on macOS.

Generating the key is a straightforward process. You just need to be fine with having to use the terminal.

Opening the Terminal

1. To generate SSH keys, you must open the terminal on your Mac.

One of the easiest ways to do this is to open up Spotlight by pressing COMMAND + SPACEBAR.

After opening Spotlight, type “terminal” into the search box.

Search for Terminal in Spotlight

2. Spotlight should automatically find the “Terminal” application. Press the ENTER key to open it or clickTerminal” in the list.

Open Terminal using Spotlight

3. You should now have the Terminal open on your macOS device and can safely move on and generate your SSH Keys

Using Terminal on macOS to generate SSH Key

Using the Terminal to Generate an SSH key on macOS

4. Now that we have the terminal open, we can now use the “ssh-keygen” tool to generate SSH keys on macOS.

With this command we are using two options that allow us to control key aspects of the key that is generated.

  • -b: This option allows us to set the size of the SSH key. The larger the key, the longer it will take to generate and authenticate.

    At the time of publishing, a key size of 4096 offers the best balance between security and speed.
  • -t: The “t” option allows us to specify the encryption type. For this key we will be using “rsa” but your server might support different encryption types for SSH connections.

    If you are connecting to a more modern server with your SSH keys, consider using “ed25519” as the type instead. Just remove the “-b” option if you choose to use this new encryption method.
ssh-keygen -b 4096 -t rsa

5. After running the above command, you will be prompted to enter a name for your new SSH keys.

If you don’t have any keys set up previously, it is safe to keep with the defaults and press the ENTER key.

These keys will be saved to the current user’s home directory as “id_rsa” (Private Key) and “id_rsa.pub” (Public Key).

To use a custom name, you must also type in the path. For example, to save the key into your users Documents folder you would use like “~/Documents/private_key“.

 Generating public/private rsa key pair.
Enter file in which to save the key (/Users/emmet/.ssh/id_rsa): 

6. Next, you can set a passphrase for your macOS SSH keys. By setting a passphrase, you will be required to enter it before you can use the key.

A passphrase helps prevent unauthorized access to your private key. However, this password is not required, and you can press ENTER to skip these steps.

Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 

7. Your SSH key has now been successfully generated on your Mac. You will get an output similar to the one below.

This output will show you where your keys have been saved, the key’s fingerprint, and the randomart. A randomart is a way of quickly comparing two keys.

Your identification has been saved in /Users/emmet/.ssh/id_rsa
Your public key has been saved in /Users/emmet/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:+lGd9B/k2xgtTM9tA9SP1xwxqmrYx1bxgLNUKF

The key's randomart image is:
+---[RSA 4096]----+
|     o. ..  =* o+|
|     ..o . =o+*..|
|      +..o..*.+. |
|     . =+.+o B   |
|      +.S+ +E .  |
|       +o . .    |
|     o o.        |
|    . + +        |
|     . o         |
+----[SHA256]-----+

Conclusion

Hopefully at this stage, you will now know how to generate an SSH key on macOS.

These keys are a great way to improve security when connecting to an SSH server. They are more secure thanks to the sheer size of the key and the fact they use cryptography to generate and verify the keys.

Please feel free to comment below if you have any questions about generating SSH keys.

If you found this tutorial to be helpful, we highly recommend checking out our many other macOS tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *