如果ssh-gen生成私钥的时候设置了口令,那么每次验证的时候都需要输入口令,有点麻烦。一个办法是使用ssh-agent启动一个密钥服务,然后通过ssh-add将某个密钥加入到服务中去,这样就不需要每次输密码。但是ssh-agent的使用还是不太方便,每次都需要手动启动,然后在shell中设置环境变量。
macOS在这一点上比较友好,可以通过keyring提供密钥服务,只需
ssh-add -k
就行了。不知道什么时候Windows的Credential Manager也能做到这一点。
所以一个变通的办法是使用PuTTY提供的Pageant程序来提供密钥服务器的角色。Pageant和PuTTY一起发行,所需要需要安装PuTTY:
scoop install putty
然后如果你之前的密钥是通过ssh-keygen生成的,则需要通过PuTTYgen将其转化为PuTTY能够识别的.ppk格式,主要是下面几步:
- Run PuTTYgen;
- Press Load to load the private key in OpenSSH format;
- Press Save private key to save the private key in .ppk format;
- Now you can load the private key in .ppk format to Pageant.
具体参考Pageant refuses to load SSH-2 key generated with GitBash
接下来就是要设置Pageant的自启动,参考pageant-autoload-keys-at-startup.txt
To make Pageant automatically run and load keys at startup:
- Find the location of pageant.exe
- Windows key + R to open the 'run' dialog box
- Type: 'shell:startup' in the dialog box
- Create a shortcut to the pageant.exe and put into this startup folder.
- Right click on the shortcut and open 'Properties'
- In 'Target' add: "<route to>/pageant.exe" myprivatekeyname.ppk
- In 'Start in' add: "<route to myprivatekeyname.ppk>"
- Click on the shortcut link and check that Pageant has started and has loaded your keys
最后,为了在Cygwin里面使用Pageant提供的密钥服务,需要安装ssh-pageant:
apt-cyg install ssh-pageant
然后在Shell的启动脚本中加入下面内容:
# ssh-pageant
eval $(/usr/bin/ssh-pageant -r -a "/tmp/.ssh-pageant-$USERNAME")
如果Cygwin窗口启动时显示:ssh-pageant pid 9590
。那么差不多就成功了
其他参考
(完)
2020-05-05 更新
在Git里面使用的话,需要定义一个环境变量GIT_SSH_COMMAND(注意不是GIT_SSH),内容如下:
C:\\path\\to\\klink.exe -auto-store-sshkey
Klink来自于Kitty,因为Putty里面的Plink不支持“-auto-store-sshkey”选项。
(更新完)
2020-08-07 更新
如果要使用代理的话,可以设置
set GIT_SSH_COMMAND=C:/path/to/link.exe -auto-store-sshkey -proxycmd "ncat --proxy-type http --proxy 127.0.0.1:8080 %host %port"
klink的–proxycmd有点像ssh的proxycommand配置:
Host hostToYourRepo
ProxyCommand ncat --proxy hostToYourProxy:1080 %h %p
ncat则是nmap的一部分,来自于:https://nmap.org/ncat/
尝试了以下klink的-nc选项,好像不行:
plink -agent -l %user %proxyhost -nc %host:%port
上面的例子来自于Setting up an SSH proxy with PuTTY¶
其他参考
- 10 useful ncat (nc) Command Examples for Linux Systemshttps://www.linuxtechi.com/nc-ncat-command-examples-linux-systems/
- Tunneling ssh via a SOCKS5 Proxy on Windows
- How can I use SSH with a SOCKS 5 proxy?
- Tutorial: how to use git through a proxy
- netcat
- Run SSH over a SOCKS proxy?
- How do I connect to a server behind an socks5 proxy through SSH on mac/linux?
- Connect to an SSH server over an HTTP proxy using plink and netcat
- PuTTY configuration equivalent to OpenSSH ProxyCommand
- Multiple ssh hop connections with Putty
- putty
(更新完)
2022-10-10更新
不知何故,使用plink做Git的SSH客户端的时候,会出现fatal: protocol error: bad line length character: logi
错误。
Git: fatal: protocol error: bad line length character: logi讨论了这个问题,不过没有给出解决方案。
如何更改cygwin的home目录
可以通过修改/etc/nsswitch.conf
,具体格式和内容,参考https://cygwin.com/cygwin-ug-net/ntsec.html#ntsec-mapping-nsswitch-passwd。
修改home目录的话,可以设置其中的db_home条目,但是对于域用户似乎作用不大,不如直接设成windows,即
db_home: windows
db_shell: /usr/bin/zsh
上面把默认的shell设置成了zsh。
参考
- How can I change my Cygwin home folder after installation?
- Changing the HOME for Cygwin
- Linux shell experience on Windows: Cygwin, Oh My Zsh and Windows Terminal。
(更新完)