我正在尝试超过3个小时为github设置多个帐户并且疲惫不堪。我已经尝试了几乎所有可能的方式在这里描述,github和文章,但没有一个工作。我也是github和Unix的新手。所以需要你的帮助来解决这个问题。下面是我正在做的事情
我正在使用Windows 7并为两个不同的帐户设置了两个ssh密钥。
在用户的.ssh
目录中创建配置文件,并添加到代码
#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
现在我尝试使用下面的代码添加远程
git remote add origin git@ac2.github.com:myaccount/my.git
并按波纹管代码推送
git push origin master
但是当我试图推动时它给了我错误:
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.
非常感谢..
其他信息:
我测试了id_rsa_ac2
并提供了成功通过身份验证的消息。但奇怪的是,用原始帐户提供的用户名不是ac2
帐户用户名
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
@VonC的回答是有效的,并且如果有人想要使用的话,可以添加最终代码作为我的答案。
答案 0 :(得分:4)
所以根据@ VonC在这里的答案,我所做的一切。
ssh -T ac2.github.com
以下是我用于配置文件的代码
#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
所以现在一旦你完成了这个,你可以根据需要开始使用这两个帐户。
对于主要帐户我添加了远程作为git remote add origin git@github/youraccount/rep.git
的原点
要推送使用git push origin master
,这将上传到您的第一个帐户。
为使用git remote add ac2 ac2.github/yoursecondaccount/rep.git
的第二个(ac2)帐户添加远程
要推送使用git push ac2 master
,这将上传到第二个(ac2)帐户。
要检查是否添加了远程使用git remote -v
,并且如果您要删除任何人,请使用git remote rm origin
,而原点是您添加的遥控器。
希望这些信息对其他遇到同样问题的人有所帮助。
再次感谢@VonC
答案 1 :(得分:2)
要将您的配置考虑在内,您需要在远程地址中使用其Host
名称:
git remote add origin ac2.github.com:myaccount/my
如果您已经定义了一个HOME
环境变量(在Windows上没有默认定义,但是如果您使用msysgit git-cmd.bat
则定义了该变量)到您拥有.ssh的目录目录,其id_rsa_ac2
私钥和id_rsa_ac2.pub
公钥,然后它将起作用。
答案 2 :(得分:2)
这是一个自动添加两个GitLab帐户到您的设置的脚本。
https://gitlab.com/procyclinsur/Fedora-Environment
#!/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
运行脚本后,您需要分别将创建的两个公钥的内容复制到每个GitLab帐户。
另一个注意事项是,在使用git clone git@gitlab.com:<account>/<project>.git
时,您应该将gitlab.com
替换为以下内容。
git clone git@gitlab-home:<account>/<project>.git
和
git clone git@gitlab-work:<account>/<project>.git
分别
答案 3 :(得分:0)
我正在使用此脚本,用于切换身份和其他所有必需的设置(例如git设置)
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