SSH : Public keys generation

The purpose of this short post is to explain how being connected to an ssh server without typing the password, how ?
With the generation of public ssh keys.

I. Client : Generation of public keys

First (just for the first time) you have to genere the 2 keys :
[donunix@client] ssh-keygen -t dsa

During the dsa key generation, ssh-keygen ask you to enter a path phrase (it replace the password system). type [enter] if you doesn't what to genere a path phrase.

The 2 keys are in the ~/.ssh directory :
[donunix@client] ls ~/.ssh/id_dsa*
id_dsa id_dsa.pub

  • id_dsa is the private key
  • id_dsa.pub is the public key to send too the server

Client : send to the server the public keys

copy the public keys to the server :
[donunix@client] scp .ssh/id_dsa.pub user@server:.ssh/client.pub

Server : Record the client public keys

Connect to the server :
[donunix@client] ssh donunix@server

record the keys on the .ssh/authorized_keys file :
[donunix@server] cd .ssh
[donunix@server] cat client.pub >> authorized_keys

And that's all folk ;)
You can now run ssh sessions without typing your password.

2 comments:

Twidi said...

Pourquoi ne pas utiliser ssh-copy-id pour envoyer la clé publique ? Pas besoin ainsi d'aller éditer ou faire quelque manipulation que ce soit sur le serveur distant

clementd said...

Un autre truc sympa, est de se faire un fichier .ssh/config, où on peut mettre par exemple :

Host monhost
Hostname www.monhost.com
User divarvel
IdentityFile /home/divarvel/.ssh/id_rsa.pub
Port 8080

Un simple "ssh monhost" suffit, ce qui est bien pratique, surtout que ça marche aussi avec scp.
Pour pousser le vice un peu plus loin, on peut ajouter un alias sur la commande ssh monhost (j'ai fait ça pour tous mes serveurs, une commande de 3 caractères suffit pour me connecter à chacun d'eux)