🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# SSH公钥 >[success]使用SSH公钥可以让你在你的电脑和码云通讯的时候使用安全连接(Git的Remote要使用SSH地址) > >[danger]通常情况下,一台电脑上配生成一个SSH公钥就可以了,因为一般情况下,一台电脑上会登录一个git账号。但是也会遇到,同一台电脑上需要连接多个git的情况,这时一个SSH公钥就不够用了。 > ## 1. 一台电脑使用一个git账户 >[success]这时,需要生成一个SSH公钥就可以了。 生成并部署SSH key ### 如何生成ssh公钥 你可以按如下命令来生成 sshkey: ~~~ ssh-keygen -t rsa -C "xxxxx@xxxxx.com" # Generating public/private rsa key pair... # 三次回车即可生成 ssh key ~~~ 查看你的 public key,并把他添加到码云(Gitee.com) SSH key添加地址:https://gitee.com/profile/sshkeys) ~~~ cat ~/.ssh/id_rsa.pub # ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC6eNtGpNGwstc.... ~~~ 添加后,在终端(Terminal)中输入 ~~~ ssh -T git@gitee.com ~~~ 若返回 ~~~ Welcome to Gitee.com, yourname! ~~~ 则证明添加成功。 ## 2. 一台电脑使用多个git账户 >[success]这时如果使用同一SSH公钥添加的话,就会出现如下提示: >![](https://box.kancloud.cn/a0489b203b3b6fda3417d678e7d71ce7_579x108.png) >只为一个SSH公钥只能被一个git账号使用。所以在这里就需要使用多个公钥。 > 生成一个新的自定义名称的公钥: ~~~ //生成方式基本一样 ssh-keygen -t rsa -C "YOUR_EMAIL@YOUREMAIL.COM" -f ~/.ssh/my //执行完成后,会在 ~/.ssh/目录下生成一个 my 和 my.pub 文件 ~~~ 在 SSH 用户配置文件 ~/.ssh/config 中指定对应服务所使用的公秘钥名称,如果没有 config 文件的话就新建一个,并输入以下内容: ~~~ Host github.com www.github.com IdentityFile ~/.ssh/my //添加my.pub 到你的git服务器网站上。 ~~~ 测试配置文件是否正常工作 ~~~ ssh -T git@gitcafe.com ~~~ 如果,正常的话,会出现如下提示: ~~~ Welcome to Gitee.com, yourname! ~~~ 如果出现如下提示,则说明有权限问题: ~~~ Permission denied (publickey) ~~~ 如果有权限问题的情况下,你对项目执行push操作的时候,会得到如下提示: ~~~ Warning: Permanently added the RSA host key for IP address '192.30.252.129' to the list of known hosts. Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. ~~~ ### 多用户时出现权限问题的原因: >[success]github使用SSH与客户端连接。如果是单用户(first),生成密钥对后,将公钥保存至 GitHub ,每次连接时SSH客户端发送本地私钥(默认~/.ssh/id_rsa)到服务端验证。单用户情况下,连接的服务器上保存的公钥和发送的私钥自然是配对的。但是如果是 多用户 (first,second),我们在连接到second的帐号时,second保存的是自己的公钥,但是SSH客户端依然发送默认私钥,即first的私钥,那么这个验证自然无法通过。 > ### 解决ssh权限问题(): >[success]通常一台电脑生成一个ssh不会有这个问题,当一台电脑生成多个ssh的时候,就可能遇到这个问题,解决步骤如下: > 查看系统ssh-key代理,执行如下命令 ~~~ $ ssh-add -l ~~~  以上命令如果输出 The agent has no identities. 则表示没有代理。如果系统有代理,可以执行下面的命令清除代理: ~~~ $ ssh-add -D ~~~ 然后依次将不同的ssh添加代理,执行命令如下: ~~~ $ ssh-add ~/.ssh/id_rsa $ ssh-add ~/.ssh/my ~~~ 你会分别得到如下提示: ~~~ 2048 8e:71:ad:88:78:80:b2:d9:e1:2d:1d:e4:be:6b:db:8e /Users/aysee/.ssh/id_rsa (RSA) //和 2048 8e:71:ad:88:78:80:b2:d9:e1:2d:1d:e4:be:6b:db:8e /Users/aysee/.ssh/id_rsa (RSA) 2048 a7:f4:0d:f1:b1:76:0b:bf:ed:9f:53:8c:3f:4c:f4:d6 /Users/aysee/.ssh/aysee (RSA) ~~~ 如果使用 ssh-add ~/.ssh/id_rsa的时候报如下错误,则需要先运行一下 ssh-agent bash 命令后再执行 ssh-add ...等命令 >[success]Could not open a connection to your authentication agent. > 配置 ~/.ssh/config 文件 如果没有就在~/.ssh目录创建config文件,该文件用于配置私钥对应的服务器 ~~~ # Default github user(first@mail.com) Host github.com HostName github.com User git IdentityFile C:/Users/username/.ssh/id_rsa # my (company_email@mail.com) Host github-my HostName github.com User git IdentityFile C:/Users/username/.ssh/my ~~~ Host随意即可,方便自己记忆,后续在添加remote是还需要用到。 配置完成后,在连接非默认帐号的github仓库时,远程库的地址要对应地做一些修改,比如现在添加second帐号下的一个仓库test,则需要这样添加: ~~~ git remote add test git@github-aysee:ay-seeing/test.git #并非原来的git@github.com:ay-seeing/test.git ~~~ ay-seeing 是github的用户名 测试 ssh ~~~ ssh -T git@github.com ~~~ 你会得到如下提示,表示这个ssh公钥已经获得了权限 ~~~ Hi USERNAME! You've successfully authenticated, but github does not provide shell access. ~~~