生成 SSH 密匙
第一步:检查是否已有 SSH 密匙
一般 SSH 密匙会在 c:/Users/you/.ssh
目录下,查看此目录下是否有类似以下文件:
- id_dsa.pub
- id_ecdsa.pub
- id_ed25519.pub
- id_rsa.pub
如果有的话,跳过第二步。
第二步:新生成 SSH 密匙
要生成新的密匙,请在命令终端中执行以下操作。如果不需要更改默认的密匙存放地址,根据提示按回车键:
ssh-keygen -t rsa -C "your_email@example.com"
# 输入你自己的邮箱,最好与你的 Gitlab 帐号注册邮箱保持一致
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/you/.ssh/id_rsa): [Press enter]
接下来,你会被要求键入密码(密码可以不填写,直接回车):
Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]
当输完密码后,你会看到如下提示:
Your identification has been saved in /c/Users/you/.ssh/id_rsa.
Your public key has been saved in /c/Users/you/.ssh/id_rsa.pub.
The key fingerprint is:
01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@example.com
然后将生成的新密匙加入到 ssh-agent(如未安装 ssh-agent 请跳过此步):
ssh-agent -s
# Agent pid 59566
ssh-add /c/Users/you/.ssh/id_rsa.pub
第三步:将你的密匙加入到你的 Gitlab 帐号中
首先,拷贝你的公共密匙到剪贴板中:
clip < /c/Users/you/.ssh/id_rsa.pub
或者你也可以直接用编辑器打开它来拷贝其内容。
接下来登录 git.cairenhui.com,在顶部导航中选择 "Profile settings",进入用户设置频道。
在二级导航处,选择 "SSH Keys" 选项,点击右上角的 "Add SSH Key" 按钮。
先将你的拷贝内容粘贴到 "Key" 一栏,再填写 "Title" 一栏。
最后别忘记点击 "Add key" 按钮确认。
第四步:设置SSH config
在 C:\用户\你的用户目录\.ssh
目录下新建 config 文件, 文件里录写入下述内容
Host git.cairenhui.com
HostName git.cairenhui.com
Port 8244
User git
IdentityFile C:\用户\你的用户目录\.ssh\id_rsa
第五步:测试生效
在命令终端中输入:
ssh -T git@git.cairenhui.com
不出意外的话,你会看见以下提示:
The authenticity of host 'git.cairenhui.com (172.16.1.50)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)?
别担心,第一次都会有此提示,应该是类似指纹识别之类的东东,输入 yes 继续。
当看到终端提示:
Welcome to GitLab, yourname!
恭喜你,大功告成!