[openssh]windows 10使用cygwin安装openssh服务端

2,625 阅读3分钟

install-openssh-with-cygwin-on-windows-10

本来windows10上已经有内置的openssh,我可能爱折腾不愿意使用内置的,所以使用cygwin重新安装了一个ssh服务.

安装步骤总结

  1. 安装cygwin
  2. 使用管理员权限运行cygwin安装opensshopenssl
  3. ssh-host-config命令开始配置ssh
  4. windows10内置的sshdssh-host-config默认的服务名冲突解决
  5. 一系列ssh-host-config配置,包括daemon名称 ntsec和创建默认的cyg_server来运行sshd服务
  6. ssh-user-config生成用户的密钥(非必须,可以使用密码登陆,用户就是windows的正常用户)
  7. 启动服务net start cygsshd
  8. 使用putty登陆localhost测试
  9. 卸载ssh
  10. 重新安装ssh

注意:cygwin使用的是windows的用户和权限,所以默认/etc/下是没用passwdgroup等文件的,不要太惊讶。

开始我也没用搞明白,而且ssh-host-config中创建一个cyg_server用户是真实的windows账户,但是需要组策略才能够看到,这个用户用来启动sshd服务的,而且不能用来登陆windows,因为是一个令牌用户,在组策略>计算机配置>windows配置>安全设置>本地策略>用户权限分配> 创建一个令牌对象中看到。

cygwin安装openssh和openssl

假设你已经安装了cygwin,使用cygwin安装openssh和openssl详情看参考:

how-to-get-ssh-command-line-access-to-windows-7-using-cygwin/

修复sshd service is already installed.

警告:sshd service is already installed.

sshd-service-installed-error

windows10-build-in-openssh

出现sshd service is already installed.的问题是因为windows 10内置了一个openssh,造成了冲突,现在只需要修改cygwin安装的openssh中的默认的sshd服务名称。

修改默认的sshd服务名

路径: C:/cygwin64/bin/sshd-host-config (32位的有所区别,找不到用everything搜索一下啊)

修改: service_name=sshd 为 service_name=cygsshd (cygsshd自定义的)

修改完之后重新执行ssh-host-config,上面的问题解决.

下面是运行中的截图.

ssh-host-config

sshd-as-service-ntsec

建议使用默认的cyg_server,没用必要重新自定义,我当时也是好奇,搞得走了很多弯路。

config-user-name-password

启动服务

也可以重启一下系统,服务会自动启动

net start cygsshd  # cygsshd是自定义的service_name
# 或者使用cygwin运行
cygrunsrv -S cygsshd

ssh-user-config

选择生成用户的RSA口令就行了,RSA比较常用,其它的不是很常用。

卸载ssh服务

# 删除sshd服务, sshd时默认的服务名,如果你定义了,输入你的服务名
cygrunsrv --stop sshd
cygrunsrv --remove sshd

# Delete any sshd or related users (such as cyg_server) from /etc/passwd
#   (use your favorite editor)

# Delete any sshd or related users (such as cyg_server) from the system
# 这里删除的是windows上的用户
net user sshd /delete
# 如果ssh-host-config配置过程中没用使用cyg_server,请替换为你自定义的名字
net user cyg_server /delete

重装ssh重新设置权限

如果设置权限不行,删除sshd.log和empty文件夹.


chown <USERNAME> /var/log/sshd.log
chown -R <USERNAME> /var/empty
chown <USERNAME> /etc/ssh*
chmod 755 /var/empty
chmod 644 /var/log/sshd.log

参考资料

Installing Cygwin and Starting the SSH Daemon

how-to-get-ssh-command-line-access-to-windows-7-using-cygwin/

cygwin-sshd-on-windows-domain

how-to-regenerate-ssh-client-keys

mkpasswd

mkgroup

how-to-uninstall-reinstall-cygwin-to-use-the-sshd