How To Generate SSH Key On Ubuntu Server 24.04?

SSH on Linux distributions is commonly used to connect from other devices remotely to your Linux VPS server. To use the SSH service on your Linux device or your server, you need to create two SSH keys: one public and one private key.

First, you need to connect to your Ubuntu Server With SSH Using PuTTY application if you are using Windows. And if you are using other operating systems, you can connect to your server from their normal terminal.

If you don’t have an Ubuntu VPS for yourself yet, you can buy an Ubuntu VPS and get starting in few minutes. then connect securly using the SSH keys you will generate following this article.

Generate SSH Key on Ubuntu 24.04

To use the SSH connection protocol with key authentication method instead of logging in with username and password every time, and generating SSH keys.

We will use the ssh-keygen command to generate keys. First, make sure you’ve installed OpenSSH correctly.

ssh -V

If it returns your OpenSSH version, it means you’ve installed it successfully and follow the steps below to create keys; otherwise, read the article for Installing OpenSSH on Ubuntu.

Create SSH Key Pair in Ubuntu

SSH keys have many types, including ed25519, which is a modern standard type, and an older and universally compatible type called RSA. You can see commands for creating both of them below:

ed25519 type:

ssh-keygen -t ed25519

RSA type:

ssh-keygen -t rsa -b 4096

Specify SSH Key Path in Ubuntu

After entering the command, it will ask you for a destination for saving the SSH keys. You can specify a destination for it, or you can leave it empty and press Enter to use the default destination, which is recommended.

specifiy destinaion for ssh key in linux terminal

Set Password on SSH Key in Ubuntu

Here in this step, you will be prompted to set a password for your keys. You can leave it empty and press Enter to continue. But it would be more secure if you set a password.

SSH key creation password requirment in linux terminal

Verify SSH Key in Ubuntu Server

After setting the password, you will see information about the generated key, like the fingerprints and random pattern generated for your key, and the location that your files are stored. You can navigate to that location to verify the file creation.

This is the default location where your keys are stored if you don’t change the destination path:

/home/username/.ssh

Open SSH Public Key in Ubuntu

To verify if your SSH keys are generated or not, you can open your SSH public key file and verify if its content is in the correct format or not.

Running this command, you can see the content of your public key file.

cat ~./.ssh/id_ed25519.pub

You will see output like this:

ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJa5yjA7UAGGiMYfbquPRMqY6+3lJZ/9NCxIHm45UOAb root@linux

Login to Linux Server with SSH Key

With the SSH key created, you can connect to other Linux servers from your Linux device, or if you are using a Linux server, you can accept incoming SSH connections with a key request.

For connecting to your Linux server, you need to copy your SSH key to the destination server, which you can do it manualy by copying it to the ~/.ssh/authorized_keys file or you can use this command to automate this proccess.

ssh-copy-id username@server_ip

After running this command, you will be prompted to enter a password. You should enter the passphrase you set when generating your SSH key. This is only required for the first connection.

For future connections, you don’t need to use the command above; you can simply connect using the standard SSH command.

ssh username@server_ip

Common Issues of Creating SSH Key in Ubuntu

These are some issues you may encounter when creating an SSH key on your Ubuntu VPS server. Even small problems can lead to connection or authentication failures.

It is important to follow each step carefully. SSH connections are critical because they are very important for your system security.

ssh-keygen Not Recognized

This is one of the main and common issues you can face when you are using the ssh-keygen command; it says ssh-keygen is not recognized.

For fixing this issue, you need to install OpenSSH on your Ubuntu server to fix it. It is usually not installed on Linux distributions automatically; you need to install it manually.

SSH Folder Permissions in Ubuntu

Sometimes, SSH key creation or usage can fail due to incorrect permissions on the .ssh directory. You need to set the correct permissions on your Linux server. SSH requires strict permissions for security reasons, and if your permissions are open to everyone, it may be ignored.

It’s better to have permissions like:

FilenamePermissionCommand
.ssh DirectoryFull access to root usersudo chmod 700 ~/.ssh
Private KeyRead and Write access to root usersudo chmod 600 ~/.ssh/id_ed25519
Public KeyReadable for all users and modifiable by rootsudo chmod 744 ~/.ssh/id_ed25519.pub

Overwrite Existing SSH Key in Ubuntu

When generating a new SSH key, you will be warned whether you want to overwrite the existing key; this action would cause problems if the previous key was already being used. To avoid this issue, you can back up your SSH keys, or you can take backup of your Linux VPS.

SSH key Firewall Problems in Ubuntu

You may also encounter silent connection issues, where everything is configured correctly, but you still cannot connect. This can happen if your Linux server firewall is blocking SSH traffic, especially on port 22 (and in some cases other ports if they were changed from the default).

You can fix this problem by opening the specified port for SSH on Linux firewall.

Conclusion

SSH is the key connection type to Linux servers, and if you want to make it more secure, you can use SSH keys to avoid using a username and password on every login. We covered how you can create it on your Ubuntu server in this article and explained the common issues you may face.

If you want to connect with SSH key pairs from a Windows device, you need to create SSH keys on Windows, too.


author image

The Author Jacob R

i am Jacob a developer and cybersecurity enthusiast with strong skills in web development and a passion for building practical digital solutions,Working on web development for 4 years. Hope my guides be helpful

More from Jacob R

Post Your Comment

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