Se connecter avec une clé publique plutôt qu’avec un User/Password représente plusieurs avantages :
- une clé générée sera toujours plus robuste qu’un mot de passe en général
- il n’y a pas besoin d’utiliser de mot de passe pour se connecter en SSH
Génération de la clé
- Chacune de ces commandes peut être utilisée pour définir le type de chiffrement utilisé pour communiquer avec SSH :
ssh-keygen -t rsa -b 4096ssh-keygen -t dsassh-keygen -t ecdsa -b 521ssh-keygen -t ed25519- Il peut être nécessaire d’utiliser tel ou tel type de clé selon le type de chiffrement accepté par le serveur auquel vous vous connectez.
Tip
Il est recommandé d’utiliser ed25519 qui correspond au standard actuel en termes de sécurité.
Generating public/private ed25519 key pair.Enter file in which to save the key (/home/contact/.ssh/id_ed25519):Created directory '/home/contact/.ssh'.Enter passphrase (empty for no passphrase):Enter same passphrase again:Your identification has been saved in /home/contact/.ssh/id_ed25519Your public key has been saved in /home/contact/.ssh/id_ed25519.pubThe key fingerprint is:SHA256:monhash contact@mondomaineThe key's randomart image is:+--[ED25519 256]--+|%@+. ||EBCVB. ||+=Bo. ||+.o* . ||oo..o . S S ||=+oo o ||B++ o . . ||+=.. o . ||o. . . |+----[SHA256]-----+Renseigner les informations de connexion
ssh-copy-id contact@mondomainessh-copy-id nom_user@ip_ou_domaine
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"The authenticity of host 'dns.it.fr (172.16.10.10)' can't be established.ECDSA key fingerprint is SHA256:lda9Psn+r3CoE3P2eH2hDAR6f50GaTNlUTF5Hw+QqKA.Are you sure you want to continue connecting (yes/no/[fingerprint])? yes/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keysadmin@dns.it.fr's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'admin@dns.it.fr'"and check to make sure that only the key(s) you wanted were added.Se connecter en SSH
ssh admin@dns.it.frssh nom_user@ip_ou_domaine
Tip
Vous êtes maintenant connecté en SSH grâce à la clé d’autorisation.
Exporter la clé d’autorisation
cat .ssh/authorized_keysssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC/3hQOJnQs9ocUaKiFAmOB4tWLzfsgF50VAo7H7at3FImZ+HQeKTTmds8CxOjaHA1j+4Z4+2hfhBVO5exL4/hMyZTmBJzVEImak+j5UxnRzX6EbVl6btZG7Wek5ifrrTbLvIdgKQrI4E1dv8C7NuPdqbaGTIs3+Uw3mIwkkNy6hvGJ6Tiqt1elri38cvux26pCuG6fbVhqxoXTbWLoJuxzYd2nAyH4ZlvYR34ktPYCNkaOvfaYRSnbVcQ3a7xQwLuURPyOAjTnDWcBUi4ekoIStfQvGC7BT9scPmYyKLB+d58qowl74VELOQIruDgc0o66Zx3bctKU6dw9AGi/LwDOdJLqHqqdihKG15vIgnCnTcY4F9p+KcG3g2tp+KnXmmBtUWll8mma6OdlglfTPAxJxjS+L9cpEOuwT2sKgHgXpuxfllmTnqfSJMtbZhH5ywB0Eq8fYvNBIsJ02T0Jd8GOq6qCia+7RwoU15Vu+hIp5qRCu17ceJzE= root@pc-admin