Blog

How to Generate an SSH Key on Linux?

Kamil Porembiński Kamil Porembiński
Share

Generating an SSH key on Linux is a straightforward process that secures your SSH connections to servers. This guide will walk you through the steps to create an SSH key pair (public and private) and ensure the security of your private key.

Step 1: Verify OpenSSH Availability

Most Linux distributions (e.g., Ubuntu, Debian, Fedora) come with OpenSSH pre-installed. To check if it’s available:

  1. Open Terminal.
  2. Type: ssh If you see a list of commands and options, OpenSSH is installed and ready to use. If not, you can install it with your package manager, for example: sudo apt update sudo apt install openssh-client

Step 2: Generate the SSH Key

To generate an SSH key, follow these steps:

  1. In the Terminal, enter: ssh-keygen
  2. You’ll be asked where to save the key. By default, it will be saved in: /home/YourUsername/.ssh/id_rsa Press Enter to confirm the default location or provide a custom path if desired.NOTE: If a key already exists, you’ll be asked if you want to overwrite it. To keep the existing key, choose 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

After generating the keys, 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

Your private key should be treated as confidential, and no one else should have access to it. Here’s how to secure it:

  1. Set appropriate permissions: Your 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 Linux is quick and easy, but protecting your private key is essential for secure SSH connections. Keep it secure, use a passphrase, and create 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 Linux.

Contact

Do you have questions? Get in touch with us