-
Notifications
You must be signed in to change notification settings - Fork 0
Managing SSH keys for GitHub and GitLab
In this guide, we will
- first generate an SSH key for GitHub and register it in our GitHub account then
- generate another SSH key for GitLab and register it in our GitLab account and finally
- set up a config file that will manage which SSH key to use in different scenarios.
I recently had to manage two ssh keys (one for Github and one for GitLab).
The first question you can ask yourself is can you have the same ssh key for both GitHub and GitLab? The answer is yes but it is not advisable.
The best answer is that you should set one ssh key for Github and another one for GitLab. First, you should check for existing ssh-keys on your system:
$ ls -al ~/.ssh
# Lists the files in your .ssh directory, if they exist- Check the directory listing to see if you already have a public SSH key. By default, the filenames of the public keys are one of the following:
id_rsa.pub
id_ecdsa.pub
id_ed25519.pub🔎 You can simply delete these files if you want to remove the keys. No additional step is required.
If you don't have an existing public and private key pair or don't wish to use any that are available to connect to GitHub, then generate a new SSH key:
$ ssh-keygen -f ~/.ssh/id_ed25519_hub -t ed25519 -C "your_email@example.com"
# Use your GitHub email address🔎 If you are using a legacy system that doesn't support the Ed25519 algorithm, use:
$ ssh-keygen -f ~/.ssh/id_rsa_hub -t rsa -b 4096 -C "your_email@example.com"
This creates a new ssh key, using the provided email as a label.
> Generating public/private ed25519 key pair.- Ensure the ssh-agent is running. You can use the "Auto-launching the ssh-agent" instructions in "Working with SSH key passphrases", or start it manually:
# start the ssh-agent in the background
$ eval `ssh-agent -s`
> Agent pid 59566- Add your SSH private key to the ssh-agent. If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_ed25519_hub in the command with the name of your private key file.
$ ssh-add ~/.ssh/id_ed25519_hub- Copy the SSH public key to your clipboard.
If your SSH public key file has a different name than the example code, modify the filename to match your current setup. When copying your key, don't add any new lines or whitespace.
$ clip < ~/.ssh/id_ed25519_hub.pub
# Copies the contents of the id_ed25519_hub.pub file to your clipboard🔎 If
clipisn't working, you can locate the hidden.sshfolder, open the file in your favorite text editor, and copy it to your clipboard.
- In the upper-right corner of any page, click your profile photo, then click Settings.
- In the user settings sidebar, click SSH and GPG keys.
- Click New SSH key or Add SSH key.
- In the "Title" field, add a descriptive label for the new key.
- Paste your key into the "Key" field.
- Click Add SSH key.
- If prompted, confirm your GitHub password.
$ ssh-keygen -f ~/.ssh/id_ed25519_lab -t ed25519 -C "max.mustermann@uni-bielefeld.de"
# User your GitLab email address- Ensure the ssh-agent is running. You can use the "Auto-launching the ssh-agent" instructions in "Working with SSH key passphrases", or start it manually:
# start the ssh-agent in the background
$ eval `ssh-agent -s`
> Agent pid 59566- Add your SSH private key to the ssh-agent. If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_ed25519_lab in the command with the name of your private key file.
$ ssh-add ~/.ssh/id_ed25519_labTo use SSH with GitLab, copy your public key to your GitLab account.
- Copy the contents of your public key file. You can do this manually or use a script. For example, to copy an ED25519 key to the clipboard:
cat ~/.ssh/id_ed25519_lab.pub | clip- Sign in to GitLab.
- In the top right corner, select your avatar.
- Select Settings.
- From the left sidebar, select SSH Keys.
- In the Key box, paste the contents of your public key. If you manually copied the key, make sure you copy the entire key, which starts with
ssh-ed25519orssh-rsa, and may end with a comment. - In the Title text box, type a description, like Work Laptop or Home Workstation.
- Optional. In the Expires at box, select an expiration date. (Introduced in GitLab 12.9.) The expiration date is informational only and does not prevent you from using the key. However, administrators can view expiration dates and use them for guidance when deleting keys.
- Select Add key.
Generally, in Windows machines, the SSH config file is stored in the following location: /c/Users/PC_USER_NAME/.ssh/
Just follow the steps below (if you're using Git Bash):
- Go to the .ssh directory
/c/Users/PC_USER_NAME/.ssh/, click the right mouse button, and choose "Git Bash Here" or open a terminal at this location. - Create a file named "config" with the following command:
$ touch config- Now open the config file with the command (you can use any text editor as well):
$ nano config- Now write the following lines inside the config file:
Let's assume you've created two files named id_ed25519_hub for Github and id_ed25519_lab for GitLab.
# GITHUB
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_ed25519_hub
# GITLAB
Host gitlab.com
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_ed25519_lab
From now on, to ensure that our commits and pushes from each repository on the system use the correct user, we will have to configure user.email and user.name in every repository freshly cloned or existing before.
To do this use the following commands in each repository folder.
git config user.email "my_office_email@gmail.com"
git config user.name "Rahul Pandey"- Git Theorie
- Git herunterladen und installieren
user.nameunduser.emailaktualisieren- Das
Biomechatronik PraktikumGitlab Repository klonen
- Flutter SDK herunterladen
- Flutter zur Umgebungsvariable
PATHhinzufügen - Android Studio und Plugins installieren
flutter doctorFehlermeldungen beheben- USB-debugging vorbereiten
- Was ist Flutter und Dart?
- DartPad Tutorial: create simple Widgets
- Neues Modul entwerfen
- Datentypen, Datenstrukturen
- Klassen
- Kommentierung
- Sound null-safety
- Asynchrone Programmierung
- Aufbau des Projektes
- Galeriebild
- Spracheneinstellungen
- Neues Package hinzufügen