需求
最近工作中需要配置一下CentOS 7系統(tǒng)能夠通過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
到這里window server的ssh服務就安裝完成了。
實現(xiàn)ssh無密碼登錄
登錄到我們的CentOS系統(tǒng)中,先檢查有沒有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服務中的用戶后,@后面是window服務的ip,運行這條命令需要密碼登錄。
依然無法免密登錄
本以為安裝好ssh,配置好公鑰,就萬事大吉可以免密登錄了,然而現(xiàn)實會一次又一次的打臉。
運行命令看能否免密登錄
ssh administrator@192.168.xx.xx
結果確是始終需要我輸入密碼,無奈的我只能繼續(xù)網(wǎng)上查找解決方案。大概搜出了以下幾種方案:
- 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文件中的參數(shù),在Authentication下發(fā)現(xiàn)了一個StrictModes參數(shù)。
# Authentication:
#LoginGraceTime 2m
#PermitRootLogin prohibit-password
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
看了下StrictModes的解釋,大意是指定sshd在接受登錄之前是否需要檢查用戶文件和主目錄的文件模式和所有權。這通常是必要的,因為新手經(jīng)常會把自己的目錄和文件設成任何人都有寫權限,默認是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,重啟后發(fā)現(xiàn)可以正常免密登錄了。