generate ssh keys on Ubuntu
How To

Generate SSH keys on Ubuntu to Connect to Remote Systems

Sagar Sharma
Sagar Sharma

Table of Contents

SSH is the go-to tool for connecting to a remote Linux system.

The remote Linux server (usually) has SSH configured to accept connections from other machines.

But you also need SSH on the remote systems from where you are connecting to the server. They are called client machines.

Ubuntu 18.04 Review of New Features
Ubuntu 18.04 Review of New Features

If you use Ubuntu and want to connect to remote Linux servers, you must generate SSH keys first.

In this tutorial, I will explain the following:

  • How to generate SSH keys on Ubuntu
  • How to use the SSH keys to connect to a remote server
  • How to copy the generated keys to the remote server for password-less connection (optional)

So let's start with the first one.

Generate SSH keys on Ubuntu

To generate keys, all you have to do is execute the following command:

ssh-keygen -t rsa

Here, the -t rsa is used to specify which type of keys to generate. Here, I went with the rsa as it is a widely used form.

Once you execute the above command, it will ask you the following:

  • Where to save the keys (hit enter to select the default location)
  • If you already have keys, would you like to overwrite them?
Generate ssh keys in ubuntu

Once done, it will show the location of the generated public and private keys. And if you went with the defaults, it must be stored inside ~/.ssh.

And if you're curious, you can list the contents of the .ssh directory using the ls command as shown:

ls -l ~/.ssh
directory where the ssh keys are stored
đź’ˇ
SSH keys are generated in a pair of public and private keys. You can share the public key but you must never share the private key.

Connect to remote server using SSH

Now that you have successfully generated the SSH keys, you can try connecting to the remote server.

đź“‹
You need to know the username and password of an account on the remote Linux server. You must also know the server's IP address to connect to it via SSH.

You can use the SSH command like this:

ssh username@server-ip-address

When asked, you must provide the password of the user account you are using. As you can see, only people who know the password are allowed to enter the remote system.

Let's say, you found out that the IP address of the server is 192.168.122.159 and you know the root password of this system. Here's what you should use.

ssh [email protected]

You can exit the remote server and close the SSH session using the exit command:

exit
log in as a root via ssh in ubuntu

You enter the password when asked.

đź’ˇ
You probably already know that when you enter password in Linux terminal, nothing is displayed on the screen. You type the password and press enter blindly.

This is good. But there is one problem here. You need to enter the password every time you use SSH. If you find it annoying, you can opt for password-less SSH connection. Let me show you how.

Copy SSH keys to the remote server for passwordless connection

In this method, you save your public key in the list of authorized keys on the remote server. This way, the server will trust the connection from your client machine and won't ask for the password.

To copy the SSH public key to remote server, you can use the ssh-copy-id command in the following manner:

ssh-copy-id username@server_IP

In my case, the username is sagar and the IP is 192.168.122.159 so my command would look like this:

ssh-copy-id [email protected]
copy ssh key to remote server

And there you have it!

From now on, you can connect to your remote server using the ssh command without the password:

ssh username@IP-of-server

Pretty cool. Isn't it?

I hope this helps you in generating SSH keys on Ubuntu and utilizing them for remote connections.

If you have questions, please let me know in the comments.



Sagar Sharma

Sagar Sharma

A software engineer who loves to tinker with hardware till it gets crashed. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.