当前位置:   article > 正文

linux上git使用多个账号_linux git 多账户

linux git 多账户

背景:在工作中,都会有一个工作的Git帐号(公司Gitlab),而空闲时间做的个人东西又想放进Github里面,这时候就需要配置两个帐号和服务器。假设之前已经配置好了工作的帐号,打开Git bash:

1、创建个人的SSH key:

 

[html] view plain copy

  1. #新建SSH key:  
  2. $ cd ~/.ssh     # 切换到C:\Users\Administrator\.ssh  
  3. ssh-keygen -t rsa -C "youremail@example.com"  # 新建工作的SSH key  
  4. # 设置名称为id_rsa_hason(名字随意)  
  5. Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa): id_rsa_hason  

 

2、添加新密钥到SSH agent:

 

因为默认只读取id_rsa,为了让SSH识别新的私钥,需将其添加到SSH agent中:

 

[html] view plain copy

  1. ssh-add ~/.ssh/id_rsa_hason  


注意:如果出现Could not open a connection to your authentication agent,参考底部详情

3、修改config文件

若~/.ssh/目录下不存在config文件,则新建一个,内容写上:

 

[html] view plain copy

  1. # 该配置用于工作  
  2. # Host 服务器别名  
  3. Host 192.168.2.36  
  4. # HostName 服务器ip地址或机器名  
  5. HostName 192.168.2.36  
  6. # User连接服务器的用户名  
  7. User huanghs  
  8. # IdentityFile 密匙文件的具体路径  
  9. IdentityFile C:/Users/P/.ssh/id_rsa  
  10.   
  11.   
  12. # 该配置用于个人 github 上  
  13. # Host 服务器别名  
  14. Host github.com  
  15. # HostName 服务器ip地址或机器名  
  16. HostName github.com  
  17. # User连接服务器的用户名  
  18. User hasonHuang  
  19. # IdentityFile 密匙文件的具体路径  
  20. IdentityFile C:/Users/P/.ssh/id_rsa_hason  

 

4、添加新密钥到Github

 

把~/.ssh/id_rsa_hason.pub的内容添加到Github的SSH keys中

 

5、测试

使用ssh -T git@Host进行测试,其中Host指上面配置的服务器别名

 

[html] view plain copy

  1. ssh -T git@github.com  

 

 

 

 

 

6、大功告成!

 

 

常见问题:

1、出现Could not open a connection to your authentication agent

3种解决方法:

(a) 先输入ssh-agent bash,然后再输入ssh-add ~/.ssh/id_rsa_hason;

(b)先输入eval $(ssh-agent),然后输入ssh-add ~/.ssh/id_rsa_hason;

(c)使用Git GUI生成密钥,密钥会自动被加进ssh-agent中;

阿里云学生机1年114元限时活动(24岁以下都可以购买)https://promotion.aliyun.com/ntms/act/campus2018.html?userCode=a6violqw阿里云1888元红包:https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=a6violqw

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/671395
推荐阅读
相关标签
  

闽ICP备14008679号