Reputation: 101
I'm trying to set multiple GitHub accounts for multiple projects I'm working on. It just won't work.
First of all, I'm on Windows 11.
I've created the keys, they're in C:/Users/User/.ssh/...
I created a config file inside the same /.ssh folder and this is the content:
Host github-user1
HostName github.com
User git
IdentityFile ~/.ssh/github-user1
Host github-user2
HostName github.com
User git
IdentityFile C:\Users\User\.ssh\github-user2
Notice I tried two ways of pointing to the key files. I also tried '/c/Users/User/.ssh/', which also does not work.
I added the public keys to both github accounts
I added the keys to the ssh agent
I edited the local repo git config file to use the correct remote origin and user, it looks like this:
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
sshCommand = ssh -i ~/.ssh/github-user1
[user]
name = user1
email = [email protected]
[remote "origin"]
url = git@github-user1:user1/repositoryName.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
I tested authentication like so:
ssh -T github-user1
This worked, the user was successfully authenticated, for both users in both repositories
I checked the config with 'git config --list' and this was the output:
diff.astextplain.textconv=astextplain
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.process=git-lfs filter-process
filter.lfs.required=true
http.sslbackend=openssl
http.sslcainfo=C:/Program Files/Git/mingw64/etc/ssl/certs/ca-bundle.crt
core.autocrlf=true
core.fscache=true
core.symlinks=false
pull.rebase=false
credential.helper=manager
credential.https://dev.azure.com.usehttppath=true
init.defaultbranch=master
core.sshcommand=C:/Windows/System32/OpenSSH/ssh.exe
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
core.sshcommand=ssh -i ~/.ssh/github-user1
user.name=user1
[email protected]
remote.origin.url=git@github-user1:user1/repositoryName.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master
Looks correct to me...
Finally I test fetching 'git fetch' and this is what I get:
ssh: Could not resolve hostname github-user1: Name or service not known
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
This happens with both users, that both authenticate correctly. No matter what I try, this is all I get.
Last thing I tried is 'ssh -vvv github-user1' to see whether it could connect, it did. But, this rule was in the output:
debug1: Will attempt key: [email protected] RSA SHA256:blahblahblah agent
So appearantly it tries to connect with the wrong user. Also, if I prompt for this:
ssh -T [email protected]
The same wrong user comes up. So, where does this come from? I did not configure this anywhere. Before I started to configure ssh keys, I logged in and out of both accounts a few times, so I guess something got saved somewhere, but I haven't been able to find it anywhere. Anyone any idea where to look?
Also, I noticed that on github, it says the key as never been used:
I did delete the git user in the Windows References, so that is not the problem.
Upvotes: 0
Views: 59
Reputation: 101
Ok, so here's my solution. As I already mentioned, when I tested ssh -T [email protected]
I got the wrong user authenticated. I did not define [email protected] as a host anywhere. So I decided to set user1 as a default github.com host like so:
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/github-user1
Host github-user2
HostName github.com
User git
IdentityFile ~/.ssh/github-user2
The local repository git config file now looks like this:
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
sshCommand = ssh -i ~/.ssh/github-user1
[user]
name = user1
email = [email protected]
[remote "origin"]
url = [email protected]:user1/repositoryName.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
Also, I forgot to add the line sshCommand = ssh -i ~/.ssh/github-user2
in the local repository git config file for user2, so I added that as well. The config for user2 now looks like this:
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
sshCommand = ssh -i ~/.ssh/github-user2
[user]
name = user2
email = [email protected]
[remote "origin"]
url = git@github-user2:user2/repositoryName.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
These steps fixed the problem for me. I can now fetch from git on both remote repositories and I can see in both github accounts that my keys are being used.
Upvotes: 0