多用户配置ssh登录

因为在实际的工作学习场景中,公司的远程代码仓库大多是自己搭建的,例如我司就是gitLab,需要配置一个ssh,个人学习代码仓库在gitHub,也要配置一个SSH,所以必须要配置俩了。

管理多组密钥对

有时候你会针对多个服务器有不同的密钥对,每次通过指定 -i 参数也是非常的不方便。比如你使用 github 和 coding。那么你需要添加如下配置到 ~/.ssh/config:

多用户配置ssh登录

.ssh/config
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Host codeup.aliyun.com
HostName codeup.aliyun.com
User git
IdentityFile ~/.ssh/xxx@example.cn.ed25519
IdentitiesOnly yes
# 加载到 ssh-agent
AddKeysToAgent yes
# 添加到用户钥匙串
UseKeychain yes
Host github.com-gomro
HostName github.com
User git
IdentityFile ~/.ssh/devops@example.cn.ed25519
IdentitiesOnly yes
# 加载到 ssh-agent
AddKeysToAgent yes
# 添加到用户钥匙串
UseKeychain yes
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
IdentitiesOnly yes
# 加载到 ssh-agent
AddKeysToAgent yes
# 添加到用户钥匙串
UseKeychain yes

创建独立的ssh密钥和公钥

1
2
3
email=$(git config user.email)
echo $email
ssh-keygen -t ed25519 -f ~/.ssh/$email.ed25519 -C $email

验证密码

1
2
3
email=$(git config user.email)
echo $email
ssh-keygen -y -f ~/.ssh/$email.ed25519 -C $email