• Skip to main content
  • Skip to primary sidebar
BMA

BeMyAficionado

Inspire Affection

Login To Any Server Without Using Password

October 23, 2019 by varunshrivastava Leave a Comment

This is the most basic stuff that every college grad student must know. Public key authentication is the most secure way of logging into any server. And it doesn’t require you to remember your password or type it every time you want to login to the server, which actually saves valuable seconds of your time.

This article is going to be all about Logging into your servers using SSH public key authentication.

Table of Contents

  • Why Use SSH Public Key
  • How Does Public Key Authentication Works
    • Symmetric Key (Before Diffie-Hellman) Encryption
    • Public-Private Key Encryption
  • How To Generate Unique Public-Private Key
  • Adding id_rsa.pub Key To Server’s Authorized Keys
  • Logging To Your Server Without Password
  • Disabling Password-Based Authentication
  • Conclusion

Why Use SSH Public Key

There are tons of benefits of using public-key authentication over traditional password-based authentication.

  • You don’t have to remember your password every time you have to log in to the system.
  • You don’t have to type the long password every time, which saves you time
  • You can log in to multiple servers without any extra effort.
  • You are always secure.
  • Individual accesses can be managed using public keys.
  • You don’t have to share the password between developers.
  • Multiple people can access the system using the same authentication (useful in some cases).
  • Github and Gitlab allow you to save your public key on their servers in order to streamline the login experience.
  • And more…

This article is not going to be your guide on generating an SSH key. There are a lot of such articles out there. This article is going to give you a better knowledge about the Public key authentication and why you should always use these instead of a password.

How Does Public Key Authentication Works

How does public key authentication works

Public Key authentication is a genius idea of securing communication between two parties.

It is a cryptography system in which two keys are required for authentication. The entire juice of this authentication system lies in the algorithm by which these two keys are generated.

This was invented in 1976 by Whitfield Diffie and Martin Hellman. For this reason, it is sometimes called Diffie-Hellman encryption. It is also called asymmetric encryption because it uses two keys instead of one key (symmetric encryption).

The two keys required in this authentication are called Public Key and Private Key. These two keys are always generated in pairs and they share a special connection. Let’s understand this special connection.

Server

Symmetric Key (Before Diffie-Hellman) Encryption

Symmetric key cryptography before Diffie-Hellman

Before the idea of public-key encryption, people were relying on symmetric key encryption.

In symmetric encryption, there is only one key. This key is called secret key that is shared between two or multiple intended parties with which they can encrypt and decrypt the messages.

Although the symmetric encryption system is perfectly fine and secure but it suffers a major drawback. The drawback is that the secret key needs to be shared beforehand. Only then the person will be able to encrypt or decrypt the message. And trust me this is a big deal.

In today’s world, there is no easy way to share this key with another person sitting on a different continent.

Sharing key before-hand safely and securely is a big security threat in itself. And in the world war 2 they have already shown that symmetric encryption is not the way to the future.

Therefore, Diffie-Hellman encryption takes precedence over symmetric key encryption. And the entire internet communication is based on the same.

Now let’s see how public-private key encryption works.

Public-Private Key Encryption

Diffie-Hellman Key Exchange

In this encryption, mathematical beauty is used to generate two identical keys. These keys are related in a way that the message encrypted using one key (public) can only be decrypted by another key (private).

If you want to understand the mathematics to know how these two keys are generated then please read the following paper:

  • http://www.mathaware.org/mam/06/Kaliski.pdf

The public key is the one which is shared with different people and the private key is the one which is kept secure. The private key should not be shared outside.

The special connection that I was talking about is this:

messages encoded using public keys can only be decoded by its related private key.

So if the message is intended to be read by you then it should be encrypted using your public-key. In that way, you will be the only person who can decrypt the message using your private key.

Similarly, whenever you want to login to any server. You give your public key to that server. So whenever you use SSH for logging into that system, it automatically tries to identify and authenticate you using your private-public key pair.

If you want to read more how the code works, then read the following code base of openSSH:

  • https://github.com/openssh/openssh-portable/blob/master/ssh-add.c

How To Generate Unique Public-Private Key

The first step in the authentication is to generate that unique pair of keys. So, before you proceed further, refer to the following blog (https://serverpilot.io/docs/how-to-use-ssh-public-key-authentication) and set up your public and private key. And then I will show you how can you use your public key to log in to your Github account.

  • https://serverpilot.io/docs/how-to-use-ssh-public-key-authentication

As soon as you set up the keys come back and then we will proceed further and log into the server.

After generating the keys you will find two keys in your ~/.ssh directory.

  • id_rsa (private key)
  • id_rsa.pub (public key)

Adding id_rsa.pub Key To Server’s Authorized Keys

Before you can log in to your server using asymmetric authentication, you will need to add your id_rsa.pub key to the .ssh/authorized_key file.

And for that, you will have to log in to your server once with your username and password.

And before that first copy your ~/.ssh/id_rsa.pub in your clipboard by typing below command:

pbcopy < ~/.ssh/id_rsa.pub

Now log in to your server:

ssh [email protected]

It will ask you to type the password (hidden for security reason). Once you type the password, you will be logged into your server.

Next step is to contact the copied key to the ~/.ssh/authorized_keys file:

cat >> ~/.ssh/authorized_keys
{paste id_rsa.pub key here}

Press control + d to exit the command.

  • Basic Authentication With PHP And MySQL

Logging To Your Server Without Password

Once you have added your public key to the authorized_key file in your server, you can now login without typing your password.

All you need to do is ssh into the server and the rest will be taken care by the ssh utility.

ssh [email protected]

That is all. You should be able to log into the server.

Next and most important step is to disable password-based login into the system. Although you have added the public key to the server, still your password is enabled and anyone trying to brute force the combination might log into the system.

So, to completely secure your server, the wise thing is to disable the password-based authentication into the system.

Disabling Password-Based Authentication

Log into your server: ssh [email protected].

The configuration for disabling the PasswordAuthentication is kept in the following file: /etc/ssh/sshd_config

Open the file in the editor, use VI editor to open the file by typing following command: vi /etc/ssh/sshd_config

Find the line that says:

PasswordAuthentication yes

If the above line is commented, then uncomment the line and replace yes with no. So, the above line should look like:

PasswordAuthentication no

Now restart the server for it to take effect by typing following command:

service sshd restart

Password access should now be removed from the server.

Conclusion

This article was intended to give you a piece of brief information about the working of Public-Private Key encryption. And how you can use rsa public key to log in to your servers.

Here is a list of topics that we covered in this article:

  • Why Use Public-Key Authentication?
  • How does public-key authentication works?
  • How to generate unique public-private asymmetric keys?
  • Adding public key to the server’s authorized_keys file
  • Logging to your server without password
  • Disabling PasswordAuthentication on your server so that no one login using their passwords.

Let me know how you find this article. Waiting for your response.

Related

Filed Under: Programming, Tutorials Tagged With: asymmetric key encryption, public-private key, ssh

Primary Sidebar

Subscribe to Blog via Email

Do you enjoy the content? Feel free to leave your email with me to receive new content straight to your inbox. I'm an engineer, you can trust me :)

Join 874 other subscribers

Latest Podcasts

Recent Posts

  • Is The Cosmos a Vast Computation?
  • Building Semantic Search for E-commerce Using Product Embeddings and OpenSearch
  • Leader Election with ZooKeeper: Simplifying Distributed Systems Management
  • AWS Serverless Event Driven Data Ingestion from Multiple and Diverse Sources
  • A Step-by-Step Guide to Deploy a Static Website with CloudFront and S3 Using CDK Behind A Custom Domain

Recent Comments

  • Varun Shrivastava on Deploy Lambda Function and API Gateway With Terraform
  • Vaibhav Shrivastava on Deploy Lambda Function and API Gateway With Terraform
  • Varun Shrivastava on Should Girls Wear Short Clothes?
  • D on Should Girls Wear Short Clothes?
  • disqus_X5PikVsRAg on Basic Calculator Leetcode Problem Using Object-Oriented Programming In Java

Categories

  • Blogging
  • Cooking
  • Fashion
  • Finance & Money
  • Programming
  • Reviews
  • Software Quality Assurance
  • Technology
  • Travelling
  • Tutorials
  • Web Hosting
  • Wordpress N SEO

Archives

  • November 2024
  • September 2024
  • July 2024
  • April 2024
  • February 2024
  • November 2023
  • June 2023
  • May 2023
  • April 2023
  • August 2022
  • May 2022
  • April 2022
  • February 2022
  • January 2022
  • November 2021
  • September 2021
  • August 2021
  • June 2021
  • May 2021
  • April 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • February 2020
  • December 2019
  • November 2019
  • October 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • January 2019
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016

Tags

Affordable Hosting (4) algorithms (4) amazon (3) aoc-2020 (7) believe in yourself (4) best (4) database (4) earn money blogging (5) education (4) elementary sorting algorithms (4) experience (3) fashion (4) finance (6) Financial Freedom (7) food (7) friends (3) goals (5) google (5) india (10) indian cuisine (5) indian education system (4) java (16) life (16) life changing (4) love (4) make money (3) microservices (9) motivation (4) oops (4) podcast (6) poor education system (4) principles of microservices (5) problem-solving (7) programmer (5) programming (28) python (5) reality (3) seo (6) spring (3) success (10) success factor (4) technology (4) top 5 (7) typescript (3) wordpress (7)

Copyright © 2025 · Be My Aficionado · WordPress · Log in

Go to mobile version