Windows Server 2019・Windows10

  • Windows Server 2019・Windows10からはOpenSSHを公式サポート
    • SSHクライアントは有効化済み
    • SSHサーバは自分で有効化する必要あり
      • サービスの初期設定は手動、停止状態なので、必要に応じて起動、自動化する

Windows Server 2016

インストール

インストール

ファイアウォールの設定

  • WindowsFirewall?でPort22をOpen
    New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22

起動

net start sshd
  • 初回起動時に、以下に鍵ファイルが生成される
    C:\ProgramData\ssh

インストール実行例

PS C:\opt\openssh> powershell.exe -ExecutionPolicy Bypass -File install-sshd.ps1
  [*] C:\opt\openssh\moduli
Inheritance is removed from 'C:\opt\openssh\moduli'.
'NT AUTHORITY\Authenticated Users' now has Read access to 'C:\opt\openssh\moduli'.
'BUILTIN\Users' now has Read access to 'C:\opt\openssh\moduli'.
      Repaired permissions

[SC] SetServiceObjectSecurity SUCCESS
[SC] ChangeServiceConfig2 SUCCESS
[SC] ChangeServiceConfig2 SUCCESS
sshd and ssh-agent services successfully installed
PS C:\opt\openssh> 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                : 規則は、ストアから正常に解析されました。 (65536)
EnforcementStatus     : NotApplicable
PolicyStoreSource     : PersistentStore
PolicyStoreSourceType : Local


PS C:\opt\openssh> net start sshd
OpenSSH SSH Server サービスを開始します..
OpenSSH SSH Server サービスは正常に開始されました。

設定

サービス設定

  • サービスの画面を起動
  • サービス一覧の中に「OpenSSH SSH Server」があるので、これを右クリックし、プロパティを選択
  • プロパティ画面の中のスタートアップの種類を「手動」から「自動」へ変更

sshd_config の編集

  • インストールフォルダ(sshd.exeがあるフォルダ)に sshd_config_default があるが、設定ファイルの場所はここではない
  • C:\ProgramData?\ssh\sshd_config が設定ファイルのパス
  • 但し、エクスプローラから直接編集しようとすると「アクセス拒否」になる
  • 以下のようにコマンドを実行して編集する
    PS> cd \ProgramData\ssh
    PS> Invoke-Item sshd_config
    • メモ帳などで編集
  • 編集
    • コメントアウトする。これが有効だとAdministratorsグループ所属の場合は、.sshの下のauthorized_keysを読んでくれない
      # 一番下の2行はコメントアウト
      # Match Group administrators
      #       AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys
  • ログの出力先・レベル変更
    # Logging
    SyslogFacility LOCAL0 # LOCAL0に変更(C:\ProgramData\ssh\logs に出力されるようになる)
    LogLevel DEBUG        # ここは一般的なsshd_configの通り
  • defaultで有効になっているが、念のためコメントアウトを解除
    PubkeyAuthentication yes

administrators_authorized_keys の作成

$acl = Get-Acl C:\ProgramData\ssh\administrators_authorized_keys
$acl.SetAccessRuleProtection($true, $false)
$administratorsRule = New-Object system.security.accesscontrol.filesystemaccessrule("Administrators","FullControl","Allow")
$systemRule = New-Object system.security.accesscontrol.filesystemaccessrule("SYSTEM","FullControl","Allow")
$acl.SetAccessRule($administratorsRule)
$acl.SetAccessRule($systemRule)
$acl | Set-Acl

アクセス権限の変更

  • 再度、管理者権限 の PowerShell 上で OpenSSH インストールフォルダに移動
  • [.\FixHostFilePermission?.ps1] を実行
    • インストール中の問いには全て [Y] で OK

トラブルシューティング

デバッグモードで起動してみる

PS C:\opt\openssh> sshd -d
debug1: sshd version OpenSSH_for_Windows_8.6, LibreSSL 3.3.3
debug1: private host key #0: ssh-rsa SHA256:*******************************************
debug1: private host key #1: ecdsa-sha2-nistp256 SHA256:*******************************************
debug1: private host key #2: ssh-ed25519 SHA256:*******************************************
debug1: rexec_argv[0]='C:\\opt\\openssh\\sshd.exe'
debug1: rexec_argv[1]='-d'
debug1: Bind to port 22 on ::.
Bind to port 22 on :: failed: Permission denied.
debug1: Bind to port 22 on 0.0.0.0.
Bind to port 22 on 0.0.0.0 failed: Permission denied.
Cannot bind any address.

システム エラー 1067 が発生しました。

  • 解決策1:FixHostFilePermissions?.ps1 の実行
    powershell.exe -ExecutionPolicy Bypass -File .\FixHostFilePermissions.ps1

Bind to port 22 on :: failed: Permission denied.

  • 他に22番ポートを使っているプロセスがないか、netstat コマンドを使って確認

no matching cipher found.

It is also possible that a host key has just been changed

no matching key exchange method found

アンインストール

  • 管理者権限 の PowerShell 上で OpenSSH インストールフォルダに移動し、下記を実行
    powershell.exe -ExecutionPolicy Bypass -File uninstall-sshd.ps1

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2021-06-16 (水) 11:10:20 (1039d)