How To Use Multiple SSH Keys For Different Git Accounts » The Bored Dev (2024)

Table of Contents
Introduction Conclusion Related FAQs

Spread the love

In this article we will learn how to use multiple SSH keys for different Git accounts on your machine. The most common reasons to need this is in the case that you have a different account for work and personal projects or even if you are a freelance/contractor working for different organisations.

Let’s start!

Introduction

We will take you through the steps you will need to follow in order to be able to push to your git repositories using the right author and having the right permissions every time.

If you are using the right user in your commit, but still using the wrong SSH key, you will get an error message similar to this:

$ git push ERROR: Permission to theboreddev/myrepository.git denied to theboreddev.fatal: Could not read from remote repository.Please make sure you have the correct access rightsand the repository exists.

Let’s see what do we have to do to fix this issue.

1 – Create a New SSH Key

The first thing you will need is an SSH key that will be added to your corresponding Github account. If you don’t have an SSH key yet and you don’t know how to generate an SSH key, check our article “How to Generate SSH Keys”.

Once you have your SSH key, it’s time to add it to Github!

2 – Add SSH Key to Github Account

Once you have your new SSH key, you will have to add it as a valid SSH key into your Github account. That’s the way that Github will know that you are a “trusted” user for that account when connecting through SSH.

If you don’t know how to do that, check our article “How to Add SSH Keys to Github”.

3 – Modify SSH config

In order to be able to determine what SSH key will be used for each repository, we will need to add configuration to our SSH config to associate each key to certain repositories.

To modify your SSH config you can run:

vim ~/.ssh/config

Once in your editor, you will need two different sections for github.com hosts. Our configuration will look like this:

Host github.com Hostname github.com AddKeysToAgent yes UseKeychain yes IdentityFile ~/.ssh/id_ed25519Host github.com-theboreddev Hostname github.com AddKeysToAgent yes UseKeychain yes IdentityFile ~/.ssh/id_boreddev

The first SSH key (id_ed25519) will be used for every github.com repository by default. We will see now how to use the alternative key for other repositories.

4 – Associate Repository to New Key

Once we have that configuration in place, the next step is not very intuitive but we’ll try to make it clear.

We have to either clone our repository in a different way or modify the remote origin url for our existing repositories.

Let’s see how to do it in both cases:

4.1 – Clone New Repository

If we are going to clone a new repository, instead of using the original github.com url, we will clone it in a slightly different way:

git clone git@github.com-theboreddev:theboreddev/myrepo.git

Notice that the hostname has changed to use the host we previously configured in our ~/.ssh/config file!

4.2 – Modify Remote Url For Existing Repository

If instead we already have a local repository we would like to configure to be able to push using the new SSH key, we’d have to do the following:

git remote set-url origin git@github.com-theboreddev:theboreddev/myrepo.git

That’s all! It should work now as well.

5 – Set User For Current Repository

One more thing to do, if we have a global username and email already defined in Git, is to override the username and email for our current repository. This step will only be needed if you need to use a different user from the one you normally use.

In order to do so, you’d just have to do something similar to this inside your git repository:

git config user.name "theboreddev"git config user.email "[email protected]"

That’s all! If you commit a change and try to push to your repository, it should work!

Conclusion

In this article we have learned how to configure our machine to be able to use multiple SSH keys for different Github accounts in a secure manner. Not having to worry about how to select one or the other, just be adding a suffix to our url’s hostname!

That’s all from us today! We hope you have learned something new today and enjoyed this article as much as we did writing it!

Thanks for reading us! See you soon!

Related

How To Use Multiple SSH Keys For Different Git Accounts » The Bored Dev (2024)

FAQs

How To Use Multiple SSH Keys For Different Git Accounts » The Bored Dev? ›

If you want to use the SSH protocol for both accounts, you can use different SSH keys for each account.

Can I have multiple SSH keys for different GitHub accounts? ›

If you want to use the SSH protocol for both accounts, you can use different SSH keys for each account.

Can I use one SSH key for multiple accounts? ›

So, No - you'll need a separate key for each account. Although you need multiple ssh key pairs for multiple accounts you can configure multiple ssh identities and use via aliases on your machine. You can also just use your username in place of "git" or "hg". Still need separate keys, though.

Can you have multiple SSH keys? ›

You can use a single key to access any number of hosts. The public key must be included in the authorized_keys file on the server for password-less access. You can also use different keys for accessing different systems, if you prefer. Interacting with Git Fusion requires a separate key.

Can I use the same SSH key for multiple repositories in GitHub? ›

If you use multiple repositories on one server, you will need to generate a dedicated key pair for each one. You can't reuse a deploy key for multiple repositories.

Can I use the same SSH key for different GitLab accounts? ›

My guess would be that you're using the same SSH key for both private and job things, but (as the error message suggests) you can't upload the same SSH key to multiple accounts, so you'll need to create a new key for one of the use cases (my suggestion would be to create two, one for each use case, and stop using the ...

Can you use the same SSH key twice? ›

You can't use the same key between accounts. You must create new keys for each individual Bitbucket account. In this case, I would suggest you add this SSH key on this Jenkins user (not as an access key to the repository) and give them read access to the repositories you need.

Should I use one SSH key for everything? ›

Yes, a single private key can be used to log in to multiple Linux systems. It's even recommended to do so. The process is very straightforward. When you use an utility such as ssh-keygen, this generates a so-called private/public key pair.

How many SSH keys can each user have assigned? ›

Explanation: In the context of Secure Shell (SSH), each user can generate multiple SSH keys. Essentially, no strict limit is set on how many SSH keys a user can have assigned. The principal purpose of the SSH key pair, comprised of a public and a private key, is to authenticate a client to a server.

Can I use the same SSH key on two bitbucket accounts? ›

Bitbucket doesn't allow you to use the same SSH Key on more than one account.

How to switch between SSH keys in git? ›

Multiple SSH Keys settings for different github account
  1. create different public key. create different ssh key according the article Mac Set-Up Git $ ssh-keygen -t rsa -C "[email protected]" ...
  2. Modify the ssh config. $ cd ~/.ssh/ $ touch config $ subl -a config. ...
  3. Clone you repo and modify your Git config.

Is it okay to reuse SSH keys? ›

It would be safer to create new key and invalidate the old one in bitbucket configuration, but you can reuse it. It is enough to copy private and public part of the key from previous computer to a new one. Disk location should be ${home}/. ssh where ${home} depends on your operating system.

Can I have two SSH connections? ›

Of course, you cannot establish two nested ssh connections, but we can do some tricks on server A to run a pseudo bjobs which actually runs the real bjobs on server B. Maybe you have already guessed, we need to, on server A, run bjobs on server B via ssh without interactively entering the password.

Do I need a new SSH key for each repository? ›

No. An SSH key is used to establish a secure connection between a computer and CodeArts Repo. After you create an SSH key pair on your computer and add the public SSH key to your account in CodeArts Repo, you can use the key to access all repositories under the account on your computer.

Can I use same SSH key for different projects? ›

1 Answer. You can use the same key for multiple remote systems. The private key is never shared with the service you log in to, so authenticating towards Github will not allow Github to impersonate you.

How do I use multiple SSH keys in GitHub windows? ›

3. Create a Config File
  1. Step 1: Navigate to the .ssh Folder. Begin by opening your terminal and accessing your . ...
  2. Step 2: Configuration Setup. Now, it's time to define the host names and associate them with the respective SSH keys for each of your GitHub accounts. ...
  3. Step 3: Save and Close. ...
  4. Step 4: Terminal Commands.
Oct 30, 2023

Can multiple users SSH at the same time? ›

The short answer - Yes. It usually works by default. The long answer - Depending on what you are using it for, it may slow down with multiple connections, but that is a bandwidth issue, not an ssh issue.

Can I use same SSH key for different machines? ›

To use your SSH key to log in to another remote machine, you must copy the public key file to the remote machine.

Top Articles
COSMOS PRICE PREDICTION 2024 - 2025
Delete a file - Microsoft Support
Napa Autocare Locator
Www.politicser.com Pepperboy News
Comforting Nectar Bee Swarm
Sportsman Warehouse Cda
Beds From Rent-A-Center
Crime Scene Photos West Memphis Three
Dark Souls 2 Soft Cap
Seth Juszkiewicz Obituary
Aita Autism
Craigslist Cars Nwi
6th gen chevy camaro forumCamaro ZL1 Z28 SS LT Camaro forums, news, blog, reviews, wallpapers, pricing – Camaro5.com
The Shoppes At Zion Directory
Restaurants Near Paramount Theater Cedar Rapids
Swedestats
Caledonia - a simple love song to Scotland
EASYfelt Plafondeiland
Winco Employee Handbook 2022
Ac-15 Gungeon
Chime Ssi Payment 2023
Turbo Tenant Renter Login
Cb2 South Coast Plaza
At 25 Years, Understanding The Longevity Of Craigslist
Panolian Batesville Ms Obituaries 2022
No Limit Telegram Channel
208000 Yen To Usd
Table To Formula Calculator
Anesthesia Simstat Answers
Weather Underground Durham
Craigslist Sf Garage Sales
Grand Teton Pellet Stove Control Board
Ixlggusd
Ixl Lausd Northwest
Amici Pizza Los Alamitos
Louisville Volleyball Team Leaks
Reborn Rich Ep 12 Eng Sub
Dr Adj Redist Cadv Prin Amex Charge
The Thing About ‘Dateline’
Silive Obituary
התחבר/י או הירשם/הירשמי כדי לראות.
Exam With A Social Studies Section Crossword
Rocket Lab hiring Integration & Test Engineer I/II in Long Beach, CA | LinkedIn
Aznchikz
Used Auto Parts in Houston 77013 | LKQ Pick Your Part
15:30 Est
Rocket Bot Royale Unblocked Games 66
Coleman Funeral Home Olive Branch Ms Obituaries
Nfsd Web Portal
Buildapc Deals
라이키 유출
Lorcin 380 10 Round Clip
Latest Posts
Article information

Author: Terrell Hackett

Last Updated:

Views: 6579

Rating: 4.1 / 5 (72 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Terrell Hackett

Birthday: 1992-03-17

Address: Suite 453 459 Gibson Squares, East Adriane, AK 71925-5692

Phone: +21811810803470

Job: Chief Representative

Hobby: Board games, Rock climbing, Ghost hunting, Origami, Kabaddi, Mushroom hunting, Gaming

Introduction: My name is Terrell Hackett, I am a gleaming, brainy, courageous, helpful, healthy, cooperative, graceful person who loves writing and wants to share my knowledge and understanding with you.