如果你已经有了一套名为 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. Linux下修改Tomcat默认端口

    假设tomcat所在目录为/usr/local/apache-tomcat/ 1.打开tomcat配置文件#vi /usr/local/apache-tomcat/conf/server.xml 2. ...

  2. mysql创建表和数据库

    创建数据库,创建数据库表,例子.MySQL语句   1.创建数据库:    创建的代码:create  数据库的代码:database   数据库表名:随便起,只要自己记住就行.test create ...

  3. Vue.js组件之间的调用

    index.html: <div id="app"></div> 运行完index.html之后自动寻找运行main.js文件 main.js: impor ...

  4. JVM系列1:Java内存区域

    JVM系列主要分享自己都虚拟机的理解,我自学时的知识框架多来源于<深入理解Java虚拟机_JVM高级特性与最佳实践>这本书,感兴趣的朋友可直接去阅读这本书. 本系列暂定有3部分,它们是学习 ...

  5. 了解各种不同意义上的new

    问题1:请说明new operator 和 operator  new的差异? 1.new   operator : 一般我们写代码的时候,例如:String *p = new String(&quo ...

  6. [转]MAC系统下Sublime Text3 配置Python3详细教程(亲测有效)

    原文地址: https://blog.csdn.net/weixin_41768008/article/details/79859008?tdsourcetag=s_pctim_aiomsg 这段时间 ...

  7. Java学习笔记(十二):java编译跨平台运行原理

    class文件由java源代码通过javac编译器编译生成,只能为JVM所识别.

  8. linux上部署Appach,让文件目录以网页列表形式访问

    效果: 1.首先,需要安装Apache httpd服务 yum install -y httpd 2.查看或者设置httpd主配文件 vim /etc/httpd/conf/htpd.conf 从中可 ...

  9. HDU 5723 Abandoned country(kruskal+dp树上任意两点距离和)

    Problem DescriptionAn abandoned country has n(n≤100000) villages which are numbered from 1 to n. Sin ...

  10. Linux驱动之异常处理体系结构简析

    异常的概念在单片机中也接触过,它的意思是让CPU可以暂停当前的事情,跳到异常处理程序去执行.以前写单片机裸机程序属于前后台程序,前台指的就是mian函数里的while(1)大循环,后台指的就是产生异常 ...