MENU

git同时设置多个ssh-key

July 5, 2022 • linux相关

1. 生成 ssh-key

gitee 平台一套:

$ ssh-keygen -t rsa -b 2048 -C '[email protected]' -f ~/.ssh/gitee_id_rsa

github 平台一套:

$ ssh-keygen -t rsa -b 2048 -C '[email protected]' -f ~/.ssh/github_id_rsa

如果是 ED25519,则如:

ssh-keygen -t ed25519 -C "<comment>" -f ~/.ssh/xxx_id_ed25519

2. 设置 git 配置 ~/.ssh/config

# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_id_rsa

# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa

添加 id_rsa

$ eval $(ssh-agent -s)
$ ssh-add /root/.ssh/id_rsa

3. 用 ssh 命令分别测试

$ ssh -T [email protected]
$ ssh -T [email protected]

4. 出现权限问题

$ ssh-add id_rsa
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for 'id_rsa' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.

修改下权限即可:

$ chmod 0600 id_rsa

或者:

$ chmod 0400 id_rsa
Last Modified: August 19, 2022