若是你已经有了一套名为 id_rsa
的公秘钥,将要生成另一个公钥,好比 aysee ,你也可使用任何你喜欢的名字。javascript
步骤以下:java
一、生成一个新的自定义名称的公钥:git
ssh-keygen -t rsa -C "YOUR_EMAIL@YOUREMAIL.COM" -f ~/.ssh/aysee
执行命令后,生成命名的公钥和生成默认公钥的步骤同样。github
执行完成后,会在 ~/.ssh/目录下生成一个 aysee 和 aysee.pub 文件。shell
二、在 SSH 用户配置文件 ~/.ssh/config 中指定对应服务所使用的公秘钥名称,若是没有 config 文件的话就新建一个,并输入如下内容:bash
Host github.com www.github.com IdentityFile ~/.ssh/aysee
三、添加 aysee.pub 到你的git服务器网站上。服务器
四、测试配置文件是否正常工做ssh
ssh -T git@gitcafe.com
若是,正常的话,会出现以下提示:ide
Hi USERNAME! You've successfully authenticated, but github does not provide shell access.
若是出现以下提示,则说明有权限问题:测试
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.
github使用SSH与客户端链接。若是是单用户(first),生成密钥对后,将公钥保存至 GitHub ,每次链接时SSH客户端发送本地私钥(默认~/.ssh/id_rsa)到服务端验证。单用户状况下,链接的服务器上保存的公钥和发送的私钥天然是配对的。可是若是是 多用户 (first,second),咱们在链接到second的账号时,second保存的是本身的公钥,可是SSH客户端依然发送默认私钥,即first的私钥,那么这个验证天然没法经过。
一般一台电脑生成一个ssh不会有这个问题,当一台电脑生成多个ssh的时候,就可能遇到这个问题,解决步骤以下:
一、查看系统ssh-key代理,执行以下命令
$ ssh-add -l
以上命令若是输出 The agent has no identities. 则表示没有代理。若是系统有代理,能够执行下面的命令清除代理:
$ ssh-add -D
二、而后依次将不一样的ssh添加代理,执行命令以下:
$ ssh-add ~/.ssh/id_rsa $ ssh-add ~/.ssh/aysee
你会分别获得以下提示:
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 ...等命令
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
# aysee (company_email@mail.com) Host github-aysee HostName github.com User git IdentityFile C:/Users/username/.ssh/aysee
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.