ssh免密登录和Windows Git服务器

132 阅读1分钟

客户端

  • 生成私钥、公钥
$ ssh-keygen -t ed25519 -C "lxr@example.com" // -C注释
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/lxr/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in C:\Users\username/.ssh/id_ed25519.
Your public key has been saved in C:\Users\username/.ssh/id_ed25519.pub.
The key fingerprint is:
SHA256:OIzc1yE7joL2Bzy8!gS0j8eGK7bYaH1FmF3sDuMeSj8 username@LOCAL-HOSTNAME

The key's randomart image is:
+--[ED25519 256]--+
|        .        |
|         o       |
|    . + + .      |
|   o B * = .     |
|   o= B S .      |
|   .=B O o       |
|  + =+% o        |
| *oo.O.E         |
|+.o+=o. .        |
+----[SHA256]-----+
  • 发送公钥
$ cat /c/Users/lxr/.ssh/id_ed25519.pub
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOx77t5o475 Administrator@PC-20230304LWJY

服务端

  • 添加公钥
$ cat id_ed25519.pub >> ~/.ssh/authorized_keys

如果服务端是Windows

修改文件 C:\ProgramData\ssh\sshd_config

确保以下2条没有被注释
PubkeyAuthentication yes
AuthorizedKeysFile	.ssh/authorized_keys

确保以下2条有注释掉
#Match Group administrators
#       AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys

Git仓库

推荐裸仓库

git init -bare sample.git

如果仓库不是裸仓库

git init sample

git config receive.denyCurrentBranch ignore

如果Git仓库在Windows

修改注册表,修改openssh默认shell

image.png

git clone lxr@192.168.0.20:D:/WORK/2_gitrepo/sample.git