Sudo Rambles
  • Home
  • Privacy Policy
  • About
  • Contact
Categories
  • cheat-sheets (2)
  • guides (11)
  • news (1)
  • ramblings (4)
  • tutorials (10)
  • Uncategorized (9)
Sudo Rambles
Sudo Rambles
  • Home
  • Privacy Policy
  • About
  • Contact
  • guides
  • tutorials

Git and the multiple SSH keys

  • 12th October 2021

Intro

As a developer you work on many projects at the same time. Maybe you work on your own project on Gitlab and you’re helping a friend with theirs on GitHub. So, you just added your second SSH key and you’re about to hit that sweet “git clone” or “git fetch” command and git freaks out. Let’s fix that

Adding a SSH key

Let’s first go through the steps of adding a new SSH key.

You will need the OpenSSH client at version 6.5+, which is usually pre-installed on Windows, Linux and MacOS

Encryption types

Gitlab recommends either “ED25519” or “RSA” as the encryption algorithm for a new SSH key.

Open a terminal and type

ssh-keygen -t ed25519 -C "<comment>"

OR 

ssh-keygen -t rsa -b 2048 -C "<comment>"

Press enter and follow the on-screen guide. The first prompt will ask you where the new key pair should be stored. Suggestion – leave it as the default, and just update the file name if you’re like me and you tend to forget stuff easily.

The next prompt will be for a password to protect your shiny new key pair. Or you can leave it blank if you want to

Finaly a confirmation is displayed with a nice randomart image attached to it.

Here’s the full command with the inputs all together:

SSH key generation dialog

As we’re talking about multiple keys, let’s go ahead and generate two more:

Another SSH key generation
A third SSH key generation

Here’s what the directory looks like after the above commands

Directory screenshot after generation

Pointing git to the correct key

Currently, git has no idea which key to use when you call “git fetch” on your GitHub repo. Let’s fix that.

Open a terminal and sudo nano/vim/vi into ~/.ssh/config

We need to point each key to a host, and you can do that by using the following directive:

Host host.extension
  IdentityFile ~/.ssh/KEY_TO_LINK

Example of the above keys being configured to three separate domains:

Config file mapping example

Additional flags

You can add many more flags to each directive. The one you see added above means the following:

Specifies whether keys should be automatically added to a running ssh-agent(1). If this option is set to yes and a key is loaded from a file, the key and its passphrase are added to the agent with the default lifetime, as if by ssh-add(1). If this option is set to ask, ssh(1) will require confirmation using the SSH_ASKPASS program before adding a key (see ssh-add(1) for details)

https://man.openbsd.org/ssh_config

For more details on additional flags, please check the main ssh config man page: https://man.openbsd.org/ssh_config

That’s it. Happy coding

Home
Previous Article
vault and go logos
  • cheat-sheets
  • guides
  • tutorials
  • Uncategorized

Vault&Go(role id based)

  • 21st January 2021
View Post
Next Article
  • guides

Dockerised ntopng on a Raspberry Pi 4

  • 17th December 2021
View Post
Sudo Rambles
  • LinkedIn
  • Twitter
A programmer's blog

Input your search keywords and press Enter.

GDPR notice
This website uses cookies to cache data localy. This means the site loads faster on later visits. Please, indicate if you're ok with this.Yep, that's fineNot my cup of tea Read More
Cookie Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT