How to SSH without password

Tags: 

By theory, SSH (Secure Shell) without password must use private key and public key for pairing between server and client. public key must be put on server and private key must be put on client.

Run below command from client to generate public and private key files

ssh-keygen -t dsa

Then it will show some dialogs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Generating public/private dsa key pair.
Enter file in which to save the key (/home/username/.ssh/id_dsa): 
 
Created directory '/home/username/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
 
Your identification has been saved in /home/username/.ssh/id_dsa.
Your public key has been saved in /home/username/.ssh/id_dsa.pub.
The key fingerprint is:
5c:f6:a2:aa:ac:91:a5:35:1e:2a:3f:04:a0:76:f4:c3 username@ubuntu:~$
The key's randomart image is:
+--[ DSA 1024]----+
|                 |
|.  .             |
|o . o     o      |
|o. . E . o .     |
|... = . S . .    |
|  .B o   . .     |
|..= .   .        |
| o.o   .         |
|  ooo..          |
+-----------------+

Line 2 You can change save path of public and private key files and it will be at /home/username/.ssh/id_dsa by default if you leave it blank.
Line 5-6 You must put passphase and confirm it to increase security and protect your key file if it had stolen. You can leave this blank too but consider about your security.

So you will have id_dsa file for private key and id_dsa.pub file for public key in path you have enter.

Copy id_dsa.pub file to your server at /home/serverusername/.ssh/ and put command

cd /home/serverusername/.ssh/
mv id_dsa.pub authorized_keys
chmod 600 authorized_keys

Copy id_dsa file to client that you want to SSH to server. Don't forget to change file permission to 600 with chmod command. By the way, you can use any filename you want but you must specific correct path and file in below command.

Test SSH from client to server by run

ssh -l serverusername -i /home/username/.ssh/id_dsa hosturl

-l serverusername is username on server that you want to SSH
-i /home/username/.ssh/id_dsa is path and filename of private key
hosturl is ip or hostname of your server