• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here:Home » How To Set up a Passwordless SSH Login in Ubuntu 22.04

By Abhishek Ghosh January 17, 2024 7:25 am Updated on January 17, 2024

How To Set up a Passwordless SSH Login in Ubuntu 22.04

Advertisement

This guide is intended to disable the password-based SSH login for the root user and create an identity file-based SSH authentication like AWS, HP cloud provides a way to log in. If you are using VPSDime, you will get the option while reinstalling OS. Still, many of the web hosts deliver SSH login using the root user account. This is a security risk since any attacker can easily guess the username root and run a dictionary-based attack. The attackers can log in using the root account using different types of methods.

Definitely, using fail2ban reduces the risk but for multiple reasons we need to enable this key based login system. We need to perform a few steps so that we can log in to a non-root user account belonging to the sudoers group using a certificate-based identification system. After a successful login, we have to run the sudo su command to gain the root privilege.

 

Create a non-root User and Disable SSH Login of the root User

 

SSH as root user and add a new user with the command:

Advertisement

---

Vim
1
adduser example-name

example-name is an example. You can replace newuser with any username you wish. It can be harry, joe, krishna etc. The adduser command creates a new user, plus a group and home directory for that user.

After running this command, you’ll be asked for password and some questions.

You may get an error message that you have insufficient privileges. (This typically only happens for non-root users.) The system will add the new user; and then prompt you to enter a password. Enter a secure password, then retype it to confirm. You can SSH to the server with this username and password but you’ll not gain the root privilege. For that reason, we have to add the new user to the sudoers group:

Vim
1
usermod -aG sudo example-name

Now, open a new terminal window and try to SSH with this username. After login, type sudo su and check whether you can run commands just like the root user. If you can, then move to the next step. Open the SSH configuration file while logged in as this user. Do not keep any SSH session open from the root user account:

Vim
1
nano /etc/ssh/sshd_config

Find the line PermitRootLogin and set it to no, usually, this is the last line of the file:

Vim
1
2
3
...
PermitRootLogin no
...

restart the service:

Vim
1
service sshd restart

Now, you’ll unable to SSH as the root user. This is secure but we will make the system more secure.

 

Setup SSH Login with Key File

 

Login as this newly created user and run sudo su to gain root privilege. Do not execute these commands as the root user. Run these commands:

Vim
1
2
3
mkdir -p ~/.ssh/pem
cd ~/.ssh/pem
ssh-keygen -b 2048 -f identity -t rsa

How To Set up a Passwordless SSH Login in Ubuntu 22-04

Do not work as the root user. The above screenshot was taken to avoid showing the username.

Hit the Enter key to leave the passphare empty. It will generate 2 files in pem dir (identity and identity.pub). Copy public key contents to authorized_keys:

Vim
1
cat identity.pub >> ~/.ssh/authorized_keys

Run a cat command on authorized_keys to verify the content. Now run cat on the private key:

Vim
1
cat ~/.ssh/pem/identity

This is the file you’ll use to SSH to the server. Do not loss it.

Highlight the content using your mouse and copy it. Paste it on your local computer’s new text file (save it as name.pem, the name can be anything you want). Save a copy on your GitHub private gist, Dropbox Cloud etc as a backup. For example, I have saved the file named as name.pem on the computer from where I will SSH to this server. Set permission for PEM file on your local copy:

Vim
1
sudo chmod 600 name.pem

First test whether you can SSH with this key to log in:

Vim
1
ssh -i name.pem example-name@server-ip

It will ask you for a password. The next step is risky for the first-time deployment since you can loss the SSH access. So, SSH to your server from two other bash screens. There is always a way for the sysadmins sitting on the datacenter to SSH in any situation but that will consume time.

Disable the password-based authentication:

Vim
1
nano /etc/ssh/sshd_config

Update PasswordAuthentication from “yes” to “no” as below:

Vim
1
2
3
4
...
# Change to no to disable tunnelled clear text passwords
PasswordAuthentication no
...

Restart SSH server:

Vim
1
sudo service ssh restart

Do not close any SSH session. Open a new bash screen on your local computer and run SSH login command in this format:

Vim
1
ssh -i name.pem example-name@server-ip

You can face this kind of error:

Vim
1
Permission denied (publickey).

That is usually related to some mistake while following the steps. Also, this command is important:

Vim
1
sudo chmod 600 name.pem

If the file permission changes, the authentication

Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to How To Set up a Passwordless SSH Login in Ubuntu 22.04

  • How to Enable PEM Certificate Based SSH Login

    After following this guide, you’ll get an identity file-based SSH authentication like AWS, HP cloud provides a way to log in. Commonly most of the cloud servers and dedicated servers are delivered with SSH login using the root user account. This is a security risk since any attacker can easily guess the username root and […]

  • Nginx WordPress Installation Guide (All Steps)

    This is a Full Nginx WordPress Installation Guide With All the Steps, Including Some Optimization and Setup Which is Compatible With WordPress DOT ORG Example Settings For Nginx.

  • Join/Merge Multiple Log Files For Big Data Analysis

    Here Are The Ways To Join/Merge Multiple Log Files For Big Data Analysis, Store Them To OpenStack Based Cloud Storage And Delete Old Files.

  • Step By Step Initial Cloud Server Setup For The New Users

    Here Is A Detailed, Easy, Mandatory To Follow Step By Step Initial Cloud Server Setup For The New Users Which Can Be Used For Any Web Host.

performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

vpsdime

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (24.3K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • Cloud-Powered Play: How Streaming Tech is Reshaping Online GamesSeptember 3, 2025
  • How to Use Transcribed Texts for MarketingAugust 14, 2025
  • nRF7002 DK vs ESP32 – A Technical Comparison for Wireless IoT DesignJune 18, 2025
  • Principles of Non-Invasive Blood Glucose Measurement By Near Infrared (NIR)June 11, 2025
  • Continuous Non-Invasive Blood Glucose Measurements: Present Situation (May 2025)May 23, 2025
PC users can consult Corrine Chorney for Security.

Want to know more about us?

Read Notability and Mentions & Our Setup.

Copyright © 2026 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy