Configuring SSH for Gitlab

De WikiBR
Cette page est une version traduite de la page Mettre en place SSH pour Gitlab et la traduction est terminée à 100 %.

The aim of this guide is to help you set up a computer to use GitLab with SSH authentication. It is aimed at beginners: for a more general introduction, see the main page on SSH and the BR’s advanced tips.

The installation steps for Windows 10 and 11 are described below. For Linux and macOS, please also refer to the main SSH guide.

The setup is relatively simple. Follow carefully the following steps.

Prérequis

Before you start, make sure you have git installed on your computer. To check this:

  • open a cmd or PowerShell terminal (press the Windows key + R, then type cmd or powershell);
  • type git -v and check that Git is installed; if it isn’t, install Git here;
  • then type ssh -V to check that SSH is installed; if it isn’t, install OpenSSH here.

Mise en place

Étape 1 : Générer une clé SSH

Open a terminal (cmd or powershell) and type the following command:

ssh-keygen -t ed25519 -C "ordi portable vaneau"

Replace ordi portable vaneau with a name of your choice. You can leave the other fields empty by pressing Enter for each option.

Please note that there is an option to change the key’s name (by default id_ed25519) and the save location. There is also another option to add a passphrase to the key, which is recommended if your hard drive is not encrypted.

Attention : Cette commande génère deux fichiers dans le dossier C:\Users\<ton_nom>\.ssh : la clé privée (id_ed25519) et la clé publique (id_ed25519.pub). DO NOT SHARE THE PRIVATE KEY. It's the public key which should be added to Gitlab. We cannot stress enough this point. All the security counts on the fact that the private key stays secret.

Étape 2 : Ajouter la clé SSH sur Gitlab

  1. Connecte-toi sur Gitlab avec tes identifiants Sigma BR ;
  2. Clique sur ton avatar en haut à gauche, puis sur Preferences, puis sur SSH Keys (ou va directement sur cette page) ;
  3. Clique sur Add new key ;
  4. Copie le contenu du fichier C:\Users\<ton_nom>\.ssh\id_ed25519.pub (ou un autre chemin si tu as choisi un autre emplacement) en remplaçant <ton_nom> par ton nom d’utilisateur Windows et colle-le dans le champ Key ;
  5. (optionnel) Supprime la date d’expiration de la clé si tu veux qu’elle reste valide indéfiniment ;
  6. Clique sur Add key.

Étape 3 : Préparer SSH pour utiliser la clé

  1. Ouvre le dossier C:\Users\<ton_nom>\.ssh sur VSCode (ou ton éditeur de texte préféré) ;
  2. Crée un fichier config (sans extension) ;
  3. Ajoute le contenu suivant :
Host gitlab.binets.fr
  User git
  IdentityFile C:\Users\<ton_nom>\.ssh\id_ed25519

En remplaçant <ton_nom> par ton nom d’utilisateur Windows.

Étape 4 : Activer l'agent SSH

Taper ssh-add -L

  • Si ta clef ssh est affichée, tout va bien, tu peux passer à l'étape suivante
  • Sinon, suit ces étapes :

Sur windows

  1. Aller dans la recherche windows et lancer Services
  2. Chercher OpenSSH Authentification Agent
  3. Double cliquer dessus et mettre : Type de démarrage : Automatique
  4. Appuyer sur démarrer
  5. Dans un terminal, faire ssh-add

Sur Linux/Mac

Voilà une solution un peu nulle qu'il faudra refaire à chaque fois que tu redémarre ton ordi. Lance un terminal et tape :

eval `ssh-agent`
ssh-add

N'hésite pas à trouver une meilleure solution et à l'écrire ici.

Étape 5 : Tester la connexion SSH

Pour tester la connexion SSH, ouvre un terminal et tape la commande suivante :

ssh -T gitlab.binets.fr

Tu devrais voir un message comme ça :

The authenticity of host 'gitlab.binets.fr (129.104.201.46)' can't be established.
ED25519 key fingerprint is SHA256:sn+wsip9OTBcwiY0ZplULWXNij3v3kdj/tP228i2+70.
Are you sure you want to continue connecting (yes/no/[fingerprint])?

Écrit yes puis appuie sur Entrée.

Si tout s’est bien passé, tu devrais voir un message de bienvenue de Gitlab. Dans le cas contraire, vérifie que tu as bien suivi toutes les étapes précédentes. Si tu rencontres des difficultés, n’hésite pas à demander de l’aide au Binet Réseau en permanence ou sur PaniX.

Étape 6 : Configurer l’identité Git

Pour finir, il faut configurer correctement ton identité sur Git. Dans ton terminal, tape les commandes suivantes :

git config --global user.name "<Ton Nom>"
git config --global user.email "<ton.nom>@polytechnique.edu"

En remplaçant <Ton Nom> par ton nom et <ton.nom>@polytechnique.edu par les valeurs adéquates.

Félicitations ! À partir de là, tu peux utiliser Gitlab avec une authentification SSH.

Lorsque tu clones un dépôt, veille bien à choisir le lien SSH et non HTTPS.