How to Generate SSH Keys on Ubuntu?


Introduction

In this article, we will focus on how to generate SSH keys on Ubuntu - one of the most popular Linux distributions used by developers worldwide. We will provide step-by-step instructions for generating an SSH key pair using both command line interface (CLI) and GUI tools like FileZilla. The article will also cover common issues that users may encounter when generating or using their SSH keys on Ubuntu along with troubleshooting tips and solutions for each issue.

Whether you're a developer looking for secure access to remote servers or a system administrator tasked with maintaining security at scale, this article is designed to help you generate strong and secure SSH key pairs on Ubuntu. So let's dive in!

The Benefits of Using SSH Keys over Passwords

Using passwords for authentication has always been problematic as they can be easily guessed by hackers if not properly secured. In contrast, SSH keys are much more secure, as they are nearly impossible to crack without access to the private key. Moreover, SSH keys provide a higher level of security than passwords because unlike passwords, SSH keys cannot be guessed or stolen.

Thus, using SSH keys is an effective way to protect your machine and data from unauthorized access. Additionally, with the increasing importance of remote work in today's world, SSH provides a safe and secure way for people to remotely access their machines from anywhere in the world.

Generating an SSH Key Pair on Ubuntu

SSH keys are cryptographic security keys used to authenticate secure connections to remote machines. To generate an SSH key pair on Ubuntu, you can use the command line interface (CLI) tool called 'ssh-keygen'. The ssh-keygen command generates two separate files: a private key and a public key, both of which are used in different ways to enable secure authentication.

To generate an SSH key pair on Ubuntu, first open a terminal window by pressing Ctrl + Alt + T. Then type the following command and press enter −

ssh-keygen 

By default, ssh-keygen generates private and public RSA keys with a key size of 2048 bits. You can also specify alternative options such as DSA or ECDSA keys, different bit sizes for your key length, and adding a passphrase for extra security.

For example, to generate an ECDSA key instead of the default RSA type with size 4096 bits and adding a passphrase for extra security −

ssh-keygen -t ecdsa -b 4096 -C "my-secure-passphrase" 

Once you have entered the desired options, ssh-keygen will prompt you for the location where your new SSH keys should be saved. By default, these files will be saved in the ~/.ssh directory with names id_rsa for your private key and id_rsa.pub for your public one.

Types of Keys

There are several different types of SSH keys available that can be generated using ssh-keygen depending on your requirements −

  • RSA −This is one of the most common types of SSH keys.

    It uses asymmetric encryption methodology which means that it allows both encryption and decryption using separate but mathematically related keys. RSA supports various lengths including 2048-bit or 3072-bit.

  • DSA −Digital Signature Algorithm (DSA) is another type of asymmetric cryptography that can be used for SSH key generation. It is faster than RSA but less secure as well.

    DSA keys are usually shorter and less secure than RSA keys and they are widely used for digital signatures in the US legal system.

  • ECDSA −Elliptic Curve Digital Signature Algorithm (ECDSA) is a newer type of asymmetric cryptography that is faster and more secure than both RSA and DSA keys.

It uses smaller key sizes to achieve a similar level of security, making it more efficient in terms of bandwidth usage. ECDSA keys come in different sizes including 256-bit, 384-bit or 521-bit.

Adding a Passphrase

When generating an SSH key pair on Ubuntu, you have the option to add a passphrase to your private key which adds extra security. A passphrase provides an additional layer of protection against unauthorized access to your private key, meaning if someone gains access to your private key file they cannot use it unless they also know the passphrase.

To add a passphrase while generating an SSH key pair, you simply enter it when prompted by ssh-keygen tool. Alternatively, you can leave the passphrase empty if you don't want this extra protection.

Saving Keys

By default, ssh-keygen saves both public and private keys in ~/.ssh directory with id_rsa as default file names: - `~/.ssh/id_rsa` : Private Key - `~/.ssh/id_rsa.pub`: Public Key

You can change these names with "-f" argument followed by custom file name −

ssh-keygen -t rsa -b 4096 -C "my-secure-passphrase" -f "~/.ssh/my_key" 

This will save two files named my_key (private) and my_key.pub(public) under .ssh directory. Once your keys are generated successfully, next step is to add your public key to a remote server to allow secure authentication.

Adding Your Public Key to a Remote Server

Why You Need to Add Your Public Key

When you generate an SSH key pair, you create both a private and public key. The private key should always be kept secure and should never be shared with anyone.

However, the public key can be shared with remote servers in order to authenticate your connection. When you connect to a server that has been set up for SSH authentication, it will ask for your public key.

If the server recognizes your public key, it will allow you to connect without entering a password. The reason why this method is so popular is because it provides an extra layer of security over traditional password authentication.

Passwords can be stolen or hacked, but it is much more difficult for someone to access your private key file unless they physically have access to the device hosting the file itself. Using an SSH key pair simplifies the process of logging into remote servers while also increasing security.

Step-by-Step Instructions for Adding Your Public Key

Now that we've discussed why adding your public key is important let's take a look at how this can be done using either command line interface (CLI) or GUI tools like FileZilla −

Add Public Key via CLI:

  • Open a terminal on Ubuntu by pressing Ctrl + Alt + T

  • Navigate to where you generated your SSH keys

  • Use the following command syntax −

ssh-copy-id user@remotehost

Add Public Key via FileZilla:

  • Open FileZilla and connect to your remote host.

  • Click Edit > Settings > SFTP.

  • In "SSH Private Keys" click "Add" and browse for the private key file on your computer which matches your public one.

  • Click OK.

After completing either method above, you should now be able to connect to your remote server using SSH authentication with your public key.

Conclusion

In today's world, data security is a crucial aspect of every individual and organization. With an increasing number of cyber-attacks and data breaches, it has become essential to use robust security measures to protect your sensitive data. SSH keys provide a secure way to authenticate yourself when accessing remote servers or applications.

Instead of relying on passwords, which can be stolen, SSH keys use cryptographic algorithms to ensure secure communication between your device and the remote server. SSH keys are an integral part of secure communication protocols, and generating them on Ubuntu is simple and straightforward.

Updated on: 06-Jun-2023

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements