CentOS 7实现免密登录到Windows Server

需求

最近工作中需要配置一下CentOS 7系统能够通过ssh免密登录到windows server。

windows server安装ssh服务

  1. 安装cygwin,下载地址https://www.cygwin.com/。
  2. 在提示你选择安装哪些包的时候,勾上我们所需要的openssh。
  3. 安装好之后,打开cygwin的终端,运行cygrunsrv -L查看已安装的服务
$ cygrunsrv -L
cygsshd
  1. Cygwin运行OpenSSH配置文件
$ ssh-host-config -y
  1. 配置完之后启动ssh服务
cygrunsrv --start cygsshd

到这里window server的ssh服务就安装完成了。

实现ssh无密码登录

登录到我们的CentOS系统中,先检查有没有ssh公钥文件夹,没有的话则生成一个:

ssh-keygen -t rsa

然后用ssh-copy-id将公钥复制到windows server服务器上

ssh-copy-id -i .ssh/id_rsa.pub [email protected]

ssh-copy-id会将key写到远程机器的 ~/ .ssh/authorized_key文件中,其中administrator就是你windows服务中的用户后,@后面是window服务的ip,运行这条命令需要密码登录。

依然无法免密登录

本以为安装好ssh,配置好公钥,就万事大吉可以免密登录了,然而现实会一次又一次的打脸。

运行命令看能否免密登录

ssh [email protected]

结果确是始终需要我输入密码,无奈的我只能继续网上查找解决方案。大概搜出了以下几种方案:

  • 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遍,始终未能成功,

  1. 尝试第一种方案依然是需要密码登录
  2. 第二种则是提示
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,重启后发现可以正常免密登录了。

你可能感兴趣的:(CentOS 7实现免密登录到Windows Server)