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 (1.1.1.2) from a Local server (1.1.1.1), it asks for a password. Only when we enter correct password then it allows connecting to it.
[root@Localhost]# ssh root@1.1.1.2
root@1.1.1.2'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 root@localhost.localdomain
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.
Suggested Articles
[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 1.1.1.2
root@1.1.1.2's password:
Now try logging into the machine, with "ssh '1.1.1.2'", 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 1.1.1.2:/root/.ssh/authorized_keys
root@1.1.1.1'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 root@1.1.1.2
Last login: Fri Oct 27 22:37:12 2017 from 1.1.1.2
[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.

Leave a Reply

1 Comment on "How to Connect Two Linux Servers Without Password via SSH"

Notify of
avatar
Sort by:   newest | oldest | most voted
Xanny
Guest

This is a huge security risk. You should NEVER allow root login via SSH. If anything, set up a standard user with SSH Key Authentication (which is what this is, the title of this article isn’t a good one) and then sudo as needed.

At the very least, alert your readers that SSH login as root is a security risk.

wpDiscuz