SSH login without password using public key authentication

On Computer A
Create the public and private keys using the command ssh-keygen

[root@computerA .ssh]# ssh-keygen -t rsa
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:
82:7f:35:8e:05:54:48:3e:4a:3e:af:c7:6a:62:c7:fb root@lin.com

Note: If you do not enter any passphrase by leaving the field blank, then you will not be prompted for the passphrase when login in to the remote computer.

Check to see if the files have been created.

[root@computerA .ssh]# ls -l /root/.ssh
total 16
-rw——- 1 root root 409 Aug 23 11:38 authorized_keys
-rw——- 1 root root 1679 Aug 23 11:40 id_rsa
-rw-r–r– 1 root root 409 Aug 23 11:40 id_rsa.pub
-rw-r–r– 1 root root 1212 Dec 12 2011 known_hosts

Copy the contents of the file id_rsa.pub to the remote computer you wish to login to. This file is your puiblic key. Simply cut and paste the content of the file id_rsa.pub to the file /root/.ssh/authorized_keys on the remote computer. Append public key to the file authorized_keys, or create the file if it does not exits.

To simplyfy things you can also use the command ssh-copy-id to copy the public key to the remote computer. See example below.

[root@computerA .ssh]# ssh-copy-id -i ~/.ssh/id_rsa.pub ComputerB
21
root@ComputerB’s password:
Now try logging into the machine, with “ssh ‘ComputerB’”, and check in:

.ssh/authorized_keys

to make sure we haven’t added extra keys that you weren’t expecting.

You are done. Try login in to the computerB from computerA.

About Andrew Lin

Hi, I have always wanted to creat a blog site but never had the time. I have been working in Information Technology for over 15 years. I specialize mainly in networks and server technologies and dabble a little with the programming aspects. Andrew Lin

View all posts by Andrew Lin →