7

I am trying since more than 3 hours to setting up multiple account for github and litteraly tired. I have tried out almost all possible way describe here, github and articles and none of them worked. I am completely newbie to github and Unix as well. So need your help to fix this. Here below what I am doing

I am using Windows 7 and have set two ssh key for two different accounts.

  1. id_rsa
  2. id_rsa_ac2

Than created config file in .ssh directory of the User and added below code

#Account one
Host github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile /c/Projects/.ssh/id_rsa

#Account two
Host ac2.github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile /c/Projects/.ssh/id_rsa_ac2

Now I am trying to add remote by using below code

git remote add origin git@ac2.github.com:myaccount/my.git

and push with bellow code

git push origin master

But when I am trying to push it is giving me Error: Error: Permission to myaccount/my.git denied to {account}. // where it is considering default user account and not for ac2 user account fatal: Could not read from remote repository.

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

Thanks a lot..

Additional Info:

I have test the id_rsa_ac2 and giving successfully authenticated message. But the strange thing is giving username with original account not with the ac2 account username

Hi {username!} You've successfully authenticated, but GitHub does not provide shell access. //here user id should be from ac2 but it is showing userid from id_rsa and not from id_rsa_ac2

INFORMATION: Final Code

@VonC's answer worked and adding final code as my answer if anyone want to use.

Community
  • 1
  • 1
Code Lover
  • 6,794
  • 18
  • 66
  • 129

4 Answers4

4

So according to @VonC's answer here what I have done.

  1. I have generate ssh key for another account and added with id_rsa_ac2 (where ac2 is for second account)
  2. Than just cross checked either it works with ssh -T ac2.github.com
  3. Created config file (without extention) in /c/Users/yourname/.ssh/ directory

Here is the code what I used for config file

#Account one
Host github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile /c/Users/yourname/.ssh/id_rsa
    User git

#Account two
Host ac2.github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile /c/Users/yourname/.ssh/id_rsa_ac2
    User git

So now once you done this you can start to use both account as you need.

for main account I added remote as origin with git remote add origin git@github/youraccount/rep.git Than to push use git push origin master this will upload to your first account.

To add remote for second (ac2) account used git remote add ac2 ac2.github/yoursecondaccount/rep.git Than to push use git push ac2 master this will upload to the second (ac2) account.

To check if it has added remote use git remote -v and incase if you want to remove anyone than use git remote rm origin where origin is your added remote.

Hope this information will helps to other who is having the same issue.

Thanks again to @VonC

Code Lover
  • 6,794
  • 18
  • 66
  • 129
  • It's all because of you! Thanks again for your great help and appreciation :) – Code Lover Nov 26 '12 at 20:46
  • @CodeLover I left another answer with a script to automate everything. ( although the process is a bit different it works for GitLab, and I would assume GitHub as well.) let me know if you think I should add anything. – rustysys-dev Dec 23 '17 at 05:09
2

For your config to be taken into account, you need to use its Host name in your remote address:

git remote add origin ac2.github.com:myaccount/my

If you have defined a HOME environment variable (which isn't defined by default on Windows, but is defined if you are using the msysgit git-cmd.bat) to a directory under which you have your .ssh directory, with its id_rsa_ac2 private key, and id_rsa_ac2.pub public key, then it will work.

Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • Great help! it's done and working both account. Just for knowledge I have to use your both answers where you described to define `User git` than it worked. Thanks a lot – Code Lover Nov 26 '12 at 15:08
  • @VonC I left another answer with a script to automate everything. ( although the process is a bit different it works for GitLab, and I would assume GitHub as well.) let me know if you think I should add anything – rustysys-dev Dec 23 '17 at 05:10
  • @CodeLover 6 years later and my answer is not good enough? I agree, the other answers improve on it, but I still had answered your original question – VonC Nov 21 '18 at 13:08
  • @VonC it isn't about that but to make the answer selected which is more useful to other with detailed steps as well. Anyway, I have selected your answer once again. – Code Lover Nov 22 '18 at 05:58
  • @CodeLover Agreed: please do select the answer you think will help users. I was just surprised yesterday ;) – VonC Nov 22 '18 at 05:59
  • @VonC no problem :) It happens – Code Lover Nov 28 '18 at 06:04
2

Here is a script to Automate the addition of two GitLab Accounts to your setup.

setup-gitlab.sh

#!/bin/bash

# VERIFIED FOR FEDORA 27 MATE (Likely to work in others distros)
# Multi Account SSH for GitLab/OpenSSH Setup.
ROOT=root
if (( whoami == $ROOT ))
    then 
    echo "Run as standard user"
elif [[ -z $1 || -z $2 ]]
    then
    echo "command usage: setup-gitlab.bash workemail@domain.com homeemail@domain.com"
elif [[ ! $1 =~ .*@.*\..* ]]
    echo "Work email is not in the correct format. Must match regex .*@.*\..*"
elif [[ ! $2 =~ .*@.*\..* ]]
    echo "Home email is not in the correct format. Must match regex .*@.*\..*"
else
    HOMEEMAIL=$1
    WORKEMAIL=$2
    USRNAME=`whomai`

# /home/<username>/.ssh/
# ├── config
# ├── home-gitlab-key
# ├── home-gitlab-key.pub
# ├── known_hosts
# ├── work-gitlab-key
# └── work-gitlab-key.pub

#Executed to match the above directory.
    ssh-keygen -t rsa -C "$WORKEMAIL" -b 4096 -f work-gitlab -N ""
    ssh-keygen -t rsa -C "$HOMEEMAIL" -b 4096 -f home-gitlab -N ""

# Agent Configuration Setup (.ssh/config)
    cat >> ~/.ssh/config <<EOF
Host gitlab-work
  HostName gitlab.com
  User git
  IdentityFile /home/$USRNAME/.ssh/work-gitlab-key

Host gitlab-home
  HostName gitlab.com
  User git
  IdentityFile /home/$USRNAME/.ssh/home-gitlab-key
EOF

# Agent Setup (potentially optional???)
    cat >> ~/.bashrc <<'EOF'
eval "$(ssh-agent -s)"
for i in `ls ~/.ssh/*.pub` ; do ssh-add ${i::-4} ; done
EOF

    . .bashrc

fi

After running the script you will need to copy the contents of the two public keys created to each GitLab account respectively.

Another note, when using git clone git@gitlab.com:<account>/<project>.git your should replace gitlab.com as follows.

git clone git@gitlab-home:<account>/<project>.git

and

git clone git@gitlab-work:<account>/<project>.git

respectively.

rustysys-dev
  • 753
  • 1
  • 7
  • 21
0

I am using this script, that switch indentity and other all neccessary like git setting

https://gist.github.com/milosjanda/a86ebc039293f22fabe723024ec42b46

if [[ -f ~/.ssh/.work ]]; then
  echo "swith to HOME"
  # mv ~/.ssh/id_rsa ~/.ssh/work; mv ~/.ssh/home ~/.ssh/id_rsa
  rm ~/.ssh 2> /dev/null
  ln -s ~/.ssh-work/home ~/.ssh
  git config --global user.email "my.name@gmail.com"
else
  echo "swith to WORK"
  # mv ~/.ssh/id_rsa ~/.ssh/home; mv ~/.ssh/work ~/.ssh/id_rsa
  rm ~/.ssh 2> /dev/null
  ln -s ~/.ssh-work/work ~/.ssh
  git config --global user.email "my.name@company.eu"
fi

# Delete all identities from ssh-agent
ssh-add -D

# Add new identity to ssh-agent
ssh-add