2

I created a script to create remote repositories on github and in case the machine has more than one user who access the script to create remote repositories and for that I need a github token key I created the token key at Token Github and created the key for SSH access on github with the following command:

Account github.com/usuario1

linuxuser@linuxmint$ ssh-keygen -t rsa -b 4096 -C "[email protected]"

/home/linuxuser/.ssh/id_rsa_hotmail_github_usuario1

getting like below:

Enter file in which to save the key (/home/linuxuser/.ssh/id_rsa): /home/linuxuser/.ssh/id_rsa_hotmail_github_usuario1


Account github.com/usuario2

linuxuser@linuxmint$ ssh-keygen -t rsa -b 4096 -C "[email protected]"

/home/linuxuser/.ssh/id_rsa_yahoo_github_usuario2

getting like below:

Enter file in which to save the key (/home/linuxuser/.ssh/id_rsa): /home/linuxuser/.ssh/id_rsa_yahoo_github_usuario2

List keys ssh

ls -a ~/.ssh/
id_rsa_hotmail_github_usuario1  id_rsa_hotmail_github_usuario1.pub
id_rsa_yahoo_github_usuario2  id_rsa_yahoo_github_usuario2.pub

I copied and put each key in your account. I'm logged into my linux user where the keys were created

In the script I changed the vector of users putting the users from github leaving it as:

USUARIOS_GIT=("usuario1" "usuario2" "github_usuario3")

and the vector of the tokens I put the token of each github user

CHAVE_TOKEN=("dhkssklfhfkh" "kfhfflhd" "chave_token_github_usuario3")

that is, there are two github accounts in which I have to create remote repositories with the script logged into my machine with the user linuxuser

linuxuser@linuxmint/Developement$ gitpratico usuario1 projeto1

it shows the error below:

git push [email protected]:usuario1/projeto1.git master
ERROR: Permission to usuario1/projeto1.git denied to usuario2.

creates an empty repository but won't let me make any changes or commits after that not uploading the repository files to github.com/usuario1 it looks like it's trying to access usuario2 who owns the yahoo account.

For example, if a company machine has only one user and this machine is accessed by up to three people at different times with the same machine user and they need to use the script to create remote repositories in different github users, it presents this problem by creating the empty remote repositories and not letting any commits be made.

How do I solve this problem to be able to create repositories in different accounts without having to log in to another linux user?

Script being used to create remote repositories under different github users Script gitpratico_multiuser


I did exactly this configuration using only my user and even so it creates the empty repository not adding the files that exist in the project folder.

My file ~/.ssh/config this

# Default GitHub user (usuario1)
Host github-hotmail  
  HostName github.com
  IdentityFile ~/.ssh/id_rsa_hotmail_github_usuario1
  IdentitiesOnly yes
  User git

# Github user (usuario2)
Host github-yahoo  
  HostName github.com
  IdentityFile ~/.ssh/id_rsa_yahoo_github_usuario2
  IdentitiesOnly yes
  User git

Add ssh private keys to your agent:

$ ssh-add ~/.ssh/id_rsa_yahoo_github_usuario1
$ ssh-add ~/.ssh/id_rsa_yahoo_github_usuario2

testing the connection

in user1

$ ssh -T git@github-hotmail
Hi usuario1! You've successfully authenticated, but GitHub does not provide shell access.

in user2

$ ssh -T git@github-yahoo
Hi usuario2! You've successfully authenticated, but GitHub does not provide shell access.

When typing gitpratico_multiuser user1 <repository1>

if i type git status it does not show that there are files to be uploaded

and when i type:

When typing gitpratico_multiuser user2 <repository2>

shows the table below:

ERROR: Permission to usuario2/repository2.git denied to usuario1.
fatal: Could not read from remote repository.

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

but I still have another problem when I log in another linux user and put the settings below:

root@linuxmint# login linuxuser2

userlinux2@linuxmint$ mkdir .ssh

userlinux2@linuxmint$ nano ~/.ssh/config

# Default GitHub user (user1)
github-hotmail host
   HostName github.com
   IdentityFile /home/userlinux1/.ssh/id_rsa_hotmail_github_usuario1
   Identities Only yes
   User git

# Github user (user2)
github-yahoo host
   HostName github.com
   IdentityFile /home/userlinux1/.ssh/id_rsa_yahoo_github_usuario2
   Identities Only yes
   User git

linuxuser2@linuxmint$ sudo ssh-add /home/userlinux1/.ssh/id_rsa_yahoo_github_user2

[sudo] senha para linuxuser2:     
Could not open a connection to your authentication agent.

linuxuser2@linuxmint$ sudo ssh-add /home/userlinux2/.ssh/id_rsa_hotmail_github_usuario1

[sudo] senha para linuxuser2:     
Could not open a connection to your authentication agent.

I can't create repositories for github user1 and user2 logged into another linux user, I tried to create the .ssh folder and copy the private keys to linuxuser2 but I also didn't get results how can I solve this problem I searched and didn't find anything to regarding how to use the same key of github_usuario1 and github_usario2 created in userlinux1 in another different linuxuser2.

How can I solve this problem?

1 Answer 1

1

The root cause of this is the way Github have chose to use SSH keys. There is a work around...

Normally SSH should specify the user it wishes to login as. However Github have chosen to identify the user only using the public key. Notice you always specify git@github.com, so you are always telling SSH to login as the user git.

So normally you would have just one SSH key attached to both accounts, and specify which user you wanted, but you cannot do that here because the SSH key will be used to identify which account.

This is problematic because your SSH command line will try every public key you give it, and stop at the first one that succeeds. This means you will accidentally login as the wrong user, and then Github will tell you "Access Denied" because the successful login cannot access the desired Git repository.

People might be tempted to call it "wrong" and it certainly causes a bit of a headache in this context. This setup is not wholly unusual though.


What can you do about it?

You can setup profiles in your SSH configuration: there's a reasonably nice example here.

Create or edit the file ~/.ssh/config/ and add two profiles. I'll call them jim and bob:

Host github-jim
   HostName github.com
   IdentityFile ~/.ssh/jim_private_key
   IdentitiesOnly yes
   User git
   
Host github-bob
   HostName github.com
   IdentityFile ~/.ssh/bob_private_key
   IdentitiesOnly yes
   User git

Now you just have to make a minor alteration to the git URL to tell SSH which key to use:

Eg:

git clone github-jim:jim/projeto1.git

git clone github-bob:bob/projeto1.git

Notice how we replace [email protected] for either github-jim or github-bob to specify the desired user, and so select the right private key file.

1
  • I reviewed the script and looked at the answers here and noticed that my mistake was really the lack of the host to the ssh link in the possible syntaxes. ` git push git@github-hotmai:user1/project1.git master git push github-hotmail:user1/project1.git master ` and for github_user2 ` git push git@github-yahoo:user2/project2.git master or git push github-yahoo:user2/project2.git master `
    – Jean
    Apr 23, 2023 at 22:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .