Git - 使用多个帐号

执行ssh-add时出现Could not open a connection to your authentication agent

若执行ssh-add /path/to/xxx.pem是出现这个错误:Could not open a connection to your authentication agent,则先执行如下命令即可:

  ssh-agent bash


更多关于ssh-agent的细节,可以用 man ssh-agent 来查看



背景:在工作中,都会有一个工作的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 "[email protected]"  # 新建工作的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 [email protected]  


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中;


出处:https://blog.csdn.net/a258831020/article/details/50373060

猜你喜欢

转载自blog.csdn.net/jackliu16/article/details/80460911
今日推荐