您现在的位置是:首页 > 博客日记 > Liunx Liunx

Git配置多个SSH-Key

2021-11-01 23:35:40 【Liunx】 人已围观

Git配置多个SSH-Key

1.背景

当有多个git账号时,比如:

  1. a. 一个gitee,用于公司内部的工作开发;
  2. b. 一个github,用于自己进行一些开发活动;

2.解决方法

生成一个gitee用的SSH-Key

  1. $ ssh-keygen -t ed25519 -C "666@majiameng.com" -f ~/.ssh/gitee_id_rsa

生成一个github用的SSH-Key

  1. $ ssh-keygen -t rsa -C '666@majiameng.com' -f ~/.ssh/id_rsa

3.在 ~/.ssh 目录下新建一个config文件,添加如下内容(其中Host和HostName填写git服务器的域名,IdentityFile指定私钥的路径)

  1. # gitee
  2. Host gitee.com
  3. HostName gitee.com
  4. PreferredAuthentications publickey
  5. IdentityFile ~/.ssh/gitee_id_rsa
  6. # github
  7. Host github.com
  8. HostName github.com
  9. PreferredAuthentications publickey
  10. IdentityFile ~/.ssh/id_rsa

4.用ssh命令分别测试

  1. $ ssh -T git@gitee.com
  2. $ ssh -T git@github.com

测试gitee,成功的话会返回下图内容

  1. $ ssh -T git@gitee.com
  2. Hi 我的数据裤呢?! You've successfully authenticated, but GITEE.COM does not provide shell access.

测试github,成功的话会返回下图内容

  1. $ ssh -T git@github.com
  2. The authenticity of host 'github.com (20.205.243.166)' can't be established.
  3. RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
  4. This key is not known by any other names
  5. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
  6. Warning: Permanently added 'github.com' (RSA) to the list of known hosts.
  7. Hi majiameng! You've successfully authenticated, but GitHub does not provide shell access.


关注TinyMeng博客,更多精彩分享,敬请期待!
 

很赞哦! ()