Featured image of post How to Create SSH Key on Ubuntu for Secure Server Access

How to Create SSH Key on Ubuntu for Secure Server Access

Learn how to create an SSH Key on Ubuntu to securely access servers remotely without using passwords. Enhance security with these simple steps.

In this article, we’ll explore how to create an SSH Key on Ubuntu for remote server access. This approach allows us to access the server without a password, significantly enhancing security.

What is SSH?

SSH stands for Secure Shell Protocol, a cryptographic network protocol that enables users to access remote computers securely over the internet.

While SSH supports password-based authentication, using SSH keys is generally recommended. SSH keys provide a more secure method for logging into a Linux server, as they are not vulnerable to brute-force password attacks.

Generating an SSH key on Ubuntu creates two keys: a public key and a private key. The public key can be placed on any server, and you can connect to that server using an SSH client that has access to the private key.

When the public and private keys match, the server grants access without requiring a password. You can further enhance SSH key security by protecting the private key with a passphrase, which is optional but highly recommended.

Steps to Create an SSH Key on Ubuntu

Here are the steps to create an SSH key on Ubuntu:

Generating the Key Pair

The first step to generate an SSH key on Ubuntu is to create a key pair on the client side, which could be your laptop or PC. Type the following command in the terminal:

1
ssh-keygen -t ed25519

This command will produce the following output:

1
Generating public/private ed25519 key pair.

A confirmation screen will appear indicating that the SSH key generation process has started, and you will be prompted for some information, which we will discuss in the next steps.

Specifying the Key Pair Storage Location

The first prompt from the ssh-keygen command will ask where to save the key pair:

1
Enter file in which to save the key (/home/your_username/.ssh/id_ed25519):

You can press ENTER to save the file to the default location in the .ssh directory of your home directory.

Alternatively, you can choose a different file name or location by typing it after the prompt and pressing ENTER.

Creating a Passphrase

The second and final prompt will ask you to enter a passphrase:

1
Enter passphrase (empty for no passphrase):

You can leave the passphrase empty, but it is highly recommended to enter a passphrase to make your key pair more secure.

A passphrase ensures that your key pair cannot be used by unauthorized persons, as you will be prompted to enter the passphrase whenever you use the key pair to access the target server.

The overall output will look like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/your_username/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/your_username/.ssh/id_ed25519
Your public key has been saved in /home/your_username/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:example_key_fingerprint your_username@hostname
The key's randomart image is:
+--[ED25519 256]--+
|      o+o o.o.++ |
|     =oo.+.+.o + |
|     *+.oB.o. o  |
|     *.. + B .   |
|     o. = o S .  |
|    .+ o o . o   |
|    . + . ... .  |
|    . . o. . E   |
|     .. o. . .   |
+----[SHA256]-----+

The public key is now located at /home/your_username/.ssh/id_ed25519.pub, and the private key is located at /home/your_username/.ssh/id_ed25519.

Copying the Public Key to the Server

After successfully creating the key pair, it’s time to place the public key on the server you wish to access.

You can copy the public key to the authorized_keys file on the target server with the following command. Be sure to replace the example username and server address:

1
ssh-copy-id your_username@server_address

With this command, you will be able to log into the server via SSH without using a password. However, if you set a passphrase when creating the SSH key, you will be prompted to enter the passphrase upon login.

Conclusion

Creating an SSH key on Ubuntu is simple and significantly improves the security of your remote server access. By following these steps, you can connect to your server securely without using passwords, reducing the risk of unauthorized access. Adding a passphrase to your private key provides extra protection, ensuring only you can use it to access your server. Implementing SSH keys is essential for maintaining a secure and efficient server management environment.