美文网首页
多个Git配置多个ssh密钥

多个Git配置多个ssh密钥

作者: 码农工号9527 | 来源:发表于2023-04-17 11:37 被阅读0次

同一台电脑的同一个ssh共钥无法配置到多个github账号。例如公司的电脑ssh公钥配置到公司的github账号后,无法在使用同一个公钥配置个人的github账号。这时候需要生成多个ssh密钥,分别配置给不同的github账户

目前所有命令操作是在 win下的 git_bash 中操作的

生成新的ssh key

$ cd ~/.ssh/
lin@DESKTOP-8IV0LG9 MINGW64 ~/.ssh
$ ssh-keygen -t rsa -f "company"
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in company.
Your public key has been saved in company.pub.
The key fingerprint is:
SHA256:gxVP4oU3XzI1clhKC4L/154QRGSsf6+vqvSW2g7qYyc lin@DESKTOP-8IV0LG9
The key's randomart image is:
+---[RSA 2048]----+
|       .+.++*+=  |
|      ...Bo+*=.. |
|       .o..=o+   |
|       o. . o    |
|      . S. . o   |
|         .. + o  |
|          o. = o |
|        Eo.+o o .|
|       oo+o==.o+.|
+----[SHA256]-----+

执行命令 ssh-keygen -t rsa -C xxxx 后,会有一个输入密码(Enter passphrase (empty for no passphrase)) 与再次确认密码(Enter same passphrase again)的动作,这个时候如果你输入了密码,以后每次git操作都必须输入密码,推荐直接回车。踩坑人的文章地址:解决使用git,ssh每次都要输入密码

-f给新生成的密钥自定义命名,否则生成的是默认密钥:id_rsa与id_rsa.pub 它会覆盖掉现有的默认密钥,执行完成后,会在 ~/.ssh/目录下生成一个 companycompany.pub 文件

复制新的公钥到github账号

使用 cat company.pub,查看并复制显示的内容。

  1. 右上角下拉面板选择 Settings;


  2. 左侧选择 SSH and GPG keys,点击 New SSH key;


  3. 起一个 title,Key type 不用改,默认Authentication Key就行;


  4. 把公钥粘贴到 Key 选项下面。

配置~/.ssh/config 文件

vim ~/.ssh/config,写入如下内容

# key1 公司的github账号
 # github
 Host github.com
 HostName github.com
 PreferredAuthentications publickey
 IdentityFile ~/.ssh/id_rsa
 
 # key2 个人的guthub账号, lpb.github.com. me是自定义的
 Host lpb.github.com
 HostName github.com
 PreferredAuthentications publickey
 IdentityFile ~/.ssh/company

Bad owner or permissions on ~/.ssh/config,主要是没有对config的权限,运行命令: sudo chmod 600 config

测试ssh到github服务器的连接

lin@DESKTOP-8IV0LG9 MINGW64 ~/.ssh
$ ssh -T git@lpb.github.com
Hi linPB! You've successfully authenticated, but GitHub does not provide shell access.

lin@DESKTOP-8IV0LG9 MINGW64 ~/.ssh
$ ssh -T git@github.com
Permission denied (publickey).

如果出现Hi USERNAME! You’ve successfully authenticated, but github does not provide shell access.提示,说明我们的配置成功,到此就可以结束了。否则出现Permission denied (publickey)提示,说明系统ssh代理出现问题。

出现以上原因主要是因为:

github使用SSH与客户端连接。如果是单用户(first),生成密钥对后,将公钥保存至 GitHub ,每次连接时SSH客户端发送本地私钥(默认~/.ssh/id_rsa)到服务端验证。单用户情况下,连接的服务器上保存的公钥和发送的私钥自然是配对的。但是如果是 多用户 (first,second),我们在连接到second的帐号时,second保存的是自己的公钥,但是SSH客户端依然发送默认私钥,即first的私钥,那么这个验证自然无法通过。

配置ssh key代理

  1. 查看系统ssh-key代理,执行如下命令:
ssh-add -l

如果提示Could not open a connection to your authentication agent.,那么先执行下ssh-agent bash
以上命令如果输出The agent has no identities.则表示没有代理。如果系统有代理,可以执行下面的命令清除代理:ssh-add -D

  1. 然后依次将不同的ssh添加代理,执行命令如下:
ssh-add ~/.ssh/id_rsa
ssh-add ~/.ssh/company
  1. 再次进行ssh测试即可.

注意事项:ssh测试之后,在连接非默认账号的github仓库时,远程的地址要对应的作出修改.
配置了lpb.github.com用作个人的git我们就不能在用git@github了,要是用git@lpb.github.com

如果是配置git ssh密钥前已经拉过的代码,需要修改远程URL
远程URL如果是 https 的,直接改成 用 ssh 的,注意上面的提示,不用 git@github 这个,用自定义的 git@lpb.github.com 这个,如果是 ssh 的,但是是 git@github 的,也要重新改下,改成 自定义的。
查看 remote :git remote -v
修改 remote :git remote set-url origin git@lpb.github.com:[用户名]/[仓库名].git

参考文章

相关文章

网友评论

      本文标题:多个Git配置多个ssh密钥

      本文链接:https://www.haomeiwen.com/subject/qzfcjdtx.html