Git多密钥配置

一、密钥生成

前往ssh存储路径

mac:cd  ~/.ssh  
win:cd C:\Users\用户名\.ssh
ssh-keygen -t rsa -C xx@xxxxx.com
Generating public/private ed25519 key pair.
Enter file in which to save the key (/Users/mac/.ssh/id_ed25519):  xxxx
Enter passphrase (empty for no passphrase): 
Enter same passphrase again:    

说明:一般git服务器登录页面后账号设置中 SSH Keys 管理都会说明如何添加导入
  • -t 指密钥的类型,gitee举例ed25519,github举例ed25519,coding举例rsa
  • -C 指注释 一般填写邮箱地址
  • Enter File 提示你输入要生成的密钥文件名
  • Enter passphrase 提示你要输入的密钥密码 (不输入默认确认默认为空,如果设置请记录后续会用到)
  • Enter same 提示确认输入密码

二、多个密钥管理

1、举例:创建分别为gitee、coding、github、bitbucket生成了四组密钥对

mac@Mac-Pro .ssh % ls
bitbucket   coding      config      gitee.pub   github.pub
bitbucket.pub   coding.pub  gitee       github

2、使用ssh-add 命令添加私钥

ssh-add ~/.ssh/gitee 
Identity added: /Users/mac/.ssh/gitee (xx@xxxxx.com)

ssh-add ~/.ssh/coding
Identity added: /Users/mac/.ssh/coding (xx@xxxxx.com)

ssh-add ~/.ssh/github
Identity added: /Users/mac/.ssh/github (xx@xxxxx.com)

ssh-add ~/.ssh/bitbucket
Identity added: /Users/mac/.ssh/bitbucket (xx@xxxxx.com)

说明: 如果在Enter passphrase 过程中设置了密码,此操作需要输入当时密码
添加成功会有对应提示:Identity added

3、检查已添加的私钥

ssh-add -l

mac@Mac-Pro .ssh % ssh-add -l
256 SHA256:a7qJ45YL+bLodKdOrxx2a28P9V2igRlPU  xx@xxxxx.com (ED25519)
3072 SHA256:KHunn5oxHSxxxxU8YkikEsyxxX/VpOY xx@xxxxx.com (RSA)
3072 SHA256:vu+3aiCKGDcTgRNyCxxxxxEbwQ6r15tQWRGg xx@xxxxx.com (RSA)
3072 SHA256:sMzqOy2nf3SHxxxxxxxx1gqmuCx9qSMLz0mk xx@xxxxx.com (RSA)

4、添加config文件

创建config文件

touch config

文件内容:

# gitee

Host gitee
HostName gitee.com
PreferredAuthentications publickey 
IdentityFile ~/.ssh/gitee
User yaod  

# github

Host github
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github
User yaod 

# coding

Host coding
HostName kitsudo-01.coding.net
PreferredAuthentications publickey 
IdentityFile ~/.ssh/coding
User yaod

# BitBucket

Host bucket
HostName 192.168.1.22
Port 7999
PreferredAuthentications publickey 
IdentityFile ~/.ssh/bucket
User yaod 

说明

- Host example                                  # 关键词 区分用 
- HostName example.com                        # 主机地址,如果是ip填写ip地址
- User root                                   # 用户名
- IdentityFile ~/.ssh/xxx                     # 认证文件,私钥
- PreferredAuthentications publickey          # 配置值为publickey,优先使用publickey身份验证
- Port 22                                      # 指定端口,如果ip加端口号,需要填写端口号

4、在相应服务端配置公钥

对应的xx.pub中copy公钥数据,粘贴到服务端Add key的位置。

5、连通测试 举例

ssh -T git@gitee.com             
The authenticity of host 'gitee.com (212.64.63.215)' can't be established.
ED25519 key fingerprint is SHA256:+ULzij2u99B9eWYFTw1Q4ErYG/aepHLbu96PAUCoV88.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'gitee.com' (ED25519) to the list of known hosts.
Hi xxx! You've successfully authenticated, but GITEE.COM does not provide shell access.


ssh -T git@e.coding.net          
CODING 提示: Hello xxx, You've connected to coding.net via SSH. This is a Personal Key.

有些服务可能会提示host主机不受信任,可添加信任列表解决,例如上面的212.64.63.215 gitee.com

sudo vim /etc/hosts 

212.64.63.215 gitee.com

带端口号的连通测试

ssh -p 7999  -T git@192.168.1.22

出现成功字样表明连通成功,可使用ssh地址操作远程仓库

三、参考文章

Git高级之配置多个SSH key
Mac下ssh登录测试报错“Could not resolve hostname xxx: nodename nor servname provided, or not known”的解决办法

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容