Generating and Working with SSH Keys on Ubuntu 20.04

This article demonstrates How to Generating and use SSH Keys on Ubuntu 20.04

SSH Keys on Ubuntu [20.04]

1. The first step is generating the RSA Keypair on your workstation.

ssh-keygen -t rsa -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_rsa
Your public key has been saved in /home/user/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:zxczxczxczxczxczxczxczxczxczxc user@user_server
The key's randomart image is:
+---[RSA 4096]----+
| .o.oo==*oo.|
| .o o =o+o=+=|
| .o.+ o ..o.*+|
| o..o . . o.o|
| +. C o|
| o= . |
| K..o |
| ++.. |
| o+=o |
+----[SHA256]-----+
ll .ssh
total 8.0K
-rw------- 1 0 Nov 25 03:02 authorized_keys
-rw------- 1 3.4K Nov 29 09:03 id_rsa
-rw-r--r-- 1 756 Nov 29 09:03 id_rsa.pub
  • id_rsa.pub: The associated public key. This can be shared.

2. Copying your Public SSH Key to a Server

As a next step, we have to copy the public key to the remote server which we like to access via ssh. Here we create a key file inside the remote host and copy the public key content to that file.

cat ~/.ssh/id_rsa.pub | ssh user@remote_host "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
The authenticity of host 'xx.xxx.xx.xxx (xx.xxx.xx.xxx)' can't be established.
ECDSA key fingerprint is SHA256:asdasdasdasdasdasdasdasdasd.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'xx.xxx.xx.xxx' (ECDSA) to the list of known hosts.
user@xx.xxx.xx.xxx's password:
#on remote host
chmod 700 .ssh
chmod 600 .ssh/authorized_keys

3. Verify the remote access via SSH

Now we have to verify the ssh connection to the remote host without a password. We run the simple command to list down folder content inside the remote host

 Cheersssh user@xx.xxx.xx.xx ls

Technically savvy, results-driven and goal-focused professional with years of experience, abilities and skill as a technical leader.