How to generate an SSH key pair in Linux?

You’ll often find that you’ll need SSH keys to log in into a server or your cloud provider’s machine, etc.

SSH keys are a more secure way to log in over the standard username/password authentication.

To generate a pair of SSH keys yourself while using Linux, you can use the ssh-keygen command.

Follow these steps to generate an SSH key pair

1. Open up the Terminal

2. Type the command

ssh-keygen -t rsa -b 4096

The above command will make sure you generate RSA keys, and with 4096 bit strength. It’s not that standard 2048-bit keys are useless, but 4096-bit keys are a little secure nowadays.

3. You will be prompted to type a location and/or a name for the key. Pick something descriptive.

4. Now you need to set up a passphrase. While you can skip this and leave it empty, I strongly recommend you set up a good passphrase and that you don’t lose it.

After this, two files will be generated. On fedora 35 for example, they got generated in the Home directory when at step 3 I only typed the name.

The file with .pub extension is the public key, that you will need to upload or enter in various locations. (ex: when you’re creating a new Hetzner Cloud machine)

Getting a ‘command not found’ error?

Just install openssh. Use the appropiate command based on your Distro:

  • Fedora: dnf install openssh
  • CentOS: yum install openssh
  • Ubuntu: apt-get install openssh-client
  • Debian: apt-get install openssh-client
  • Arch Linux: pacman -S openssh
  • Kali Linux: apt-get install openssh-client
  • OS X: brew install openssh

And that’s about it!

In case you want to do the same in Windows, check this article out!