5

Folks, it’s my first time to manage multiple SSH keys for multiple users. I point out in the following all the steps I carried out:

Assume I have 2 GitHub users attached to 2 different GitHub accounts, user1 and user2 respectively.

1. I was able to generate different SSH keys for each user

2. Similarly, I have uploaded the respective private keys to each user account

3. Thereafter, I set up the SSH config file like this:

Host user1.github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_git_user1      # private key user1

Host user2.github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_git_ user2      # private key user2 

4. I cleared the SSH cache and load the new identities

ssh-add -D *
eval `ssh-agent -s`
ssh-add ~/.ssh/id_git_ user1
ssh-add ~/.ssh/id_git_ user2

and after issuing ssh-add -l, both keys are listed down

5. Whenever I create a new repo to any of them, I’m used to updating the local file .git/config.
For instance, I append the following config for each repo related to user1 manually:

[user]
    name = user1
    mail = [email protected] 

and I did the same for user2 repositories

The problem is, whenever I load both keys at step 4, I was unable to do git push using user1, and it throws this error.
Let’s assume repo_user1 is a newly created repository related to user1.

ERROR: Permission to user1/repo_user1.git denied to user2
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

I doubt the problem is, git failed to load the right key of the current user cause when I redo step 4 and omit to load the second key, it works fine. The error also says denied to user2 though my config is set for user1.

I really appreciate any workaround to avoid repeating the same steps each time I push my update.

Thanks,

3
  • The issue here is that as far as ssh is concerned, they are both the same user named "git". Commented Oct 21, 2020 at 14:39
  • @jordanm could you give more detail? Commented Oct 21, 2020 at 14:41
  • If you run git remote get-url origin on your repos, you will see it always shows [email protected]. The ssh username is always "git" with github. Commented Oct 21, 2020 at 14:56

2 Answers 2

5

Try with the follwoign ~/.ssh/config file:

Host user1.github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_git_user1      # private key user1
    User git

Host user2.github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_git_ user2      # private key user2 
    User git

Then, for repo1, make sure to use the right SSH URL:

cd /path/to/repo1
git remote set-url user1.github.com:user1/repo_user1.git

That will use ~/.ssh/id_git_user1.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this after facing lot of issue this works better for multiple user

First, one needs to generate the SSH keys

ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_work -C "[email protected]"

ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_personal -C "[email protected]"

Write this to get the contents inside the keys

cat id_ed25519_personal.pub // added to ssh keys in personal github

cat id_ed25519_work.pub // added to ssh keys in work github

enter image description here

Now, add this to your known SSH keys

ssh-add --apple-use-keychain ~/.ssh/id_ed25519_work
ssh-add --apple-use-keychain ~/.ssh/id_ed25519_personal

Validate using the below command that these SSH keys are added or not

ssh-add -l -E md5

Now, open this file ~/.ssh/config , and add these inside this file

# Work GitHub account [default]
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_work
  AddKeysToAgent yes
  UseKeychain yes

# Personal GitHub account
Host github.com-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_personal
  AddKeysToAgent yes
  UseKeychain yes

For the Validation of SSH keys, to determine if you are able to connect, add this to the terminal

ssh -T [email protected] 
ssh -T [email protected]

enter image description here

Try cloning like this now

enter image description here

# FOR WORK:
git clone [email protected]:work_username/another-project.git

# FOR PERSONAL:
git clone [email protected]:personal_username/another-project.git

OR use this in the add origin if you want to attach one repo to your GitHub

git remote add origin [email protected]:personal_username/repo.git

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.