How to SSH Without Password on Linux

In this post, i will share with you the steps on how to setup and configure linux servers to allow SSH without password. After completely performed the following steps, you can ssh from one system to another without specifying a password. With this, you can automate your tasks such as automatically copy data from server1 to server2. This steps has been tested on CentOS 6.2 and may working on other CentOS versions and Redhat Enterprise linux versions as well.

Notes:
-Client server is server2. This is where ssh session is started via the ssh command.
-Main server is server1. This is where ssh session from server2 will be connects to.
-This steps has been tested using root account on CentOS 6.2.

READ  Transfer Files Between Servers in Linux

1. Add and configure /etc/hosts for both servers (ssh client and ssh server) :

[root@server1 ~]# vi /etc/hosts
[root@server2 ~]# vi /etc/hosts

Add this lines to /etc/hosts on both servers (ssh client and ssh server) :

192.168.1.44 server1
192.168.1.48 server2

2. Login as a root to server2 and create hidden directory called ssh under your account. This steps has been tested using root account :

[root@server2 ~]# mkdir -p $HOME/.ssh

Set permission as below :

[root@server2 ~]# chmod 0700 $HOME/.ssh

3. Configure SSH Keys Authentication by typing the following command :

[root@server2 ~]# ssh-keygen

Whatever it appears just press enter until it ends, press enter for passphase as well :

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:
83:20:f0:1d:11:db:7e:e9:be:d6:ed:a2:e7:f1:ac:34 root@server2
The key's randomart image is:
+--[ RSA 2048]----+
|.   +o           |
| o . +           |
|  o + .          |
|   . o . .       |
|      o S        |
|       o .       |
|        ..E.     |
|       ...+=.    |
|       .+=o++    |
+-----------------+

4. Check what files that was produced by ssh-keygen command. Normally it’s automatically stored under $HOME/.ssh :

[root@server2 ~]# ls $HOME/.ssh
id_rsa  id_rsa.pub

5. Login as a root to server1 and create hidden directory called ssh under your account :

[root@server1 ~]# mkdir -p $HOME/.ssh

Set permission as below :

[root@server1 ~]# chmod 0700 $HOME/.ssh

6. From server2, copy over the id_rsa.pub (public key) to server1 :

[root@server2 ~]# scp $HOME/.ssh/id_rsa.pub root@server1:$HOME/.ssh
The authenticity of host 'server1 (192.168.1.44)' can't be established.
RSA key fingerprint is 71:fc:a2:51:b3:ed:bc:7b:68:ec:9e:51:a8:04:ab:fd.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'server1' (RSA) to the list of known hosts.
root@server1's password:
id_rsa.pub                                                        100%  394    

7. On server1, enter ssh directory and execute these commands :

[root@server1 ~]# cd $HOME/.ssh

Export id_rsa.pub key to authorized_keys

[root@server1 .ssh]# cat id_rsa.pub >> $HOME/.ssh/authorized_keys

Set permission as below :

[root@server1 .ssh]# chmod 0600 $HOME/.ssh/authorized_keys

8. Your have successfully configure and allow ssh without password. From now on you can log into server1 as root from server2 without any password :

[root@server2 ~]# ssh root@server1
Leave a Reply

Your email address will not be published. Required fields are marked *

SiteGround.com

A world leading hosting company that provides fully-managed innovative and secure solutions, suitable for hosting small to medium-sized websites

Built on the best available technologies combined with Google Cloud for strong redundancy and application availability. Backed by skilled experts to address web security threats, a devops team to create advanced custom security solutions, and 24/7 sysadmins to watch over the platform. This powerful, hands-on approach makes your sites faster, safer, and easier to manage. Starting from only $3.95/mo.

TRY FREE

* up to 30 days money back guarantee