Mac 上配置 SSH - 多个 SSH

进入 ssh 目录,查看目录

1
2
cd ~/.ssh
ls

ls 命令效果

1
2
config            id_rsa.pub        id_rsa_github.pub known_hosts.old
id_rsa id_rsa_github known_hosts

生成对称加密秘钥,默认名称 id_rsa

1
ssh-keygen -t rsa -C "youremail@yourcompany.com"

指定秘钥名称 id_rsa_github,名字任意定

1
ssh-keygen -t rsa -C "youremail@yourcompany.com" -f ~/.ssh/id_rsa_github

配置 config 文件

1
vim config
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# gitlab rsa pub
# Host相当于别名,随便取,使用的时候注意名称一致,git clone git@NBGit:anzhiyu-c/anzhiyu-c.git
Host NBGit
# HostName:域名
HostName github.com
# 账号,不配置User也可生效
User anzhiyu-c@qq.com
# 权限
PreferredAuthentications publickey
# 对应域名下的秘钥
IdentityFile ~/.ssh/id_rsa_github

# gitlab rsa pub
Host gitlab
HostName gitlab.com
User anzhiyu_c@163.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa

测试

1
2
ssh -T git@NBGit
Hi anzhiyu-c! You've successfully authenticated, but GitHub does not provide shell access.
1
2
ssh -T git@gitlab
Welcome to GitLab, @anzhiyu_c!