假设条件 💡
- 远程仓库所在服务器的地址: www.asagc.top
- 远程仓库的用户名: remote_user
- 本地client terminal使用的 :git bash
- 本地设置的用户名: local_user
当自己建立远程仓库后,在进行clone/push/pull等涉及到远程仓库的操作时,每次都需要输入密码.比较的麻烦,其实只要生成一份私钥private key
及公钥public key
,并把公钥上传到git所在服务器就可以达成免密操作的效果.
生成秘钥 🗝
如果之前生成过秘钥可以直接跳过这部分.没有生成过秘钥的可以接着继续.
首先执行以下cmd:
cd ~/.ssh
ssh-keygen
显示如下:
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Asa_sand/.ssh/id_rsa):
此时输入一个文件名,用来建立文件保存秘钥,不输入直接点击回车,会默认在/c/Users/Asa_sand/.ssh/id_rsa
生成,假设输入key_example
,按下回车键
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Asa_sand/.ssh/id_rsa):key_example
Enter passphrase (empty for no passphrase):
此时需要你输入一个密码,该密码可以为空,假设不输入,直接点击回车键,他会让你再次输入密码,因为前面未输入,此时直接点击回车键
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Asa_sand/.ssh/id_rsa):key_example
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
这时候就已经生成了秘钥了,terminal输出如下(local_user@Sand其实就是 你的git设置的用户名@电脑名):
以下输出手动打码了 \*是代表的任意字符
Your identification has been saved in key_example
Your public key has been saved in key_example.pub
The key fingerprint is:
SHA256:+JSv*************************************sQ local_user@Sand
The key's randomart image is:
+---[RSA 3072]----+
| **.. |
|o . ... . |
|o= = * + |
|*** O O.o. |
|o**+ +**** |
|....o o=. |
|**** . ..*. |
|o.o **** |
| o. ++ |
+----[SHA256]-----+
此时你可以执行以下cmd:
ls
输出的文件名会出现:(有就可以)
//如果前面输入秘钥名为空时,输出:
id_rsa id_rsa.pub
//如果输入为key_example,则输出
key_example key_example.pub
key_example
就是生成的秘钥中的私钥了.
key_example.pub
则是生成秘钥中的公钥,也就是接下来需要上传的那个秘钥了.
上传秘钥中的公钥⬆
上传秘钥的cmd是 ssh-copy-id
,看这cmd也大概能看出来是要复制id的.使用以下指令上传公钥,也就是前面生成的key_example.pub文件里的内容了
命令如下:
//如果前面输入秘钥名为空时,可以直接输入以下指令,会默认上传 id_rsa.pub
ssh-copy-id remote_user@www.asagc.top
//如果输入为key_example,则输入以下指令,指定上传key_example.pub
ssh-copy-id -i key_example.pub remote_user@www.asagc.top
//注意一个问题`remote_user@www.asagc.top` 中的 remote_user@之所以要输入
//是因为此时git bash使用的 用户名为local_user,服务器中并没有这个用户名,所以需要使用remote_user@来显示的指定用户名
//假设在本地git bash你设置的用户名为remote_user时,上述的`remote_user@www.asagc.top`就可以简写为`www.asagc.top`
- PS:上述的
remote_user@www.asagc.top
或者www.asagc.top
其实可以再简写一下,可以给这个host(即地址)设置一个别名(如asagc),然后上述就可以直接使用ssh-copy-id asagc
,并且不需要考虑用户名的问题,因为可以一起设置.设置方法可以参考:[此处应有链接]
点击回车后输出如下:
中间会要求输入密码,输入完成后回车即可(PS.密码输入时是不会显示在terminal上的).
ssh-copy-id -i key_example.pub remote_user@www.asagc.top
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/c/Users/local_user/.ssh/key_example.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
remote_user@www.asagc.xyz's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'remote_user@www.asagc.top'"
and check to make sure that only the key(s) you wanted were added.
到此为止就上传完成了😆
此时按照terminal的输出,直接调用cmd ssh 'remote_user@www.asagc.top'
时,会直接ssh登录到服务器,无需输入密码.
此时git的相关操作也就可以实现免密了.
NOTE
在使用git clone时还需要注意一个问题,因为本地git 设置的用户名为 local_user,所以假设远程仓库地址为ssh://www.asagc.top/~/data/repo
调用git clone ssh://www.asagc.top/~/data/repo
是不会成功的.
需要调用git clone ssh://remote_user@www.asagc.top/~/data/repo
.
若想要使用git clone ssh://www.asagc.top/~/data/repo
,有两个方式:
- 设置本地的git 用户为 remote_user.
- 给
www.asagc.top
设置别名,设置方法可以参考:[此处应有链接].