How to use the ssh-keygen Command in Linux

How to use the ssh-keygen Command in Linux

Secure Shell (also known as SSH) is a cryptographic network protocol. Ssh-keygen is a tool in the Linux distro that is used for creating new authentication key pairs for SSH. Such key pairs are used for automating logins, single sign-on, and for authenticating hosts. In simple word, ssh keygen allow a user to connect to a remote system without supplying a password

It is also used to transfer files from one computer to another computer over the network using a secure copy (SCP) Protocol.

In this tutorial post, we will show you how to set up and use password-less login on Linux/Ubuntu, RHEL/CentOS, and Fedora using ssh keys to connect to remote Linux servers without entering a password. By using Password-less login with SSH keys will increase the trust-relation between two Linux servers for easy file synchronization or transfer.

In this example, we will set up SSH password-less automatic login from server 192.168.1.101 as user sr to 192.168.1.102 with user server.

Note:- Username in First Server 192.168.1.101 is :- sr

           Username in Second Server 192.168.1.102 is :- server

In your case IP address’s and username would be different

Step 1: Create Authentication RSA SSH-Keygen Keys on – (192.168.1.101)

First login into server 192.168.1.101 with user sr and generate a pair of public keys using the following command.


sr@vetechno:~$ ssh-keygen -t rsa

How to use the ssh-keygen Command in Linux

Step 2: Now we create .ssh Directory on – 192.168.1.102

Use SSH from server 192.168.1.101 to connect server 192.168.1.102 using server as a user and create .ssh directory under it, using the following command.

Note:- Username in First Server 192.168.1.101 is :- sr

          Username in Second Server 192.168.1.102 is :- server


sr@vetechno:~$
ssh server@192.168.1.102 mkdir -p .ssh

How to use the ssh-keygen Command in Linux

Step 3: Upload/Move Generated Public Keys to Second Server – 192.168.1.102

Now use SSH from First Server 192.168.1.101 and upload a new generated public key (id_rsa.pub) on Second Server 192.168.1.102 under server‘s .ssh directory as a file name authorized_keys.


sr@vetechno:~$ cat .ssh/id_rsa.pub | ssh server@192.168.1.102 'cat >> .ssh/authorized_keys'
How to use the ssh-keygen Command in Linux
Step 4: Now we need to set Permissions on Second Server – 192.168.1.102

Set the remote ~/.ssh directory permissions to 700. Due to different SSH versions on servers, we need to set permissions on .ssh directory and authorized_keys file.

sr@vetechno:
~$
ssh server@192.168.1.102 "chmod 700 .ssh; chmod 640 .ssh/authorized_keys"
How to use the ssh-keygen Command in Linux

Step 5: Login from First Server – 192.168.1.101 to Second Server – 192.168.1.102 Server without Password

From now onwards you can log into Second Server 192.168.1.102 as server user from First server 192.168.0.12 as sr user without a password.

sr@vetechno:
~$
ssh server@192.168.1.102
How to use the ssh-keygen Command in LinuxHow to use the ssh-keygen Command in Linux

Conclusion:


Congratulations you have successfully generated RSA ssh keygen to login server without 
Passwords. 

Related Search Queries 

generate ssh key ubuntu, generate ssh key ubuntu 20, how to find ssh key in ubuntu, generate ssh key linux, ubuntu generate ssh key for git, copy ssh key ubuntu, generate ssh key ubuntu gitlab, ubuntu import ssh key.

Also Read:- How To Install Linux, Apache, MySQL, PHP (LAMP) on Ubuntu

Also Read:-  How to Install MySQL 8.0 on Ubuntu 16.04 LTS ??

Also Read:- Uninstall or Remove MySql 5.7 from Ubuntu 20.04 LTS.

Leave a Reply

Your email address will not be published.