Skip to content

TechDirectArchive

Hands-on IT, Cloud, Security & DevOps Insights

  • Home
  • About
  • Advertise With US
  • Reviews
  • Contact
  • Toggle search form
Home » Linux » How to perform SSH key-based authentication in Linux
  • openstack ola
    Openstack Deployment with Devstack Linux
  • images 5
    How to Set Up a WatchGuard XTM and Access WSM Network | Monitoring
  • changepsswd
    How to change your Windows Computer login Password Windows
  • Join Synology NAS to Active Directory Domain and Create New Users
    Create New Users and Join Synology NAS to Active Directory Reviews
  • OpenSSL on Windows
    How to Install OpenSSL on Windows Computers Windows
  • Enale FIPS compliance mode on Windows
    How to enable FIPS mode on Windows Server Oracle/MSSQL/MySQL
  • Norton Antivirus Free Download For Mac 1st
    How to fix repeated app-blocking connection alerts from Norton on Mac Mac
  • Slide2 1
    Create and Delete AD DS Partition with NTDSUTIL.EXE Windows Server

How to perform SSH key-based authentication in Linux

Posted on 04/10/202006/09/2023 Christian By Christian No Comments on How to perform SSH key-based authentication in Linux
SSH key pair

SSH stands for Secure Shell and it is used to securely communicate between two host machines over an insecure network. It can be used for file transfer, accessing a remote machine application tunneling. Basically, SSH is the Linux equivalent for Windows Remote Desktop Protocol.

For SSH to work, you need to have Openssh installed on both the host machine as well as the remote machine. Openssh is normally installed by default on most Linux machines and if it is not on your machine, the package is included in your local repository. If that too, isn’t the case, just download the rpm file and have it installed on both systems.

PLEASE NOTE: As a rule of thumb, do not generate your ssh key as the root user because if you do as the root user, only the root user would be able to use that key.

Off we go…


Here are my system credentials:
– The remote machine (note the i.p address and the user name)

Linux authentication

The host machine (note the i.p address and the user name)

Public key authentication

On the remote machine, I have created a file called testfile for the purpose of this demonstration

Secure Shell access

GENERATE KEY PAIR IN THE HOST MACHINE


On the host machines’ terminal, type ssh-keygen and hit the Enter button. The outcome of this command will look the image below:

SSH key pair

By default, the ssh key is stored in the ~/.ssh directory and it is saved in a file called id_rsa. If you wish to change the file name, enter the name you want at the prompt and hit the Enter. Now, to perform SSH key-based authentication in Linux, it will prompt you for a passphrase/password. If you do not want to put a password, just ignore the prompt and hit the Enter button.

And as easy as that, you have successfully generated and stored your ssh key.

2. COPY THE KEY TO THE REMOTE MACHINE
Now copy the ssh key i.d to the remote machine with the command:

ssh-copy-id [email protected]
Linux authentication

For the first time you try to connect to the remote machine, it will ask if you are sure you want to proceed; type yes at the prompt.

If you have done everything well, you should get a confirmation message like the image below.

Secure Shell access

3. LOG INTO REMOTE MACHINE


At the terminal of the host machine, use the following command to log in remotely:

ssh [email protected]

After you hit the Enter button, it should bring an authentication page as shown below

Linux authentication

Simply input the password/passphrase you used in creating the ssh key and you will have logged in successfully into the remote machine.

Well, the next two images show the testfile I created for this demonstration and its content.

Public key authentication
Secure Shell access

When you are done on the remote machine, just type exit at the terminal and you will be logged out of the remote machine and into your host machine.

SSHD CONFIGURATION FILE

Furthermore, the SSH server listens on port 22. In this section, I will show you how to change that to perform SSH key-based authentication in Linux. The configuration file for the SSH Daemon is found in /etc/ssh/sshd_config, and a cat command should reveal something like the image below:

Now, this is a very long file and this is just a small portion of it.

Open this configuration file with your text editor and look for the line #Port 22. Below that line, add the Port number you want for the SSH service to listen on to perform SSH key-based authentication in Linux.

In addition, Now restart the sshd server with the systemctl command

However, if you have SELinux and firewall set up and running, you will get a permission denied error when you try to run any of the SSH services. Moreover, I don’t have them configured, but I will help you with the command to perform SSH key-based authentication in Linux.

  1. To change the selinux label :
semanage port -a -t ssh_port_t -p tcp portnumber

for my demonstration, it will be :

semanage port –a –t ssh_port_t tcp 60122
  • To open the firewall to the new port

firewall-cmd –add-port=60122/tcp        [hit Enter]

firewall-cmd –add-port=60122/tcp –permanent   [hit Enter] 

CONCLUSION
On a parting note; from the configuration file, you can make other changes such as:

  1. However, Allow access to root user account
  2. Add other users access to the ssh key
  3. Add listening address e.t.c

I hope you have found this tutorial on how to perform SSH key-based authentication in Linux helpful and easy to understand.

Rate this post

Thank you for reading this post. Kindly share it with others.

  • Click to share on X (Opens in new window) X
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to share on Tumblr (Opens in new window) Tumblr
  • Click to share on Telegram (Opens in new window) Telegram
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on Bluesky (Opens in new window) Bluesky
  • Click to share on Threads (Opens in new window) Threads
  • Click to share on Nextdoor (Opens in new window) Nextdoor
Linux Tags:SSH

Post navigation

Previous Post: Unable to execute: The application GUID not found in the application list
Next Post: How to automatically resolve errors associated with failed dependencies

Related Posts

  • teams feature
    How to install and configure Microsoft Teams on Ubuntu Linux
  • Screenshot 2020 05 14 at 19.28.50
    How to install and configure Rancid Linux
  • Synergy software kvm
    Why Software KVMs such as Synergy is replacing Hardware KVMs Linux
  • zabbix
    Monitoring services using Zabbix Linux
  • ClamAV
    How to install and manage ClamAV and ClamTK on Ubuntu Linux Anti-Virus Solution
  • Security enhanced linux
    How to setup SELinux on a Linux server Linux

More Related Articles

teams feature How to install and configure Microsoft Teams on Ubuntu Linux
Screenshot 2020 05 14 at 19.28.50 How to install and configure Rancid Linux
Synergy software kvm Why Software KVMs such as Synergy is replacing Hardware KVMs Linux
zabbix Monitoring services using Zabbix Linux
ClamAV How to install and manage ClamAV and ClamTK on Ubuntu Linux Anti-Virus Solution
Security enhanced linux How to setup SELinux on a Linux server Linux

Leave a Reply Cancel reply

You must be logged in to post a comment.

Microsoft MVP

VEEAMLEGEND

vexpert-badge-stars-5

Virtual Background

GoogleNews

Categories

veeaam100

sysadmin top30a

  • openstack ola
    Openstack Deployment with Devstack Linux
  • images 5
    How to Set Up a WatchGuard XTM and Access WSM Network | Monitoring
  • changepsswd
    How to change your Windows Computer login Password Windows
  • Join Synology NAS to Active Directory Domain and Create New Users
    Create New Users and Join Synology NAS to Active Directory Reviews
  • OpenSSL on Windows
    How to Install OpenSSL on Windows Computers Windows
  • Enale FIPS compliance mode on Windows
    How to enable FIPS mode on Windows Server Oracle/MSSQL/MySQL
  • Norton Antivirus Free Download For Mac 1st
    How to fix repeated app-blocking connection alerts from Norton on Mac Mac
  • Slide2 1
    Create and Delete AD DS Partition with NTDSUTIL.EXE Windows Server

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 1,836 other subscribers
  • RSS - Posts
  • RSS - Comments
  • About
  • Authors
  • Write for us
  • Advertise with us
  • General Terms and Conditions
  • Privacy policy
  • Feedly
  • Telegram
  • Youtube
  • Facebook
  • Instagram
  • LinkedIn
  • Tumblr
  • Pinterest
  • Twitter
  • mastodon

Tags

Active Directory AWS Azure Bitlocker Microsoft Windows PowerShell WDS Windows 10 Windows 11 Windows Server 2016

Copyright © 2025 TechDirectArchive

 

Loading Comments...
 

You must be logged in to post a comment.