How to SSH Without Password on Linux

Share this Article :

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.

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

[[email protected] ~]# vi /etc/hosts
[[email protected] ~]# 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 :

[[email protected] ~]# mkdir -p $HOME/.ssh

Set permission as below :

[[email protected] ~]# chmod 0700 $HOME/.ssh

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

[[email protected] ~]# 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 [email protected]
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 :

[[email protected] ~]# ls $HOME/.ssh
id_rsa  id_rsa.pub

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

[[email protected] ~]# mkdir -p $HOME/.ssh

Set permission as below :

[[email protected] ~]# chmod 0700 $HOME/.ssh

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

[[email protected] ~]# scp $HOME/.ssh/id_rsa.pub [email protected]:$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.
[email protected]'s password:
id_rsa.pub                                                        100%  394    

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

[[email protected] ~]# cd $HOME/.ssh

Export id_rsa.pub key to authorized_keys

[[email protected] .ssh]# cat id_rsa.pub >> $HOME/.ssh/authorized_keys

Set permission as below :

[[email protected] .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 :

[[email protected] ~]# ssh [email protected]

Leave a Reply