Search code examples
gitgitlabaccount

GitLab assumes wrong username


When I run ssh -T [email protected] I get the message Welcome to GitLab, @superfake!. "superfake" is a username I have on other services, for example github I think, and on a previous computer... But it is not the correct username for GitLab, and I don't know how they have gotten associated. When I try to fetch a repo from my own project I get the message

remote: 
remote: ========================================================================
remote: 
remote: ERROR: The project you were looking for could not be found or you don't have permission to view it.

remote: 
remote: ========================================================================
remote: 
fatal: Could not read from remote repository.

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

This started a few days ago, and is the case from multiple computers, it's almost spooky, but since the username "superfake" is familiar, I guess I've done something. Any insights?

Edit:

The spookyness is reduced when remembering that I'm using ssh agent-forwarding (-A) for most computers, so it is enough to have problem on my desktop.

An observation: on XUbuntu 22.04, when running ssh -T [email protected] in a new terminal window the name is @superfake but if I open another tab, and run the command there, the name is @superrandom, which is the correct gitlab name, and git fetch works from the second tab! So there seems to be something about the environment.

After doing env > test1 in the first terminal and env > test2 in the second, I compared the environments with vimdiff test1 test2 and found that SSH_AUTH_SOCK=/run/user/1000/keyring/ssh in the firs terminal, while undefined in the other. If doing export SSH_AUTH_SOCK= in the first terminal, I get back the correct username superrandom. Not sure what's going on here.

Edit2: First, gnome-terminal indeed has problems with tabs having different environments, including the variables mentioned above, and I have switched to xfce-terminal (running xubunty).

Secondly, when connecting to github or gitlab, you don't actually connect using a user-name, only using the key, and if that key is valid it is associated with your username. So adding a key, lets say key1.pub to your gitlab user and adding

host *gitlab*
  identityfile ~/.ssh/key1.pub
  identitiesonly yes

to ~/.ssh/config will work. Also doing ssh-add -l could be necessary. Otherwise some other key might be used.

Thirdly, when using agent-forwarding, an intermediate/target computer's local .ssh/config file will override the forwarding, so any machines config file should basically only contain entries like

host nickname
  hostname server1
  user myname

for convenient access to servers, but not other fields that may contradict the forwarded agents settings to those servers.


Solution

  • Check your ssh config file: ~/.ssh/config. If you don't find it, create one.

    For it to work should have something like this:

    # Personal github account: superfake
    Host github.com
       HostName github.com
         User git
       IdentityFile ~/.ssh/id_rsa_superfake
      
    # Enterprise gitlab account: superman
    Host gitlab.com
       HostName gitlab.com
         User git
       IdentityFile ~/.ssh/id_rsa_superman
    

    (Note that IdentityFile points to each user's private key)

    Check more here: https://gist.github.com/oanhnn/80a89405ab9023894df7