Introduction

Ubuntu 20.04 Desktop does not include an SSH server preinstalled. In this article, I show how I install OpenSSH, create an SSH keypair, install the keypair Ubuntu for a user and disable password-based SSH logins. I also include a PowerShell script to mimic the base features of ssh-copy-id. This article also applies to configuring OpenSSH that is preinstalled on cloud servers from AWS, Azure, Google, and probably most vendors.

There are many articles on the Internet about installing and setting up OpenSSH. This article documents my steps for my own documentation purposes. I am currently writing a series of articles on Laravel 9 with a focus on storage and mail. My objective is to create recipes for installing and setting up everything related to Laravel 9, PHP 8.1, and Ubuntu running in a Hyper-V VM and in the cloud on Azure and Google Cloud VMs.

My environment:

  • Windows 10 Professional for development. This system is the SSH client connecting to Ubuntu running OpenSSH.
  • Hyper-V virtual machine running Ubuntu 20.04 Desktop.
  • Hyper-V virtual machine running Ubuntu 20.04 Server.
  • Azure Virtual Machine running the image Canonical:0001-com-ubuntu-server-focal:20_04-lts-gen2:latest.
  • Google Cloud Compute Engine running the image ubuntu-2004-lts.

Update the Operating System

After updating the operating system, I always reboot the OS. I am not sure if this is really necessary, but I want to ensure that all processes are using the latest code.

Install OpenSSH Server

Install OpenSSH server:

Verify that the OpenSSH server is now running:

Determine Ubuntu’s IP address

For my setup, the IP address is 192.168.10.149.

SSH Connect Test

Test connecting to Ubuntu using SSH. I set up Ubuntu with the username jhanley. Update the ssh command with your username and IP address.

For my Ubuntu installation, the UFW firewall is not active. You may need to allow the SSH port:

Setup SSH Keypair Authentication

I recommend switching from password-based logins to SSH keypairs. Digital Ocean wrote a nice article on this topic. I used the following steps on my Windows 10 desktop.

Create an SSH keypair:

Copy/Install the SSH keypair 

Windows 10 does not have the equivalent of the Linux ssh-copy-id command. I wrote a PowerShell script that installs the SSH public key onto Linux. Create a file named ssh-copy-id.ps1 and enter the following script.

I also wrap the PowerShell script with a batch script:

Execute the PowerShell script to copy the SSH public key to the user’s .ssh/authorized_keys file.

Verify that the SSH login now works using a keypair.

Setup .ssh/config

The CLI command ssh supports reading host authentication information in the file ~/.ssh/config.

Edit or create the file config in the .ssh directory. For my Ubuntu desktop configure above, add the following section:

Now I can connect to the Ubuntu system with a simplified command:

Disable Password-Based SSH Authentication

Before completing this step, make sure you can log in using your SSH keypair.

Edit the file /etc/ssh/sshd_config with elevated permissions (sudo). Change the following line to be:

Reload the OpenSSH server:

Verify that the OpenSSH server is running without errors:

As an extra security measure, I reboot the OS to ensure existing connections are terminated.

Another method to terminate all SSH sessions without rebooting:

Additional Information

Summary

Installing and setting up OpenSSH on Ubuntu 20.04 Desktop is straightforward. Switching from password to SSH keypair-based logins improves security. Since Windows 10 does not have the program ssh-copy-id, I wrote my own PowerShell script to duplicate the features I require.

Photography Credits

I write free articles about technology. Recently, I learned about Pexels.com which provides free images. The image in this article is courtesy of Pixabay at Pexels.