Generate an SSH Key on Ubuntu
Learn how to generate an SSH key on your Ubuntu machine, This guide works for both Ubuntu 22.04 and 24.04.
Not using ubuntu 22.04 & 24.04?
In order to connect to your servers using an ssh key, you need to generate a key pair on your local machine first. In this guide, we will see how to generate and secure your ssh key on Ubuntu.
Generate an SSH Key Pair
To generate a key pair, type the following command in your terminal:
ssh-keygen -t ed25519 -a 100
ssh-keygen -t rsa -b 4096 -a 100
The -a 100 option increases the bcrypt KDF rounds, slowing down brute-force attempts if someone gets your encrypted private key. It does nothing if you do not use a passphrase.
You will be asked where to save the key file. Press enter to use the default location.
Enter file in which to save the key (/home/deployer/.ssh/id_ed25519):
You will be prompted to enter a passphrase. It is recommended to use a passphrase, but it is not mandatory. If you choose to use a passphrase, make sure it is a strong one.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your key pair is saved in the ~/.ssh directory: the private key as id_ed25519 or id_rsa, and the public key as id_ed25519.pub or id_rsa.pub, depending on the type generated.
View your Public key
cat ~/.ssh/id_ed25519.pub
cat ~/.ssh/id_rsa.pub
Setting correct permissions
Usually ubuntu sets the right file permissions for generated keys, but if you copied the key from somewhere, or you are not sure, it’s always a good idea to explicitly set the permissions.
chmod 600 ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_rsa
