需求
最近工作中需要配置一下 CentOS 7 系统能够通过 SSH 免密登录到 Windows server.
Windows server 安装 SSH 服务
安装 cygwin, 下载地址 https://www.cygwin.com/.
在提示你选择安装哪些包的时候, 勾上我们所需要的 openssh.
安装好之后, 打开 cygwin 的终端, 运行 cygrunsrv -L 查看已安装的服务
- $ cygrunsrv -L
- cygsshd
Cygwin 运行 OpenSSH 配置文件
$ SSH-host-config -y
配置完之后启动 SSH 服务
cygrunsrv --start cygsshd
到这里 Windows server 的 SSH 服务就安装完成了.
实现 SSH 无密码登录
登录到我们的 CentOS 系统中, 先检查有没有 SSH 公钥文件夹, 没有的话则生成一个:
SSH-keygen -t rsa
然后用 SSH-copy-id 将公钥复制到 Windows server 服务器上
SSH-copy-id -i .SSH/id_rsa.pub administrator@192.168.xx.xx
SSH-copy-id 会将 key 写到远程机器的 ~/ .SSH/authorized_key 文件中, 其中 administrator 就是你 Windows 服务中的用户后,@后面是 Windows 服务的 ip, 运行这条命令需要密码登录.
依然无法免密登录
本以为安装好 SSH, 配置好公钥, 就万事大吉可以免密登录了, 然而现实会一次又一次的打脸.
运行命令看能否免密登录
SSH administrator@192.168.xx.xx
结果确是始终需要我输入密码, 无奈的我只能继续网上查找解决方案. 大概搜出了以下几种方案:
SSH 对目录的权限有要求, 需要设置. SSH 的权限
- chmod 700 .SSH
- chmod 600 .SSH/authorized_keys
配置 sshd_config
- cd /etc
- vi sshd_config
修改以下配置:
- PubkeyAuthentication yes
- PasswordAuthentication no
配置完之后重启 SSH 服务
- cygrunsrv --stop cygsshd
- cygrunsrv --start cygsshd
然而对于以上几种方案试了 n 遍, 始终未能成功,
尝试第一种方案依然是需要密码登录
第二种则是提示
Permission denied (publickey,keyboard-interactive)
解决办法
后来一遍遍的看了下 sshd_config 文件中的参数, 在 Authentication 下发现了一个 StrictModes 参数.
- # Authentication:
- #LoginGraceTime 2m
- #PermitRootLogin prohibit-password
- #StrictModes yes
- #MaxAuthTries 6
- #MaxSessions 10
看了下 StrictModes 的解释, 大意是指定 sshd 在接受登录之前是否需要检查用户文件和主目录的文件模式和所有权. 这通常是必要的, 因为新手经常会把自己的目录和文件设成任何人都有写权限, 默认是 yes.
- StrictModes
- Specifies whether sshd(8) should check file modes and ownership of the user's files
- and home directory before accepting login. This is normally desirable because
- novices sometimes accidentally leave their directory or files world-writable. The
- default is "yes". Note that this does not apply to ChrootDirectory, whose
- permissions and ownership are checked unconditionally.
于是尝试把 StrictModes 改为 no, 重启后发现可以正常免密登录了.
来源: http://www.jianshu.com/p/97e12987a090