Welcome to my website, have a nice day!
Dream it, Do it, Make it!

同一台电脑同时使用github和gitlab账号配置ssh keys

git.jpg

如果你的一台电脑上同时使用了github账号和gitlab账号,ssh默认会找id_rsa这个私钥,所以可能会导致后使用的账号无法正常提交代码或clone代码的情况,如报错Permission denied (publickey).,解决办法如下:

假设你已经分别为github账号和gitlab账号生成了ssh key,文件名分别为id_rsa_githubid_rsa_gitlab,并已经将公钥添加到你的github和gitlab账号。接下来配置config文件。

配置config文件

{USER_HOME}/.ssh/目录下,新建一个config文件,在文件中添加如下内容:

# For Github
Host github.com
  HostName github.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_rsa_github

# For Gitlab  
Host gitlab.com
  HostName gitlab.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_rsa_gitlab

以上内容,需要根据自己的文件进行相应的修改;

测试

$ ssh -T [email protected]
Warning: Permanently added the RSA host key for IP address '13.250.177.223' to the list of known hosts.
Hi aitlp! You've successfully authenticated, but github does not provide shell access.

Michael@DESKTOP-BL9MF61 MINGW64 ~/.ssh
$ ssh -T [email protected]
Hi weiyanzixun! You've successfully authenticated, but gitlab does not provide shell access.

注意,地址应该跟你配置的HOST一致,比如你的Host配置的是gitlab,你测试应该修改为ssh -T git@gitlab,clone代码,pull代码都是这个地址。

如果是你本地已经有的项目,需要修改origin url,操作如下:

git remote rm origin 
git remote add origin [email protected]:aitlp/Spark.git

即,将地址更改为config对应的Host。

系列文章:

  1. 同一台电脑为不同Github账号或Gitlab账号配置多个ssh keys
  2. 同一台电脑配置/关联/使用两个或多个Github账号
  3. 同一台电脑配置/关联/使用两个或多个Gitlab账号
  4. 同一台电脑同时使用github和gitlab账号配置ssh keys
赞(0)
未经允许禁止转载:Ddmit » 同一台电脑同时使用github和gitlab账号配置ssh keys

评论 抢沙发

登录

找回密码

注册