# Connexion SSH avec clé publique > Se connecter en SSH à un serveur distant en utilisant une clé publique (sans login/mot de passe). Publié le 2021-10-21 | Mis à jour le 2025-01-12 | Tags: ssh https://xsec.fr/gnu-linux/ssh-certificate/ --- import Callout from '@shared/components/Callout.astro' import { Collapsible } from '@shared/components/ui/collapsible' import { Tabs, Tab } from '@/components/ui/tabs' 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é - Choisissez le type de clé selon les capacités du serveur. Ed25519 est le choix recommandé pour un système moderne, tandis que RSA 4096 reste utile pour la compatibilité avec des serveurs plus anciens. ```sh title="Générer une clé Ed25519" ssh-keygen -t ed25519 ``` ```sh title="Générer une clé RSA 4096" ssh-keygen -t rsa -b 4096 ``` Évitez DSA, désormais obsolète. Protégez aussi la clé privée avec une phrase secrète. ```txt title="ssh-keygen -t ed25519" 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_ed25519 Your public key has been saved in /home/contact/.ssh/id_ed25519.pub The key fingerprint is: SHA256:monhash contact@mondomaine The 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 ```sh ssh-copy-id contact@mondomaine ``` `ssh-copy-id nom_user@ip_ou_domaine` ```txt title="ssh-copy-id admin@dns.it.fr" /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 keys admin@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 ```sh ssh admin@dns.it.fr ``` `ssh nom_user@ip_ou_domaine` Vous êtes maintenant connecté en SSH grâce à la clé d'autorisation. ```sh cat .ssh/authorized_keys ``` ```txt title="cat .ssh/authorized_keys" ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC/3hQOJnQs9ocUaKiFAmOB4tWLzfsgF50VAo7H7at3 FImZ+HQeKTTmds8CxOjaHA1j+4Z4+2hfhBVO5exL4/hMyZTmBJzVEImak+j5UxnRzX6EbVl6btZG7W ek5ifrrTbLvIdgKQrI4E1dv8C7NuPdqbaGTIs3+Uw3mIwkkNy6hvGJ6Tiqt1elri38cvux26pCuG 6fbVhqxoXTbWLoJuxzYd2nAyH4ZlvYR34ktPYCNkaOvfaYRSnbVcQ3a7xQwLuURPyOAjTnDWcBUi4eko IStfQvGC7BT9scPmYyKLB+d58qowl74VELOQIruDgc0o66Zx3bctKU6dw9AGi/LwDOdJLqHqqdihK G15vIgnCnTcY4F9p+KcG3g2tp+KnXmmBtUWll8mma6OdlglfTPAxJxjS+L9cpEOuwT2sKgHgXpuxfl lmTnqfSJMtbZhH5ywB0Eq8fYvNBIsJ02T0Jd8GOq6qCia+7RwoU15Vu+hIp5qRCu17ceJzE= root@ pc-admin ```