Avant tout, il faut vérifier que le serveur SSH (CIBLE) autorise l'authentification par clé publique au niveau du fichier de configuration : sshd_config
Il faut décommenter les lignes PubkeyAuthentication
et AuthorizedKeysFile
:
$ vi /etc/ssh/sshd_config _______________________________________________________________ ... # Authentication: #LoginGraceTime 2m #PermitRootLogin prohibit-password #StrictModes yes #MaxAuthTries 6 #MaxSessions 10 PubkeyAuthentication yes # Expect .ssh/authorized_keys2 to be disregarded by default in future. AuthorizedKeysFile .ssh/authorized_keys .ssh/authorized_keys2 ...
Nous devons générer une paire de clés publique/privée :
[user1@SOURCE ~]$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: b1:87:8d:3c:9e:1b:53:ca:f3:0a:fd:1f:37:f9:2d:c3 root@DEST The key's randomart image is: +--[ RSA 2048]----+ | | | | | . | | . * | | S + | | + * . | | . X ..+ | | . B oEo.| | o.o.. oo| +-----------------+Sous Windows, on utilise PuTTYgen
[user1@SOURCE ~]$ ssh-copy-id -i .ssh/id_rsa.pub user2@CIBLE user2@CIBLE's password: Now try logging into the machine, with 'ssh user2@CIBLE', and check in: .ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting.
# On édite 'id_rsa.pub' et on copie juste la clé : [user1@SOURCE ~]$ vi id_rsa.pub _______________________________________________________________ ---- BEGIN SSH2 PUBLIC KEY ---- Comment: "rsa-key-20220617" AAAAB3NzaC1yc2EAAAADAQABAAABAQC6syxBU9C0S0xpWJQ1xIdrZ9sfsetMKrxd A44P7Q/KTZ4YlBaeklFJD2Gjey0UCqK3ECixddeaqdoNg7IZSu/fn1yRq8A0HMQo sOPy6k55/NJZ6KpWG9VwHFpLGA5lvpKhxbaEwCXCbJIaj8KT0XUAp9EzSe2n+ZV6 3/Ueb5fNtRyytVGOQVa/WYupIQGeJsREDJseWiq28b/33ufxWtkw+0+996NC+Lgn f2jwVybyVSVVGQKXLEaJf5xyRGMsMvhsDc2Ngearex58ECPbrok6XPUXD3oDU1xz 1+r7cFONp80ty/XmaQemhroORdGr3ub0EMprImx/1HZrZfLjPdDL ---- END SSH2 PUBLIC KEY ---- # et on colle la clé après le mot-clé 'ssh-rsa' sur le serveur distant dans : (on supprime les sauts de ligne) [user2@CIBLE ~] vi ~/.ssh/authorized_keys _______________________________________________________________ ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC6syxBU9C0S0xpWJQ1xIdrZ9sfsetMKrxdA44P7Q/KTZ4YlBaeklFJD2Gjey0UCqK3ECixddeaqdoNg7IZSu/fn1yRq8A0HMQosOPy6k55/NJZ6KpWG9VwHFpLGA5lvpKhxbaEwCXCbJIaj8KT0XUAp9EzSe2n+ZV63/Ueb5fNtRyytVGOQVa/WYupIQGeJsREDJseWiq28b/33ufxWtkw+0+996NC+Lgnf2jwVybyVSVVGQKXLEaJf5xyRGMsMvhsDc2Ngearex58ECPbrok6XPUXD3oDU1xz1+r7cFONp80ty/XmaQemhroORdGr3ub0EMprImx/1HZrZfLjPdDL root@ma_machine # on peut aussi limiter l'accès à une machine/@IP [user2@CIBLE ~] vi ~/.ssh/authorized_keys _______________________________________________________________ from="192.168.1.1,ma_machine.mon_domaine.com" ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC6syxBU9C0S0xpWJQ1xIdrZ9sfsetMKrxdA44P7Q/KTZ4YlBaeklFJD2Gjey0UCqK3ECixddeaqdoNg7IZSu/fn1yRq8A0HMQosOPy6k55/NJZ6KpWG9VwHFpLGA5lvpKhxbaEwCXCbJIaj8KT0XUAp9EzSe2n+ZV63/Ueb5fNtRyytVGOQVa/WYupIQGeJsREDJseWiq28b/33ufxWtkw+0+996NC+Lgnf2jwVybyVSVVGQKXLEaJf5xyRGMsMvhsDc2Ngearex58ECPbrok6XPUXD3oDU1xz1+r7cFONp80ty/XmaQemhroORdGr3ub0EMprImx/1HZrZfLjPdDL root@ma_machine
Une fois que la clé publique a été copiée dans le authorized_keys
du serveur CIBLE, on peut paramétrer PuTTY pour utiliser automatiquement la clé privée correspondante.
Dans le paramétrage de la session correspondant au serveur CIBLE :
On lance ensuite la connexion :