一、什么是ssh key?
ssh key是git远程服务器与本机通信所必须的密钥,每个git服务器对应本机上的一对密钥,包括私钥和公钥。存储位置为:~/.ssh
二、为什么要生成ssh key?
如果本机想要从远程服务器上拉取代码,必须将本机的公钥上传到远程服务器上。
三、如何生成ssh key?
可以用 ssh-keygen
来创建。该程序在 Linux/Mac 系统上由 SSH 包提供,而在 Windows 上则包含在 MSysGit 包里。
ssh-keygen -t rsa -C "xxx@xxx.com" (你的邮箱,也是你在git服务器上的账号)
结果:
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/wangfengzi/.ssh/id_rsa):
Created directory '/Users/wangfengzi/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/wangfengzi/.ssh/id_rsa.
Your public key has been saved in /Users/wangfengzi/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:dZi6aPSBNJ3aIzzf7k4qQE6XHIyPLLVqTTKNeIzzlyc wangfz@fenbi.com
The key's randomart image is:
+---[RSA 2048]----+
| o |
| o o. . o |
| + = =ooo + . |
|+ B Oo== o . |
| + @ oB S |
| + E..* = |
| . . +o + o |
| .. + |
| ..o+ |
+----[SHA256]-----+
经过上述步骤后,会在~/.ssh中生成id_rsa和id_rsa.pub的一对密钥文件。
四、与ssh key相关的配置
位置:~/.ssh/config
默认为:
git clone github.com:wfz-bupt/js.git
修改为:
git clone qq:wfz-bupt/js.git
前提:
在~/.ssh/config文件里,配置了qq所用的key,示例如下:
Host qq
HostName github.com
IdentityFile ~/.ssh/id_rsa.qq
User git