In a Data-center where we have many Linux servers, it becomes very cumbersome and a tedious job to connect every server with a password for performing checks and running sript. To ease the task, the administrator can make a passwordless connection from 1 server with many servers. In this article, you are going to learn how to SSH between two Linux servers with passwordless methods.
Here we will connect Remote server from a local server via SSH without the need of entering the password.
1) Please check here, when we are trying to connect Remote Server (22.214.171.124) from a Local server (126.96.36.199), it asks for a password. Only when we enter correct password then it allows connecting to it.
[root@Localhost]# ssh email@example.com firstname.lastname@example.org's password:
2) Now please follow the steps below to make it passwordless. First, we will run “ssh-keygen” command in Local Server to generate Public Key & Private Key. Public key is stored in “/root/.ssh/id_rsa.pub” file, and private key is stored in “/root/.ssh/id_rsa” file.
Recommended Article: 5 Simple Steps to Recover Root Password of the Linux Server
[root@Localhost]# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: ce:82:d2:47:2f:94:45:8b:20:13:09:26:8c:ba:d0:bf email@example.com The key's randomart image is: +--[ RSA 2048]----+ |=o+o. . | |+..o . o . | |.. . o | |o . o | |.. . + S | |. ..+ + | | . o.+ + | | .E. o | | | +-----------------+
3) Below files are generated in Local Server after running the above command.
[root@Localhost]# cd /root/.ssh [root@Localhost]# ls -ltr -rw-r--r--. 1 root root 394 Oct 28 00:44 id_rsa.pub -rw-r--r--. 1 root root 389 Oct 28 00:45 known_hosts -rw-------. 1 root root 1675 Oct 28 02:12 id_rsa
“id_rsa” is a private key and have (-rw——-) access to the root user only.Hence it cannot be accessed by any group users & others.
“id_rsa.pub” is a public key and have (-rw-r–r–.) permission. Hence it can be accessed by the root user, any group user & others.
4) Copy public key from Local Server to the Remote Server, and save the file with the name “authorized.key” file.
This can be done by two methods.
- By using “ssh-copy-id“ command. It appends the public key to the “authorized_keys” file in the Remote server.
[root@Localhost]# ssh-copy-id -i ~/.ssh/id_rsa.pub 188.8.131.52 firstname.lastname@example.org's password: Now try logging into the machine, with "ssh '184.108.40.206'", and check in: .ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting.
- The other method is to transfer the “id_rsa.pub” file to the Remote server with name “authorized_keys”.
[root@Localhost]# scp /root/.ssh/id_rsa.pub 220.127.116.11:/root/.ssh/authorized_keys email@example.com's password: id_rsa.pub 100% 394 0.4KB/s 00:00
5) Now when you will log in to the Remote server via SSH from Local server, it will be connected without asking for password.
[root@Localhost]# ssh firstname.lastname@example.org Last login: Fri Oct 27 22:37:12 2017 from 18.104.22.168 [root@localhost ~]#
Now you are ready to connect secured passwordless connection between Local Server and Remote Server.
Please leave your comments and feedback in the comment box if you find it beneficial or in case of any doubts.