Mac 上配置多个ssh公私钥


在ubuntu上可能会遇到一些问题,解决办法如下命令(Permissiondenied (publickey)
eval ssh-agent -s


前言

当将博客同时部署到GitHub和coding(与腾讯云合作后升级了)时,需要在本地配置多个Git用的ssh-key。

腾讯云开发者平台

一个公钥只能认证一个用户,而一个用户却可以拥有多个公钥。

参考

Connecting to GitHub with SSH
配置 SSH 公钥访问代码仓库
hexo博客同时部署至github和Coding

命令一览

ssh-keygen --help
ssh-add --help

步骤

检查存在的公私钥: ls -al ~/.ssh

生成自定义的新的公私钥:

github要求:ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
coding要求(默认是2048位):ssh-keygen -t rsa -C "your_email@example.com"

注意写一下file名(必须是从根目录开始的完整的路径加文件名,否则只写文件名就是在当前路径下生成,什么都不写就是默认生成~/.ssh/id_rsa),免得默认直接覆盖掉了。
密码的话随便设置,但最好和网站上的一致。

➜  ~ ssh-keygen -t rsa -C "youremail@qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/you/.ssh/id_rsa): /Users/you/.ssh/id_rsa_coding
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/you/.ssh/id_rsa_coding.
Your public key has been saved in /Users/you/.ssh/id_rsa_coding.pub.
The key fingerprint is:
SHA256:YDSb5JxNCgVV6I8+dmmmUQscMZ+1++zErxbse872eS8 youremail@qq.com
The key's randomart image is:
+---[RSA 2048]----+
|  .o*o= o        |
|   ..X @ .       |
|   .. J .        |
|   ..o . .       |
|    oo. S.       |
|    .o.. +o      |
|   .. .. .=.     |
|    +.=  oooo E..|
|   ..*   .+*+.ooo|
+----[SHA256]-----+

查看一下已经添加到ssh-agent的 ssh-key:ssh-add -l

显示:The agent has no identities.
说明: ssh agent 并没有把我们新生成的 ssh-key添加进去

添加新生成的ssh-key到ssh-agent:ssh-add ~/.ssh/id_rsa_coding

ssh-add  # 默认添加:id_rsa
ssh-add -A  # 默认添加:id_rsa
ssh-add -K ~/.ssh/id_rsa_coding  # 指定添加
ssh-add -D  # 全部删除
ssh-add -d  ~/.ssh/id_rsa_coding  # 指定删除

去网站添加ssh公钥

腾讯云(coding)
github

测试

ssh -T git@git.coding.net 或者:ssh -T git@git.dev.tencent.com
ssh -T git@github.com

hexo 中的配置

deploy:
  - type: git
    repo: 
      github: git@github.com:dfface/dfface.github.io.git,master
      coding: git@git.dev.tencent.com:dfface/dfface.git,master
➜  blog ssh -T git@git.coding.net
The authenticity of host 'git.coding.net (118.25.166.124)' can't be established.
RSA key fingerprint is SHA256:jok3FH7q5LJdNedgXRw51ErE77S0Dn+Vg/Ik.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'git.coding.net,118.25.166.124' (RSA) to the list of known hosts.
Coding 提示: Hello dfface, You've connected to Coding.net via SSH. This is a personal key.
dfface,你好,你已经通过 SSH 协议认证 Coding.net 服务,这是一个个人公钥
➜  blog ssh -T git@github.com
The authenticity of host 'github.com (13.229.188.59)' can't be established.
RSA key fingerprint is SHA256:nThbd1IGOCspRomdRLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,13.229.188.59' (RSA) to the list of known hosts.
Hi dfface! You've successfully authenticated, but GitHub does not provide shell access.
➜  blog ssh -T git@git.dev.tencent.com
The authenticity of host 'git.dev.tencent.com (118.25.166.124)' can't be established.
RSA key fingerprint is SHA256:jok3FH7dddd7iPNehBgXRw51ErE77S0Dn+Vg/Ik.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'git.dev.tencent.com' (RSA) to the list of known hosts.
Coding 提示: Hello dfface, You've connected to Coding.net via SSH. This is a personal key.
dfface,你好,你已经通过 SSH 协议认证 Coding.net 服务,这是一个个人公钥

Cheat Sheet

ssh-keygen -t rsa -C "your_email@example.com"  # 生成rsa公私钥并存储在指定位置
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"  # 生成4096位的rsa公私钥并存储在指定位置,默认2048位
Enter file in which to save the key (/Users/you/.ssh/id_rsa): /Users/you/.ssh/id_rsa_coding  # 必须是从根目录开始的完整的路径加文件名
ssh-add  # 查看已添到ssh-agent的私钥
ssh-add  # 默认添加:id_rsa
ssh-add -A  # 默认添加:id_rsa
ssh-add -K ~/.ssh/id_rsa_coding  # 指定添加
ssh-add -D  # 全部删除
ssh-add -d  ~/.ssh/id_rsa_coding  # 指定删除
ssh -T git@git.coding.net # 测试
Ssh -T git@github.com  # 测试

推荐阅读更多精彩内容