Blog

How to Generate an SSH Key on macOS?

Kamil Porembiński Kamil Porembiński
Share

Generating an SSH key on macOS is quick and straightforward, helping to secure your SSH connections to servers. This guide will walk you through the steps to create an SSH key pair (public and private) and how to keep your private key safe.

Step 1: Check for OpenSSH Availability

macOS comes with a built-in OpenSSH tool, so you can begin generating your key right away. To check that OpenSSH is available:

  1. Open Terminal (find it in Finder under Applications > Utilities or press Cmd + Space and type “Terminal”).
  2. In the Terminal, enter: ssh If you see a list of commands and options, OpenSSH is installed and ready to use.

Step 2: Generate the SSH Key

To generate an SSH key, follow these steps:

  1. In Terminal, enter the command: ssh-keygen
  2. You’ll be prompted to enter a location to save the key. By default, the key will be saved in: /Users/YourUsername/.ssh/id_rsa Press Enter to confirm this location, or specify a different path if desired.NOTE: If a key already exists, you’ll be asked if you want to overwrite it. To keep the existing key, specify a different filename.
  3. You will then be prompted to set a passphrase for the key. Adding a passphrase is recommended as it adds an extra layer of security. Even if someone obtains your private key, they’ll need the passphrase to use it.

Step 3: Public and Private Keys

Once the key generation is complete, you’ll find two files in the .ssh directory:

  • id_rsa – the private key, which MUST be kept secure and never shared.
  • id_rsa.pub – the public key, which can be used for authentication.

Step 4: Protect Your Private Key

The private key should be treated as confidential, and no one else should have access to it. Here’s how to ensure its security:

  1. Set appropriate permissions: The private key should only be readable by you. To ensure this, run the following command in Terminal: chmod 600 ~/.ssh/id_rsa This command sets permissions so only you can read and write to the key file.
  2. Never share your private key: Think of your private key as your digital “ID card” for secure connections. Never send it by email or upload it to the cloud. If the private key is compromised, your server or account access may be at risk.
  3. Use a passphrase: If you didn’t set a passphrase when generating the key, consider generating a new key with one. A passphrase provides an additional layer of protection – even if someone acquires your key, they’ll need the passphrase to use it.
  4. Create a backup: A backup of your private key, stored securely (for example, on an encrypted USB drive), ensures you don’t lose access if the original key is misplaced.

Summary

Generating an SSH key on macOS is fast and simple. However, safeguarding your private key is crucial to maintaining secure SSH connections. Keep it secure, use a passphrase, and make regular backups. With these steps, your connections will be safe and reliable.

We hope this guide has helped you understand the process of generating and protecting an SSH key on macOS.

Contact

Do you have questions? Get in touch with us