SSH avec clés publiques

Donc, on se connecte sur le poste client et on génère une clé RSA:

$ ~ > ssh-keygen -t rsa -b 1024 -f ~/.ssh/id_rsa
Generating public/private rsa key pair.
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:
28:bd:83:81:b5:8f:22:59:44:26:28:ac:3c:91:e7:60 naxalite@chomp.naxalite
Dans cet exemple, pour ne pas avoir à saisir de mot de passe, on laisse la passphrase vide.

Ensuite, on copie le contenu du fichier ~/.ssh/id_rsa.pub de la machine locale dans le fichier ~/.ssh/authorized_keys de la machine distante. Attention, la clé publique doit être en une seule ligne dans le fichier ~/.ssh/authorized_keys!

Maintenant, pour se connecter sans taper le mot de passe, il suffit d'ajouter l'option -i <key-file> dans la commande ssh:
% ~ > ssh -u fool -i ~/.ssh/id_rsa remotehost
Pour que sshd accepte les clés publique il faut que l'option RSAAuthentication soit activée (c'est le cas par défaut).

Passer en root sur une machine.

On va avoir besoin de faire tourner un second sshd sur la machine. Pour ça on fait une copie du fichier de config (/etc/ssh/sshd_config) dans laquelle on active le login root, on change le port, on définit les fichier contenant les clés et on met en écoute sur l'interface locale (127.0.0.1).

#   $OpenBSD: sshd_config,v 1.68 2003/12/29 16:39:50 millert Exp $

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options change a
# default value.

Port 62000
Protocol 2
ListenAddress 127.0.0.1
#ListenAddress ::

# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_root_host_key
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_root_host_rsa_key
HostKey /etc/ssh/ssh_root_host_dsa_key

# Lifetime and size of ephemeral version 1 server key
#ServerKeyBits 768

# Logging
#obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
PermitRootLogin yes

#StrictModes yes

#RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile  .ssh/authorized_keys

# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#RhostsRSAAuthentication no
# similar for protocol version 2
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# RhostsRSAAuthentication and HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
PermitEmptyPasswords no

# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes

# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes

#AllowTcpForwarding yes
#GatewayPorts no
#X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
PrintMotd yes
PrintLastLog yes
TCPKeepAlive yes
#UseLogin yes
UsePrivilegeSeparation yes
#PermitUserEnvironment no
Compression yes
ClientAliveInterval 0
ClientAliveCountMax 3
UseDNS yes
PidFile /var/run/sshd_root.pid
#MaxStartups 10

# no default banner path
#Banner /etc/welcome

# override default of no subsystems
Subsystem   sftp    /usr/libexec/sftp-server

Pour lancer ce second démon, ajoutez ceci à /etc/rc.conf.local et /etc/rc.local:
$ ~ > echo "root_sshd_flags="-f /etc/ssh/<copie_de_sshd_config>" > /etc/rc.conf.local

$ ~ > cat > /etc/rc.local << "EOF"
if [ X"${root_sshd_flags}" != X"NO" ]; then
    echo -n ' sshd';        /usr/sbin/sshd ${root_sshd_flags};
fi
EOF
Après cela, vous pouvez démarrer le second démon sshd en tapant /usr/sbin/sshd -f /etc/sshd/copie_de_sshd_config.
Maintenant, il ne nous reste plus qu'a générer une clé rsa et copier la clé publique dans le ~/.ssh/authorized_keys de root et la clé privée dans le répertoire de l'utilisateur.
Si votre serveur ssh refuse de laisser root se logguer, vérifiez que UseLogin soit bien désactivé, sinon login refusera les login root distants.

Feedback:

Propositions de job, commentaires, déclarations d'amour, insultes, menaces, virus, dons etc.
D'autres infos sur beuchede.net
Vous me trouverez sur #OpenBSD.fr@freenode