LINUX comment s’authentifier en SSH avec une clé

S’authentifier grace a des clés a de nombreux avantage:

  • - Plus de mdp a taper quand on se connecte souvent sur moultes machines en ssh, c’est un gain de temps très appréciable
  • - Facilite les transferts de fichiers avec rsync/scp/etc … et permet par exemple d’automatiser des backups avec rsync

Bon allez trêve de bavardage et go go go

On génère 2 clés
Une privée: id_rsa pour l’authentification du client ^^
Une public: id_rsa.pub pour SSHD

debian:~$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/hio/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_rsa.
Your public key has been saved in id_rsa.pub.
The key fingerprint is:
0b:97:e8:18:78:50:f8:3b:c9:41:ba:f3:6d:4b:41:b9 hio@debian
The key's randomart image is:
+--[ RSA 2048]----+
|   ..            |
|  ...  .         |
|  .+  o          |
|  .oo. o .       |
|  .oo+E S        |
|  o.=+ + .       |
|   o.oo .        |
|    ..o          |
|     ...         |
+-----------------+

On se retrouve donc avec 2 fichiers contenant les clés :O

debian:~$ ls -l
total 8
-rw------- 1 hio hio 1675 mar  6 12:41 id_rsa
-rw-r--r-- 1 hio hio  392 mar  6 12:41 id_rsa.pub

Ensuite on place les clés au bon endroit

debian:~$ mkdir .ssh
debian:~$ cat id_rsa >> .ssh/authorized_keys
debian:~$ la .ssh/
total 8
-rw-r--r-- 1 hio hio  392 mar  6 12:41 authorized_keys

On met les bonnes permissions
Attention si les permissions ne sont pas bonne le serveur ssh peut refusé d’utiliser vos clés

debian:~$ chmod 600 .ssh/authorized_keys
debian:~$ ls -l .ssh/
total 4
-rw------- 1 hio hio 392 mar  6 12:41 authorized_keys

Ma configuration d’sshd

# Package generated configuration file
# See the sshd(8) manpage for details
# What ports, IPs and protocols we listen for
Port 22
# Use these options to restrict which interfaces/protocols sshd will bind to
#ListenAddress ::
#ListenAddress 0.0.0.0
Protocol 2
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes
# Lifetime and size of ephemeral version 1 server key
KeyRegenerationInterval 3600
ServerKeyBits 768
# Logging
SyslogFacility AUTH
LogLevel INFO
# Authentication:
LoginGraceTime 120
PermitRootLogin no
 
# La partie qui nous interresse
StrictModes yes
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile      %h/.ssh/authorized_keys
 
# Don't read the user's ~/.rhosts and ~/.shosts files
IgnoreRhosts yes
# For this to work you will also need host keys in /etc/ssh_known_hosts
RhostsRSAAuthentication no
# similar for protocol version 2
HostbasedAuthentication no
# Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication
#IgnoreUserKnownHosts yes
# To enable empty passwords, change to yes (NOT RECOMMENDED)
PermitEmptyPasswords no
# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no
# Change to no to disable tunnelled clear text passwords
#PasswordAuthentication yes
# Kerberos options
#KerberosAuthentication no
#KerberosGetAFSToken no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
#UseLogin no
#MaxStartups 10:30:60
#Banner /etc/issue.net
# Allow client to pass locale environment variables
AcceptEnv LANG LC_*
Subsystem sftp /usr/lib/openssh/sftp-server
UsePAM yes

Voila ensuite on recupere la clé priver par ses propres moyen , je parle a des gens intelligent, je vais qd même pas tout expliquer.

Donc pour ma part ça sera en SCP ^^

On aura donc un fichier id_rsa sur le poste client.

Ensuite pour utiliser la clé avec putty sous windows il faut la convertir.
Si vous etes sous linux vous avez juste a faire manger la clé au client ssh (ssh –help).

Si vous etes comme moi sous windows au travail par exemple ^^ , il faut encor la convertir avec puttygen.exe, c’est relativement simple , comme je pars du principe que vous etes intelligent , je ne vais donc pas rentrer dans les details.

private_key_putty

Ensuite dans putty
private_key_dans_putty

Et voila le résultat

login as: hio
Authenticating with public key "imported-openssh-key"
Linux debian 2.6.26-1-686 #1 SMP Sat Jan 10 18:29:31 UTC 2009 i686
 
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
 
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
You have new mail.
Last login: Wed Feb 25 11:23:13 2009 from fpc-a800662.bayard-presse.com
debian:~$

Voila , c’est fini, ce fut court mais intense :p

  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • Twitter

Le commentaires sont fermés.