多git帐号的SSH key切换

1.同一台电脑可以有2个git账号(不同网站的)

首先不同网站,当然可以使用同一个邮箱,比如我的github,gitlab,bitbucket的账号都是gotodiscuss[at]gmail.com 这时候不用担心密钥的问题,因为这些网站push pull 认证的唯一性的是邮箱 比如我的windows 上 2个账号一个gitlab 一个github (用的都是id_rsa)

host github
hostname github.com
Port 22

host gitlab.ikeepstudying.com
hostname gitlab.ikeepstudying.com
Port 65095

不需要指定key的位置。因为默认读取id_rsa了

这样子使用起来没有任何区别,remote 也想平时一样操作即可。因为邮箱是相同的。

2.同一台电脑有2个github账号?咋办

比如我服务器上模拟的2个用户

#gotodiscuss@gmail.com   我在gitlab的第一个账号gotodiscuss
host gitlab.gotodiscuss.com
hostname gitlab.gotodiscuss.com
Port 65095
User gotodiscuss
IdentityFile /home/suzie/.ssh/id_rsa

#  我在gitlab的第2个账号test
host gitlab-test.gotodiscuss.com
hostname gitlab.gotodiscuss.com
Port 65095
User test
IdentityFile /home/suzie/.ssh/id_rsa_second

#837368104@qq.com 我在github的账号 
host github-osteach.com
hostname github.com
Port 22
User osteach
IdentityFile /home/suzie/.ssh/id_rsa_second

所以,config基本写法:

Host myhost(这里是自定义的host简称,以后连接远程服务器就可以用命令ssh myhost)[注意下面有缩进]
User 登录用户名(如:git)
HostName 主机名可用ip也可以是域名(如:github.com或者bitbucket.org)
Port 服务器open-ssh端口(默认:22,默认时一般不写此行)
IdentityFile 证书文件路径(如~/.ssh/id_rsa_*)

这种情况下,需要几点注意

1.remote pull push的时候有问题,因为要设置邮箱问题了 pull的时候识别的是邮箱,2个github账号,2个邮箱,我们自然不能使用global的user.email了

git config -e [--global] # 编辑Git配置文件

git config --global user.name yanhaijing
git config --global user.email yanhaijing@yeah.net

git config --list #查看配置的信息

git help config #获取帮助信息
#1.取消global
git config --global --unset user.name
git config --global --unset user.email

#2.设置每个项目repo的自己的user.email
git config  user.email "xxxx@xx.com"
git config  user.name "suzie"

 

之后push pull就木有问题了

备注

生成ssh key

ssh-keygen -t rsa -C “your mail” (当前目录) 然后可以命名默认id_rsa 或者id_rsa_second 把对应的pub放到公共服务器上。

ssh-keygen 命令要在项目目录里面运行,并且要取消全局 user.name 和 user.email,建立局部user.name 和 user.email (跟你的git账户一致),这个害的我浪费了很多时间。

ps.

sourcetree 如果使用多账号时,记得修改config文件,同时在”Tools”菜单的”add ssh key”中选择私钥,最重要的是clone时要使用修改后的path.

1.//git clone 项目时提示Host key verification failed

使用 ssh 协议时会验证对方服务器的 key。ssh无法确认服务器出示的 key 是可信认的,所以问你这个 key 是不是真的是你要连接的那个服务器的。你没说「yes」所以 ssh 认为你不想继续连接,结果当然是连接失败

2.//git clone Permissions 0644 for ‘~/.ssh/id_rsa’ are too open

只要把权限降到0600就可以了

3.//Bad owner or permissions on /home/xxx/.ssh/config

ssh config文件权限的问题,把权限降到0600就可以了.

其他:在GitHub多个帐号上添加SSH公钥

 

本文:多git帐号的SSH key切换

Loading

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.