LoginSignup
5
1

More than 3 years have passed since last update.

インターネットに接続されていない環境下でWindows ServerにOpenSSHを入れる

Last updated at Posted at 2019-07-02

インターネットに接続されていない環境下でWindows ServerにOpenSSHを入れる

  • 前提:RDPでアクセス可能である
  • 公開鍵認証でログインできるようにする

前準備

ココを参考にする
https://github.com/PowerShell/Win32-OpenSSH/wiki/Install-Win32-OpenSSH

あらかじめダウンロードしておき、Windowsに移す。
https://github.com/PowerShell/Win32-OpenSSH/releases/download/v8.0.0.0p1-Beta/OpenSSH-Win64.zip
RDPでは、ローカルでファイルをコピーして、リモートでペーストするだけで良い。簡単。

インストール

解凍して C:\ 直下に置く。

PowerShellを 管理者モード で立ち上げる。
解凍したディレクトリに移動し、以下を実行。

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS C:\Windows\system32> cd C:\OpenSSH-Win64\

PS C:\OpenSSH-Win64> powershell.exe -ExecutionPolicy Bypass -File install-sshd.ps1
[SC] SetServiceObjectSecurity SUCCESS
[SC] ChangeServiceConfig2 SUCCESS
[SC] ChangeServiceConfig2 SUCCESS
sshd and ssh-agent services successfully installed

PS C:\OpenSSH-Win64> New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
Name                  : sshd
DisplayName           : OpenSSH Server (sshd)
Description           :
DisplayGroup          :
Group                 :
Enabled               : True
Profile               : Any
Platform              : {}
Direction             : Inbound
Action                : Allow
EdgeTraversalPolicy   : Block
LooseSourceMapping    : False
LocalOnlyMapping      : False
Owner                 :
PrimaryStatus         : OK
Status                : The rule was parsed successfully from the store. (65536)
EnforcementStatus     : NotApplicable
PolicyStoreSource     : PersistentStore
PolicyStoreSourceType : Local

PS C:\OpenSSH-Win64> net start sshd
The OpenSSH SSH Server service is starting..
The OpenSSH SSH Server service was started successfully.

これで設定ファイルなどが生成される

設定の変更

sshd_config の編集

PS C:\OpenSSH-Win64> notepad C:\ProgramData\ssh\sshd_config

ファイルの一番下にある以下の2行を削除する。
(このファイルが存在しない場合、公開鍵認証が途中でストップして失敗するため。)

Match Group administrators
       AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys

sshdを再起動して設定を反映。

PS C:\OpenSSH-Win64>net stop sshd
The OpenSSH SSH Server service was stopped successfully.

PS C:\OpenSSH-Win64> net start sshd
The OpenSSH SSH Server service is starting.
The OpenSSH SSH Server service was started successfully.

公開鍵認証

公開鍵をなんとかして ~/.ssh/authorized_keys に書き込む。
普通にRDP越しのコピペが楽で良い。

このとき、ファイルのアクセス権に注意。
「プロパティ>セキュリティ」を確認し、EVERYONEに対してアクセス権があると、鍵を読み込んでくれないため、要確認。

これで公開鍵認証によるSSHログインが可能になるはず。
うまくいかない場合は sshd_config に以下の設定を追加すると C:\ProgramData\ssh\logs\sshd.log にログが作成されるようになるので、がんばってトラブルシュートしてください。

SyslogFacility LOCAL0
LogLevel DEBUG3

その他ハマりポイント

  • デフォルト設定だとEd25519鍵が拒否される
5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1