Home arrow How To's arrow Linux Networking arrow OpenSSH Key-Based Authentication
OpenSSH Key-Based Authentication PDF Print E-mail
Written by Anand   
Feb 28, 2007 at 09:09 PM

Tired of keying the password every time when you SSH into your neighboring machines? We'll discuss about a very simple implementation of Key based authentication for SSH here. 

To explain this further, I'm creating two different user accounts on two systems as below, 

  • System 1 --> Mandriva 2007.0 --> Host Name "mdv2007" --> Users (anand & geek)
  • System 2 --> RHEL 5 --> Host Name "rhel5" --> Users (anand & geek)

 

Please find the below table to understand further 

System OS Host Name Users

Home Directory
 

System 1 Mandriva 2007.0 mdv2007.geekbrains.com anand /home/anand
geek /home/geek
System 2 RHEL 5 rhel5.geekbrains.com anand /home/anand
geek /home/geek

 
Please ensure the following

  • Ensure that Open SSH is installation on both the hosts.
  • Ensure that the Firewall permits SSH transactions.
  • Ensure that the hosts can be resolved by hostnames (either by DNS or by /etc/hosts) else we may need to use IP Addresses instead of host names.

 

The following tasks are performed to achieve our goal

 

  • I logged into “rhel5.geekbrains.com” as user “anand

 

  • I generated an RSA Key pair by running “ssh-keygen –t rsa

[anand@rhel5 ~]$ ssh-keygen - t rsa

Generating public/private rsa key pair.

 

  • It will prompt you for a location to save the generated key like this

Enter file in which to save the key (/home/anand/.ssh/id_rsa):

Created directory '/home/anand/.ssh'.

 

  • Set the Passphrase when prompted

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /home/anand/.ssh/id_rsa.

Your public key has been saved in /home/anand/.ssh/id_rsa.pub.

The key fingerprint is:

6b:fd:9f:1b:c5:ed:6e:32:a9:88:ba:e3:41:5c:b3:90

 

  • You can change to the directory “.ssh” to verify those two newly created files

[anand@rhel5 ~]$ cd .ssh

[anand@rhel5 .ssh]$ ls

id_rsa  id_rsa.pub

 

  • I copied the RSA public key to my Mandriva machine “mdv2007.geekbrains.com”

[anand@rhel5 ~]$ scp .ssh/id_rsa.pub anand@mdv2007:/home/anand/

anand@mdv2007's password:

id_rsa.pub     100%  408     0.4KB/s   00:00

   

  • I then logged into ”mdv2007” through SSH as below

[anand@rhel5 ~]$ ssh -l anand mdv2007

anand@mdv2007's password:

Last login: Wed Feb 28 21:05:58 2007 from rhel5

[anand@mdv2007 ~]$ ls

Desktop/  Documents/  Download/  id_rsa.pub  Music/  Pictures/  tmp/  Video/

 

  • I changed into the directory “.ssh” as below

[anand@mdv2007 ~]$ cd .ssh

[anand@mdv2007 .ssh]$ ls

known_hosts

 

  • I checked the content of the “id_rsa.pub”

[anand@mdv2007 .ssh]$ cat ../id_rsa.pub

ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAs/IyprE59J1VqculgayATV/13y91E6AU8QzN4uAc

dbk4VaQBr3i9zYurkI1p/GfTClyGbO5ChXyBPGDKNd0t0mU1xzwL4lD2jr+XD6KpEp51

kY8f1pCEtBWAFuZj4RrvfPGHJQr+c7cwhzNa6y37VuX/mb5t2cIxlqMgwVwfu5oxPyr9

rqNyDT3T1cAAbhCLoEuDzmtF9kZLnnoG2dUUnl5otqMEv9yvLXBT4457BpHDE9nLFItb

T/YYFOWSo7Nkk8vThLlN3qymUG0hue4q8UZTnwxWASDUynfNwZGpS6q+UxnuYeBcyavB

g4Qy4EXsZh00BgJkk+I6mW9nR449tw==

 

  • I copied the Public Key into the “~/.ssh/authorized_keys” file

[anand@mdv2007 .ssh]$ cat ../id_rsa.pub >>authorized_keys

[anand@mdv2007 .ssh]$ ls

authorized_keys  known_hosts

 

  • I changed the file permission to read / write only for the owner

[anand@mdv2007 .ssh]$ chmod 644 authorized_keys

 

  • I logged out from the SSH session of “mdv2007”

[anand@mdv2007 .ssh]$ exit

logout

 

Connection to mdv2007 closed.

 

  • I logged into “mdv2007” again thru SSH

[anand@rhel5 ~]$ ssh -l anand mdv2007

 

  • However, this time instead of prompting me for a password, Mandriva now prompted for a passphrase.

Enter passphrase for key '/home/anand/.ssh/id_rsa':

Enter passphrase for key '/home/anand/.ssh/id_rsa':

Last login: Wed Feb 28 21:06:27 2007 from rhel5

 

  • I logged out again from “mdv2007”

[anand@mdv2007 ~]$ exit

logout

 

  • Now to get the Key Based authentication to its highest level, On the “rhel5” host I ran the following command to start the “ssh-agent” to track the authentication

[anand@rhel5 ~]$ ssh-agent bash

 

  • I added my private key to the agent for authentication

[anand@rhel5 ~]$ ssh-add .ssh/id_rsa

Enter passphrase for .ssh/id_rsa:

Identity added: .ssh/id_rsa (.ssh/id_rsa)

 

  • I tried to login to “mdv2007” again

[anand@rhel5 ~]$ ssh -l anand mdv2007

Last login: Wed Feb 28 21:08:12 2007 from rhel5

 

  • This time I didn’t even get a prompted for a passphrase or a password

[anand@mdv2007 ~]$ exit

Logout

 

  • Now in order to allow the connection only from the Host "rhel5" I edited the "authorized_keys" file to include the "from" field.   

from=”rhel5.geekbrains.com”

AAAAB3NzaC1yc2EAAAABIwAAAQEAs/IyprE59J1VqculgayATV/13y91E6AU8QzN4uAc

dbk4VaQBr3i9zYurkI1p/GfTClyGbO5ChXyBPGDKNd0t0mU1xzwL4lD2jr+XD6KpEp51

kY8f1pCEtBWAFuZj4RrvfPGHJQr+c7cwhzNa6y37VuX/mb5t2cIxlqMgwVwfu5oxPyr9

rqNyDT3T1cAAbhCLoEuDzmtF9kZLnnoG2dUUnl5otqMEv9yvLXBT4457BpHDE9nLFItb

T/YYFOWSo7Nkk8vThLlN3qymUG0hue4q8UZTnwxWASDUynfNwZGpS6q+UxnuYeBcyavB

g4Qy4EXsZh00BgJkk+I6mW9nR449tw==

 

  • We’ve successfully implemented the SSH key based authentication for the user “anand” from the host “rhel5” to “mdv2007”.

 

The same procedure can be performed for the user “geek” at the host “mdv2007” to authenticate the user at “rhel5”.

 



User Comments

Please login or register to add comments

Last Updated ( Mar 05, 2007 at 07:31 PM )
<Previous
DailyTech Main News Feed
This channel features the latest DailyTech news articles.