Sometimes it happen when we have to use multiple GitHub accounts on a single machine, but it quite tricky to handle in a proper way. Let me describe full steps including some issues I’ve faced and its fixes.
Step 1 – Setup SSH Keys
# lets assume the first user account is: firstname.lastname@example.org # and the second one: email@example.com # use git bash # change directory to ~/.ssh/ which located on: %USERPROFILE%\.ssh $ cd ~/.ssh/ $ ssh-keygen -t rsa -C firstname.lastname@example.org # skip passphrases and the output will look like: Generating public/private rsa key pair. Enter file in which to save the key (/c/Users/Admin/.ssh/id_rsa): id_rsa_user1 Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in id_rsa_user1 Your public key has been saved in id_rsa_user1.pub The key fingerprint is: SHA256:9aksjdfhkjh/YdkfjsljkfyAjduwiuywerhjk email@example.com The key's randomart image is: +---[RSA 3072]----+ |OBO. + | |+O+=B+o . | .. # same for the second user $ ssh-keygen -t rsa -C firstname.lastname@example.org .. Enter file in which to save the key (/c/Users/Admin/.ssh/id_rsa): id_rsa_user2 ..
In result four files will be generated in
id_rsa_user1 id_rsa_user1.pub id_rsa_user2 id_rsa_user2.pub
Step 2 – Prepare configuration file
%USERPROFILE%\.ssh ) create a
config and enter the keys we’ve just generated:
# github for email@example.com Host user1 HostName github.com User git IdentityFile ~/.ssh/id_rsa_user1 # github for firstname.lastname@example.org Host user2 HostName github.com User git IdentityFile ~/.ssh/id_rsa_user2
Step 3 – Add newly generated keys to GitHub.com
Go to GitHub.com ➤ Settings ➤ SSH and GPG keys ➤ SSH Keys ➤ New SSH Key:
Enter some title and in the
Key field enter the public key content from
~/.ssh/id_rsa_user2.pub to the related GitHub accounts respectively.
Step 4 – Register Identities
# check already existed identities $ ssh-add -l # if there is a case & you'd like to delete already existed ones, use the next command: $ ssh-add -D # register new ones $ ssh-add.exe id_rsa_user1 Identity added: id_rsa_user1 (email@example.com) $ ssh-add.exe id_rsa_user2 Identity added: id_rsa_user2 (firstname.lastname@example.org)
But, if you face the next type error, like:
Could not open a connection to your authentication agent.
Then you’ve to initialize the ssh-agent:
$ exec ssh-agent bash
Step 5 – Verify
To verify configuration we need to test prepared keys by running next commands:
$ ssh -T user1
If you see errors like:
ssh: Could not resolve hostname user1 .. or smth else, please check the previous steps.
In case of success you should see the message smth like this:
# Hi <USER1_USERNAME>! You've successfully authenticated, but GitHub does not provide shell access.
The same should appear in case of running:
ssh -T user2 command, but be careful, if the
WARNING: If in the second time you’ll see the same username for the
user1, it mans that there are some errors, might be the key was not registered to the proper user, so git by default will use the
user1 account instead. So be sure that the testing key output will be with the proper username, like:
# Hi <USER2_USERNAME>! You've successfully authenticated, but GitHub does not provide shell access.
Step 6 – Test PUSH Requests
$ git remote add origin git@user1:some-github-access-granted-account/project.git $ git push origin main
$ git remote add origin git@user2:some-github-access-granted-account/project.git $ git push origin main
WARNING: Be careful, when submitting with second user, despite that correct user account (in our case
user2) is used, author on GitHub.com still could be the
user1 ‘s name, in case if both the users have access to the shared project. At least, I faced the problem at the moment there is a very simple solution for it, namely on
user2 ‘s copy of repo set local git configuration using next command:
$ git config --local user.email email@example.com
test user in verbose mode:
$ ssh -T user1
If getting error like:
Bad owner or permissions on ~/.ssh/config change permissions:
chmod 600 ~/.ssh/config
Config path on CentOS for regular users: