1. 生成SSH-Key
$ ssh-keygen -t rsa -C "你的邮箱” -f ~/.ssh/id_rsa_one
# 生成第二个SSH-Key
$ ssh-keygen -t rsa -C ""你的邮箱”” -f ~/.ssh/id_rsa_two
- 创建完成之后 我们在.ssh的根目录下可以看到文件
- C:\Users\Administrator.ssh 或者 C:\Users\电脑用户名.ssh
2. 添加私钥
$ ssh-add ~/.ssh/id_rsa_gitlab
$ ssh-add ~/.ssh/id_rsa_github
$ ssh-agent bash
3. 查看私钥列表是否存在
$ ssh-add -l
4. 修改配置文件config
- 若.ssh目录(就是私钥所在的文件夹)下无config文件,那就需要自己创建config文件(可用vs code创建)
- 创建后添加一下内容:
Host gitlab.com
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_gitlab
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github
5. 在gitlab、github网站中设置SSH keys
- 在gitlab、github网站中找到用户设置,找到SSH keys
- 在.ssh的根目录中找到对应网站的.pub结尾的文件,用记事本打开,然后复制内容到对应网站保存即可
6. 用ssh命令测试验证
$ ssh -T git@gitlab.com
$ ssh -T git@github.com
7. 清空私钥列表
$ ssh-add -D