How to use different ssh credentials for GitHub for the same Linux user
Marcin Wosinek
Posted on July 25, 2021
If you have multiple GitHub users, setting up ssh key can become tricky. You cannot add the same, default key to 2 users. You need to set up your machine to use different keys depending on which identity you want to use. This article will show how to set up ssh config to support additional ssh-key, along with your default key.
Default key set up
I assume you already have one ssh-key set up, it's working OK, and it's somehow similar to what they describe in the doc. This part of the setup will be left unchanged - you will be able to keep on using the standard domain with your standard key.
Additional key set up
First, you need to generate an additional key and make sure it's saved under another name. You can follow this documentation:
$ ssh-keygen -t ed25519 -C "your_email@example.com" -f another-identity
Where `another identity is a file name where the new key is saved.
Testing the new key
Add the another-identity.pub
to your GitHub account, and test it with:
sh
$ ssh -i another-identity -T git@github.com
Where -i another-identity
makes ssh use the non-default key for connection. If all goes well, you will see your other profile name in the GitHub response:
sh
$ ssh -i another-identity -T git@github.com
Hi marcin-test! You've successfully authenticated, but GitHub does not provide shell access.
Configuration for git CLI
With the new key in place, you can create .ssh/config
and set there:
Host another-indenity.github.com
Hostname github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/another-identity
Now, when you can test if this piece of configuration works:
sh
$ ssh -T git@another-idenity.github.com
Hi marcin-test! You've successfully authenticated, but GitHub does not provide shell access.
Checking out with new identity
Now, let's test our access to a private repository available only on our second user:
`
$ git clone git@github.com:marcin-test/test.git
Cloning into 'test'...
ERROR: Repository not found.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
`
Normal clone fails, but if we replace github.com
with another-identity.github.com
:
git clone git@another-idenity.github.com:marcin-test/test.git
Cloning into 'test'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
Now everything works as expected.
Summary
In this article, we have seen how to set up 2 identities for your GitHub projects.
Posted on July 25, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 30, 2024