最近使用码云gitee和github,二者默认公钥存储位置相同,这样会相互覆盖,不能同时使用。
为了要同时使用,就需要分开两个文件存储。计划将github公钥存储在.ssh/rsa_id中,将gitee公钥存储在.ssh/gitee.rsa_id中
先用 touch 命令新建文件:
touch .ssh/gitee.rsa_id
之后用git命令生成公钥:
git-keygen -t rsa -C "youremail@email.com"
会出现类似这样提示:
Generating public/private rsa key pair.
Enter file in which to save the key (/home/wyb/.ssh/id_rsa): /home/wyb/.ssh/gitee.id_rsa.pub
/home/wyb/.ssh/gitee.id_rsa.pub already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/wyb/.ssh/gitee.id_rsa.
Your public key has been saved in /home/wyb/.ssh/gitee.id_rsa.pub.
The key fingerprint is:
SHA256:fjlajlmfldajljflajldkfjlakjljfljaxM youremail@email.com
The key's randomart image is:
+---[RSA 2048]----+
|Bo . oo |
|*+o +2rr+o |
|*. +wfewE.. |
|+ofwfe.* . |
|o+ we S |
|o . .. X + |
|. . O . . |
| . fffweo f |
| .+o+.o |
+----[SHA256]-----+
在上面问你选择存在哪个文件(Enter file in which to save the key (/home/wyb/.ssh/id_rsa)里,输入你刚新建的文件;
之后一路回车生成完毕。
因为git的架构是这样的:
所以本地公钥在生成之后,要放在你在github/gitee上,好和你对上暗号。如何在github上添加ssh公钥,看这个教程:《Generating a new SSH key and adding it to the ssh-agent》
在执行上面这个教程,将本地公钥添加到ssh-agent时候,出现了这个warning:
wyb@wyb-ThinkPad-T420:~/.ssh$ ssh-add /home/wyb/.ssh/gitee.id_rsa.pub
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0664 for '/home/wyb/.ssh/gitee.id_rsa.pub' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
提示说:
“你这个私钥没受保护!”拒绝了这个私钥文件,因为它太开放了。根据要求,私钥是不能被其他人访问的。所以这个文件暂时被忽略。
查看.ssh/gitee.id_rsa.pub文件权限,确实还有其他人可读:
ls -al
总用量 28
drwx------ 2 wyb wyb 4096 8月 28 12:36 .
drwxr-xr-x 43 wyb wyb 4096 8月 28 09:27 ..
-rw-rw-r-- 1 wyb wyb 1679 8月 28 12:36 gitee.id_rsa
-rw-r--r-- 1 wyb wyb 402 8月 28 12:36 gitee.id_rsa.pub
-rw------- 1 wyb wyb 1675 8月 28 11:40 id_rsa
-rw-r--r-- 1 wyb wyb 398 8月 28 11:40 id_rsa.pub
-rw-r--r-- 1 wyb wyb 3318 8月 28 11:48 known_hosts
查看这个教程《SSH Private Key Permissions using Git GUI or ssh-keygen are too open》,应该把密钥文件改为只自己可读可写。用这个命令:
chmod 700 gitee.id_rsa
之后再往ssh-agent添加就可以了。
ssh-add /home/wyb/.ssh/gitee.id_rsa.pub
Identity added: /home/wyb/.ssh/gitee.id_rsa.pub (/home/wyb/.ssh/gitee.id_rsa.pub)
之后确认下是否添加成功:
$ ssh -T git@gitee.com
Hi 慕广陵! You've successfully authenticated, but GITEE.COM does not provide shell access.
git本质原理可以看这里,说的清楚详细:《git起步基础》
接着参考即可:Gitee(码云)、Github同时配置ssh key