SSH on Ubuntu Server

Hi All. This will be a short post on a topic that I came across recently, and that is how to enable SSH on Ubuntu Server. The SSH connection is a must when connecting to your server remotely, or it can make some copy and paste instructions much easier to do. So, let us begin.

Prerequisite

In this tutorial, I’m using Ubuntu Server 20.04.2 LTS that is installed on VM with Virtual Box, but this will be valid for dedicated PCs with Ubuntu Server 20.04.

But Why?

Why do we need this tutorial?

By default and because of security you can not log in via SSH only with password phase to Ubuntu Server, you need some SSH key or some other way of authentication. This error is shown in the image below.

Also, check my other post that focuses on connecting to AWS instances from Windows and Linux https://blnlabs.com/how-to-connect-to-aws-ec2-instance-from-windows-10-and-linux/.

Enable password Authentication

We need to edit file sshd_config from /etc/ssh. We want to enable Password Authentication by simply changing the value to yes.

PasswordAuthentication yes

Next, we simply need to restart the SSH service inside Ubuntu Server with the next command:

sudo systemctl restart ssh

Double-check is ssh service up and running with the command:

sudo systemctl status ssh

and you should be able to connect via SSH to your server running Ubuntu Server 20.04 with only your user name and password. As I said, this is not secure and this should be only a temporary solution in order to set up SSH key file authentication. Check out the image below.

Create SSH keys

In order to use SSH keys for authentication, we need to create them. This can be done inside your main PC not Ubuntu Server with the next command:

ssh-keygen -t rsa

Interestingly this command works on Windows and Linux, so just open the terminal emulator and copy and paste the command above. This will generate two keys, private and public. Then, we need to copy the public key to the Ubuntu Server side.

Transfer SSH key to Ubuntu Server

In order to copy the public key to Ubuntu Server, we have multiple methods. There is terminal command scp and it looks something like this:

scp file.txt remote_username@10.10.0.2:/remote/directory

Maybe the better way is to use FileZilla, and it is pretty simple

And now you have a graphical interface and maybe a bit easier way to transfer multiple files, I think that this is the way to go.

Next, place your public key (id_rsa.pub) as /home/{username}/.ssh/authorized_keys file. Now, we can remove Password Authentication (set PasswordAuthentication to no), and reset the ssh service once again.

Final words

And that is it. Now, you have a secure ssh connection to your Ubuntu Server. If you have just a local server I would maybe stop before adding ssh keys and use ssh with just Password Authentication. Did this tutorial help you? Please comment down below and see you in the next one.

Leave a Comment