C:\Program Files\OpenSSH 以管理员权限安装SSHD和ssh-agent服务:
powershell.exe -ExecutionPolicy Bypass -File install-sshd.ps1
生成Server Keys并且限制这些文件的访问权限,以管理员权限运行:
.\ssh-keygen.exe -A
powershell.exe -ExecutionPolicy Bypass -File .\FixHostFilePermissions.ps1 -Confirm:$false
New-NetFirewallRule -Protocol TCP -LocalPort 22 -Direction Inbound -Action Allow -DisplayName SSH
开启服务:
net start sshd
Set-Service sshd -StartupType Automatic
Set-Service ssh-agent -StartupType Automatic
设置默认ssh Shell: 在服务器端的windows registry中设置ssh的默认shell,
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\OpenSSH\DefaultShell

安装后,可以直接使用用户名和密码方式从client登陆到host上:
ssh user@host
要使用密匙登陆需要采用以下步骤来设置:
在$HOME中建立.ssh目录
mkdir $HOME\.ssh
cd $HOME\.ssh
生成key pair
ssh-keygen -t rsa -f id_rsa
id_rsa 为key文件名,生成时会提示输入passphrase来保护private key,也可以为空。
运行源码中的FixUserFilePermissions.ps1来对进行权限设置
FixUserFilePermissions.ps1
开启ssh-agent:
net start ssh-agent
将key注册到ssh-agent
ssh-add id_rsa
使用private key来登陆host
ssh -i .\id_rsa user@host
同样在服务器端的用户目录$HOME下建立.ssh\,并在其中建立authorized_keys文件:
touch .ssh\authorized_keys
mkdir .ssh\other_keys
将之前客户端上生成的public client key 即 id_rsa.pub文件,发送到host上的$HOME\.ssh\other_keys\文件夹下,在服务器上,将这个id_ras.pub内容写入authorized_keys文件中。
cat .\other_keys\id_rsa.pub > authorized_keys
authorized_keys文件的编码要为UTF-8,而非windows默认编码
设置authorized_keys的访问权限
FixUserFilePermissions.ps1