配置多个SSH公钥,免密码连接Git服务器

1.首先Mac上预先装有Git和openSSH。

2.通过终端生成公钥

ssh-keygen -t rsa

3.生成GitHub对应的id_rsa_github.pub公钥和id_rsa_github私钥

Enter file in which to save the key (/root/.ssh/id_rsa):/root/.ssh/id_rsa_github  #设置路径,如果不设置默认生成 id_rsa  和  id_rsa.pub

密码可以不设直接回车跳过

按照上面步骤生成Coding对应的id_rsa_coding.pub公钥和id_rsa_coding私钥

4.查看系统ssh-key代理

$ ssh-add -l

如果显示The agent has no identities,表示没有ssh-key 代理
如果有,全部删除

$ ssh-add -D

5.把 .ssh 目录下的私钥添加到ssh-key 代理

$ ssh-add ~/.ssh/id_rsa_github
$ ssh-add ~/.ssh/id_rsa_coding

6.打开ssh 管理页面把对应的公钥保存到代码管理服务器

cat ~/.ssh/id_rsa_github.pub | pbcopy

GitHub增加公钥:https://github.com/settings/keys

cat ~/.ssh/id_rsa_coding.pub | pbcopy

Coding增加公钥:https://coding.net/user/account/setting/keys

7.在 .ssh 目录创建 config 配置文件

vi ~/.ssh/config

输入如下配置信息
#别名需要与Host一致

#github
Host github
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_github

#coding
Host coding
    HostName git.coding.net
    User git
    IdentityFile ~/.ssh/id_rsa_coding

8.测试

ssh -T github
Hi github! You've successfully authenticated, but GitHub does not provide shel l access.
ssh -T coding
Hello coding! You've connected to Coding.net via SSH successfully!

说明OK。

9.通过设置Host别名克隆项目

对于github帐号下的仓库:

git clone github:githubname/repository.git

(原地址是:git@github.com:githubname/repository.git,替换后应该是:github:githubname/repository.git)
对于coding帐号下的仓库:

git clone coding:codingname/repository.git

(原地址是:git@coding.net:codingname/repository.git,替换后应该是:coding:codingname/repository.git)

10.如果已经使用原地址克隆过了,可以使用命令修改:

git remote set-url origin github:githubname/repository.git
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,958评论 19 139
  • 1. 安装 Github 查看是否安装git: $ git config --global user.name "...
    Albert_Sun阅读 13,721评论 9 163
  • 安得万里风,飘飖吹我裳。唐 杜甫 《夏夜叹》 摘要 目前使用 git 作为文件版本控制工具的开发者越来越多,同时一...
    FosterDylan阅读 4,048评论 1 4
  • 楊孜 有月亮的夜晚 我会躲到树叶的背面 当妳走近的时候 我会变成叶子的颜色 不是有意躲避 而是妳的美丽 像一把快刀...
    楊孜阅读 330评论 1 2
  • 一个人整天躺在床上,想着我们以前在一起的场景,突然我明白或许你是爱我的,只不过家庭的反对,世俗的言论,让你最终放弃...
    不哭的孩子阅读 283评论 0 1