SSH it's going to connect to Git

N.Kendrick
7 min readMay 6, 2022

SSH Keys are often required to access various version control systems, such as GitHub or GitLab.

While this does provide better security access, it first managed to confuse new programmers completely and, secondly, make it more challenging to access multiple systems accounts on a single computer or from multiple computers. Here, we will walk through the sets of getting a setting on an SSH key so you can access your various accounts from multiple computers. I promise it is easier than it sounds. Most of my screenshots are going to be from a Bash terminal and GitHub, but I have tried to provide additional information if you are working from various operating systems.

If you only see a single line of code, that should indicate it should work on all systems.

Please do this before you start cloning from repositories; it will save you a headache.

1. Get an SSH key

SSH stands for Secure Shell Protocol. This allows operating network services to become secure over insecure networks.

Open your terminal

ssh-keygen -t ed25519 -C "your_email_on_github_account@example.com" 

You'll get back something that looks something like this.

This will generate a public and private key-value pair the system can check against. To see this newly generated pair, you can go to ~/.ssh

You should see an

configid_ed25519
id_ed25519.pub

If you don't see a config file, that's okay; we will create one later.

Do you see the connection? The document is generated using the -t the output_file provided, the ed25519. If you run:

cat id_ed25519.pub 
## you will get the output of what is in the file
ssh-ed25519 $$$$$$$$$$$$A_NICE_LONG_ENCODED_STRING$$$$$$$$$$$$$$ your_email_on_github_account@example.com

I recommend changing the file names to reflect the account on which you will be using this key. You will see more on this later on. If you update this now, make sure you update both the regular private and public files, their names need to make. For example, if you changed id_ed25515 (the private key) to gitLabWork, you would get an error because the id_ed25515.pub (the public key) would no longer match. You would need to update it to gitLabWork.pub.

2. Add your SSH key to your account.

Below are the located for GitLab and GitHub to find the location to add these keys.

GitLab:

Log into the GitLab account you are going to need access to. Go to your profile, click on your profile image in the top right corner, and then click on “Edit profile”.

In the left panel now click on “SSH Keys”

Once you get to the User Settings > SSH Keys, add your Key to the Key window and provide the title. Recommended to use the location of where the key is being used

Click on “Add key”

GitHub:

Then, go to your profile and settings.

Then find the section titled "SSH and GPG keys".

On this page, click the "New SSH key" or "Add SSH key."

You will be taken to this next page.

For both systems:

Use a descriptive phrase for your computer/laptop/machine to access this account.

Within the key, in the contents of the .pub file, you will use as your key. Then "Add SSH Key". You may be asked to reconfirm your password at this time.

Now, whenever you clone a repo, make sure you have the SSH clone copied.

3. Add Another

As you may recall, when we created our first key, you included ed25519. Was it just the name used on the generated file? Not so fast.

This cryptographic solution was brought to you by Edwards-curve Digital Signature Algorithm, (EdDSA). It is a way to encrypt your access code. It's been around since about 2014, so it's possible that a new standard encryption code is being recommended by the time you are reading this. The ed25519 is what is recommended by GitHub currently.

But how does this affect you? Why am I bringing it up? Other than to say, it doesn't verify with your Security Officer, GitHub, or the internet, in general, to verify your information is being securely held. You can use this code on any number of ssh generations as this will always generate a new and unique code even given the exact same output; HOWEVER, to avoid confusion when creating multiple keys, I recommend changing the file name to reflect the associated account.

So, you will repeat steps two and three. After that, things can get tricky. You can have multiple ssh keys on a single account (this may require additional variables to be included in your config file), or you have multiple accounts on a single computer. For example, you may have a work computer and a home computer but need access to the same account, which we will see in Step 4.

4. Update Internal

Remember the config file I mentioned before? Well, you will now!

Let's go back to the ~/.ssh folder. If you have a config file, open it. If you don't

touch config 

You can use VIM, but I prefer to use open the file in TextEdit.

open config // or linux for vs code code . config

There are four essential lines you will need

Host github.com
HostName github.com
User username_on_account
IdentityFile ~/.ssh/path_and_name_to_private_ssh_key

Uh? Okay, let's break it down, bottom-up.

IdentityFile, this is the path to the private ssh key that pairs with the pub ssh key you added to your account.

User, this is the username on your GitHub account, not your email, but your username.

HostName is the URL we pass this information to. This will always be github.com unless you use this SSH key for a different system.

Host, this is a tricky one. It seems straightforward, but not quite. "Why?" you say. Do you know that funny part at the beginning of your clone URL?

git@github.com:listenToRipley/learn_docker.git

Yeah, the part that comes before the colon? That is what drives the host time. Since you will have multiple accounts on this computer, you cannot use the same Host again, or you will get a lot of failure notices when you push your content.

My solution?

Whichever account will be your primary, keep the github.com; for your secondary, tertiary, etc., go ahead and add a dash and an identifier. So your Host will look something like this:

Host github.com-project

Save your changes, and you should be able to Clone and Push to any repos you need!

Notes on Clone

If you use my recommendation above, ANY clones you do, you must include your -identifier to the clone.

Example:

git@github.com-project:listenToRipley/learn_docker.git

If you do not include this, it will try to pull from your primary account, and you will get a lot of errors.

Well, that's it! You did it! Give yourself a high-five, do a happy dance, and stay safe. Happy coding.

Sources / My journey:

--

--

N.Kendrick

A Full Stack Developer. Remember, write the documentation you wish you had had when you started your project. https://highermay.dev/