如果你已经有了一套名为 id_rsa 的公秘钥,将要生成另外一个公钥,比如 aysee ,你也可以使用任何你喜欢的名字。

步骤如下:

1、生成一个新的自定义名称的公钥:

ssh-keygen -t rsa -C "YOUR_EMAIL@YOUREMAIL.COM" -f ~/.ssh/aysee

  执行命令后,生成命名的公钥和生成默认公钥的步骤一样。

执行完成后,会在 ~/.ssh/目录下生成一个 aysee 和 aysee.pub 文件。

2、在 SSH 用户配置文件 ~/.ssh/config 中指定对应服务所使用的公秘钥名称,如果没有 config 文件的话就新建一个,并输入以下内容:

Host github.com www.github.com
IdentityFile ~/.ssh/aysee

3、添加 aysee.pub 到你的git服务器网站上。

4、测试配置文件是否正常工作

ssh -T git@gitcafe.com

  如果,正常的话,会出现如下提示:

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的时候,就可能遇到这个问题,解决步骤如下:

1、查看系统ssh-key代理,执行如下命令

$ ssh-add -l

  以上命令如果输出  The agent has no identities. 则表示没有代理。如果系统有代理,可以执行下面的命令清除代理:

$ ssh-add -D

2、然后依次将不同的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.

  

3、配置 ~/.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的用户名

4、测试 ssh

ssh -T git@github.com

  你会得到如下提示,表示这个ssh公钥已经获得了权限

Hi USERNAME! You've successfully authenticated, but github does not provide shell access.

  

生成多个git ssh密钥的更多相关文章

  1. ssh-keygen生成git ssh密钥

    title: ssh-keygen生成git ssh密钥 date: 2018-05-07 08:49:21 tags: [git,ssh-keygen] --- ssh-keygen生成git ss ...

  2. 如何设置Git SSH密钥

    1. SSH 存储在user/用户名/.ssh文件夹下 生成SSH密钥 $ ssh-keygen -t rsa -C "your_email" 2. 查看生成的公钥 $ cat ~ ...

  3. git ssh密钥配置添加

    1.  初次安装git配置用户名和邮箱 $ git config --global user.name "xxx" $ git config --global user.email ...

  4. git ssh密钥的使用

    //配置邮箱,用户名, git config --global user.name git config --global user.email git config --global --list ...

  5. git/ssh备查文档

    配置多个ssh key: 待更新 git速查表: git remote set-url origin(远程仓库名称) https://xxxxx/ProjectName.git  从ssh切换至htt ...

  6. Git SSH密钥对生成以及多个SSH存在情况配置

    一.使用Git Bash 生成一个新的SSH密钥 1. 打开 Git Bash. 2. 邮箱设置粘贴下面的文字,替换成为你自己的邮箱. Github SSH 1 $ ssh-keygen -t rsa ...

  7. jenkins SSH登录 Git配置(通过eclipse生成SSH 密钥)

    1.通过eclipse生成SSH 密钥 菜单栏的windows-->preferences-->General-->Network Connections-->SSH2--&g ...

  8. windows下生成上传git时需要用的SSH密钥

    参考:Windows上传代码到Github 打开“Git Bash” 输入 ssh-keygen -C "your email" -t rsa 出现如下结果: 成功后,信息里会显示 ...

  9. 关于git SSH Key的 生成

    最近刚接触git,简直就是一小白用户,所以决定自己记录一些东西,以备不时之需 系统环境:Windows 1.首先下载git,http://git-scm.com/download/ 2.正常安装git ...

随机推荐

  1. Fiddler 抓包工具怎么使用?怎么在Android手机端的APP抓包

    https://blog.csdn.net/loner_fang/article/details/83309266 参考这个人的微博上有fiddler主要功能使用的步骤. 序章 Fiddler是一个蛮 ...

  2. 最适合入门的Laravel中级教程(三)表单验证

    做开发有个原则是永远不能信任用户输入的数据: 即便前端已经做了验证: 在后端 php 也必须要再次验证: laravel 为表单验证提供了强大且简单的方案: 创建示例路由: routes/web.ph ...

  3. [INet] WebSocket 数据收发的详细过程

    WebSocket 和 HTTP 相似,只是一个应用层协议,对下层透明,所以不涉及 TCP/IP. 由于浏览器支持了 WebSocket,所以在用 JS 写客户端的时候,是无需考虑数据的编码解码的. ...

  4. ceph结构详解

    引言 那么问题来了,把一份数据存到一群Server中分几步? Ceph的答案是:两步. 计算PG 计算OSD 计算PG 首先,要明确Ceph的一个规定:在Ceph中,一切皆对象. 不论是视频,文本,照 ...

  5. php的pid文件指定用户

    比如pid文件指定www用户,首先得有这用户和用户组. 找到pathtophp-fpm.conf文件,修改里面得相关内容. 修改listen.owner=www listen.group=www us ...

  6. Nginx – rewrite 配置 URL重写及301跳转原理图

    Nginx – rewrite 配置 URL重写 官网:http://nginx.org/en/docs/http/ngx_http_rewrite_module.html 语法:rewrite re ...

  7. 【python深入】装饰器理解及使用

    装饰器,其实就是一个函数,它可以让其他函数在不需要做任何代码变动的前提下额外增加功能,装饰器的返回是一个函数对象. 用一个例子来说明一下什么是函数对象: 从这里可以看到,直接在函数后面加bar(),就 ...

  8. [leetcode]200. Number of Islands岛屿个数

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  9. java_22 Map接口

    1Map Collection是孤立存在的,向集合中存储元素是一个一个放进去的 Map中的集合存储是成对的,可以通过键找到值.即将键映射到值的对象.一个映射不能包含重复的键:每个键最多只能映射到一个值 ...

  10. 前后端跨域 _ cross domain

    1. 解决跨域既可以从前端, 也可以从后端. 参考好的网络资源: http://www.cnblogs.com/vajoy/p/4295825.html