Authentification par clefs SSH
Auteur : Olivier BOYER - oboyer () ajoute ! org
Mots clés
authentification, ssh, clés, keys, unix, linux, BSD
Introduction
Le but de ce document est de décrire comment s'authentifier depuis un client vers un serveur via les clés ssh.
L'utilisation des clés SSH :
- permet à un utilisateur de se loger sur un serveur distant sans avoir à saisir son mot de passe.
- peut permettre à un utilisateur de se connecter sur un serveur sans connaitre le mot de passe du login. En effet, il est possible de protéger la clé SSH par une 'passphrase'. Ainsi, pour se loger, l'utilisateur doit saisir la 'passphrase' et non le mot de passe associé au compte.
Présentation de la maquette
- Un utilisateur sur la machine client 'client1' cherche à se logger via ssh sur un serveur 'server'.
- La clé ssh générée ne sera pas protegée par une 'passphrase'.
- La version de SSH utilisée est uniquement la V2.
Note
Toutes les commandes commandes présentées ici, sont à exécuter, sauf mention contraire, en tant qu'utilisateur de la machine.
Pré-requis
- OpenSSH soit être installé est convenablement configuré sur les deux machines (client et serveur).
- Le daemon SSH doit être lancé sur la machine serveur.
- La configuration du client et du serveur SSH doit autoriser les authentifications par clés (active par défaut avec OpenSSH).
Générer des clefs ssh sur le client
Logez vous sur la machine cliente 'client1' en tant qu'utilisateur.
Créer le répertoire ~/.ssh
Ce répertoire contiennent les informations utilisateur de ssh. Pour le créer, procedez comme suit :
toto@client1$ cd $HOME toto@client1$ mkdir .ssh toto@client1$ chmod 700 .ssh toto@client1$ cat /dev/null > .ssh/known_hosts toto@client1$ chmod 644 .ssh/known_hosts toto@client1$ ls -l total 16 -rw-r--r-- 1 toto users 6618 Jan 19 22:55 known_hosts
Note : $HOME/.ssh/known_hosts
Records host keys for all hosts the user has logged into that are not in /etc/ssh/ssh_known_hosts. See sshd(8).
Générer les clés RSA et DSA sur le client
Dans notre cas, les clés ne sont pas protégées par une 'passphrase'. Lorsqu'il vous est demande de la saisir, appuyez sur la touche 'Entree'.
Pour générer la clé RSA, procédez comme suit :
toto@client1$ cd $HOME toto@client1$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/toto/.ssh/id_rsa): 'Entree' Enter passphrase (empty for no passphrase): 'Entree' Enter same passphrase again: 'Entree' Your identification has been saved in /home/toto/.ssh/id_rsa. Your public key has been saved in /home/toto/.ssh/id_rsa.pub. The key fingerprint is: a3:b2:4c:7d:9d:d8:e8:c4:e5:f9:a8:64:63:31:c7:f2 toto@client1
Pour générer la clé DSA, procédez comme suit :
toto@client1$ cd $HOME toto@client1$ ssh-keygen -t dsa Generating public/private dsa key pair. Enter file in which to save the key (/home/toto/.ssh/id_dsa): 'Entree' Enter passphrase (empty for no passphrase): 'Entree' Enter same passphrase again: 'Entree' Your identification has been saved in /home/toto/.ssh/id_dsa. Your public key has been saved in /home/toto/.ssh/id_dsa.pub. The key fingerprint is: 87:5c:64:18:2f:2a:c2:a4:3b:94:a7:fa:c2:9f:ac:dc toto@client1
Les fichiers suivants ont été générés :
toto@client1$ ls -l .ssh/ total 18 -rw------- 1 toto users 668 Jan 19 22:54 id_dsa -rw-r--r-- 1 toto users 600 Jan 19 22:54 id_dsa.pub -rw------- 1 toto users 883 Jan 19 22:53 id_rsa -rw-r--r-- 1 toto users 220 Jan 19 22:53 id_rsa.pub -rw-r--r-- 1 toto users 6618 Jan 19 22:55 known_hosts
Note : $HOME/.ssh/identity, $HOME/.ssh/id_dsa, $HOME/.ssh/id_rsa
Contains the authentication identity of the user. They are for protocol 1 RSA, protocol 2 DSA, and protocol 2 RSA, respectively. These files contain sensitive data and should be readable by the user but not accessible by others (read/write/execute). Note that ssh ignores a private key file if it is accessible by oth- ers. It is possible to specify a passphrase when generating the key; the passphrase will be used to encrypt the sensitive part of this file using 3DES.
$HOME/.ssh/identity.pub, $HOME/.ssh/id_dsa.pub, $HOME/.ssh/id_rsa.pub
Contains the public key for authentication (public part of the identity file in human-readable form). The contents of the $HOME/.ssh/identity.pub file should be added to $HOME/.ssh/authorized_keys on all machines where the user wishes to log in using protocol version 1 RSA authentication. The con- tents of the $HOME/.ssh/id_dsa.pub and $HOME/.ssh/id_rsa.pub file should be added to $HOME/.ssh/authorized_keys on all machines where the user wishes to log in using protocol version 2 DSA/RSA authentication. These files are not sensitive and can (but need not) be readable by anyone. These files are never used automati- cally and are not necessary; they are only provided for the con- venience of the user.
Préparer la machine serveur 'server'
Logez vous sur la machine serveur 'server' en tant qu'utilisateur.
Créer le répertoire ~/.ssh
Ce répertoire contiennent les informations utilisateur de ssh.
Pour créer ce répertoire, procédez comme suit :
toto@server$ cd $HOME toto@server$ mkdir .ssh toto@server$ chmod 700 .ssh toto@server$ cat /dev/null > .ssh/known_hosts toto@server$ chmod 644 .ssh/known_hosts toto@server$ ls -ld .ssh drwx------ 2 toto users 512 Jan 19 22:55 .ssh toto@server$ ls -l total 16 -rw-r--r-- 1 toto users 6618 Jan 19 22:55 known_hosts
Note : $HOME/.ssh/known_hosts
Records host keys for all hosts the user has logged into that are not in /etc/ssh/ssh_known_hosts. See sshd(8).
Créer le fichier ~/.ssh/authorized_keys2
Pour créer ce fichier, procédez comme suit :
toto@server$ cat /dev/null > $HOME/.ssh/authorized_keys2 toto@server$ chmod 600 $HOME/.ssh/authorized_keys2
Note : $HOME/.ssh/authorized_keys
Lists the public keys (RSA/DSA) that can be used for logging in as this user. The format of this file is described in the sshd(8) manual page. In the simplest form the format is the same as the .pub identity files (that is, each line contains the num- ber of bits in modulus, public exponent, modulus, and comment fields, separated by spaces). This file is not highly sensitive, but the recommended permissions are read/write for the user, and not accessible by others.
Copier les clés ssh sur le serveur
Copier les clés du client vers le serveur
Logez vous sur la machine cliente 'client1' en tant qu'utilisateur, puis exécutez les commandes suivantes :
toto@client1$ cd $HOME/.ssh toto@client1$ $ ls *.pub id_dsa.pub id_rsa.pub toto@client1$ $ scp *.pub server:
Intégrer les clés sur le serveur
Logez vous sur la machine serveur 'server' en tant qu'utilisateur, puis exécutez les commandes suivantes :
toto@server$ cd $HOME toto@server$ cat id_dsa.pub >> $HOME/.ssh/authorized_keys2 toto@server$ cat id_rsa.pub >> $HOME/.ssh/authorized_keys2 toto@server$ rm id_dsa.pub id_rsa.pub
Se connecter
toto@client1$ ssh toto@server