Public Key Authentication in Linux

Postgraduate in Communications Engineering with working experience in the Support Desk and self-study in software development.
Search for a command to run...

Postgraduate in Communications Engineering with working experience in the Support Desk and self-study in software development.
Cron is a time-based job scheduling daemon found in Unix-like operating systems, including Linux. It enables users to schedule jobs (commands or scripts) to run periodically at fixed times, dates, or intervals. These scheduled jobs are known as cron ...
The git log command is used to view the commit history of a Git repository. You can customize the output format to show specific details in a more readable or structured way. Below are some useful variations and formatting options for git log: To sh...

Overview This article will teach us how to: Install Ubuntu Server 24.04 on a laptop from scratch Install Ubuntu server by using Vagrant tool with ISO image Prerequisites: A laptop without any OS installed; A virtualization software Virtualbox t...

The /etc/passwd and /etc/shadow files are the backbone of Linux user management. Together, they store user account information and handle authentication securely. This article provides a hands-on guide to understanding these files, their structure, a...

In Linux, links are powerful tools that allow you to create references to files and directories. There are two main types of links: hard links and soft links (also known as symbolic links or symlinks). Understanding the differences between these two ...

A hostname is a human-friendly name given to a computer. It is a unique identifier that allows us to identify the machine in various network communications, making it easier to locate and manage devices. Type: hostname To see where it is stored, typ...

The motivation for using public key authentication over simple passwords is security. Public key authentication provides cryptographic strength that even extremely long passwords can not offer. With SSH, public key authentication improves security considerably as it frees the users from remembering complicated passwords (or worse yet, writing them down).
SSH public key authentication relies on asymmetric cryptographic algorithms that generate a pair of separate keys (a key pair), one "private" and the other "public". Keep the private key secret and store it on your local computer to connect to the remote system. Conceivably, share the public key with anyone without compromising the private key; you store it on the remote system in a .ssh/authorized_keys directory.
This hands-on exercise uses public key authentication to connect to the Ubuntu 20.04/Vagrant server by generating an SSH key pair on the local Ubuntu 22.04 machine. The connection between the local machine and the Ubuntu/Vagrant server is established using the bridged mode that allows the two machines to be in the same network.

To use SSH public key authentication:
The remote and local systems must have OpenSSH installed.
You need to be able to transfer your public key to the remote system. Therefore, you must either be able to log into the remote system with an established account username and password/passphrase, or have an administrator on the remote system add the public key to the ~/.ssh/authorized_keys file in your account.
Open the terminal (CTRL+ALT+T).
Verify that the ssh client is installed on the local machine:
which ssh
OR
ssh -V
Check for existing keys by listing the ssh directory with on the local machine:
ls -l ~/.ssh
Generating new keys overwrites the current ones by default. However, indicating a new name for the keys saves them to different files.
If there are no existing keys, the output indicates the directory does not exist.
If some keys are found, they should be backed up before continuing.
Generate a key pair with ssh-keygen:
ssh-keygen -b 4096
ssh-keygen is the tool used to create SSH keys, which are used for secure communication between machines.
-b specifies the number of bits in the key to create. More bits generally mean better security.
4096 is the number of bits in the key. This is considered a good balance between security and performance for most SSH applications.

Verify the key pair exists:

View the public key:

Check whether the server works by connecting to it first:
ssh username@ip_address

Add the public key to the server by copying it manually or using the ssh-copy-id tool:
ssh-copy-id username@ip_address
Public keys are in the ~/.ssh/authorized_keys file and every time a new public is added, it appears as a new one line.

Now, connect to the server from the local machine by using the SSH private key and check whether the public key authentication works:
