Windows Server on EC2 に ssh で接続する

thumbnail

EC2 上で稼動する Windows Server に向けて、公開鍵認証で SSH 接続するまでの設定手順を解説します。

執筆背景

EC2 上で稼働する Windows Server に接続したい場合は、「リモートデスクトップ(RDP)」が第一候補です。

RDP 接続により、リモートサーバに対して「使い慣れた Windows 画面での GUI 操作」が可能です。また、そのセッション内にて「PowerShell や CommandPrompt の起動、コマンド実行」も可能なため、aws-cli の ssm で接続するよりも直感的に操作できます。

「人が直接操作する」のであれば RDP で十分ですが、システム運用状況によっては「Windows Server へ定期的に接続して対応する処理(ex. 定常業務)」も発生します。その自動化ニーズが発生することを踏まえると「Windows Server での CLI 操作」についても、一通りの知見を身に着けておくことが望ましいのではないでしょうか。

AWS が提供する Windows Server 用の AMIs には、デフォルトで SSM Agent がプリインストール されています。そのため、適切な IAM インスタンスプロファイルが付与されていれば、ssm によるリモート接続が可能です。ただし、サーバ内の sshd service はデフォルトでオフとなっているため、もし ssh でリモート接続したい場合には、サーバ内での事前準備が必要となります。

本ブログでは、EC2 上の Windows Server に向けて、SSH で接続するまでの流れを解説します。

前提

本ブログの内容は、以下環境にて実行したものです。

1
2
3
4
5
6
# EC2 の AMI
Windows_Server-2022-English-Full-Base-2024.01.16

# Windows Server への接続クライアント
$ ssh -V
OpenSSH_9.6p1, OpenSSL 3.0.8 7 Feb 2023

Windows Server への SSH 接続に向けて、以下の流れで説明します。

  • リモートデスクトップ(RDP)で Windows Server に接続し、sshd を立てる
  • Windows Server に向けて、SSH で接続する

また、本手順では Windows Server 内部での設定のみを説明対象としているため、EC2 に紐づくセキュリティグループやサブネットのルートテーブルは、予め適切に設定されていることとします。

作業途中のトラブルシューティング用に「tips」を記載していますので、必要に応じて参照してください。

Windows Server に sshd を立てる

Windows Server に RDP(リモートデスクトップ)で接続して、サーバ内に sshd を立てるまでの流れを説明します。
RDP クライアントには、管理者権限を持つ Administrator を利用します。

OpenSSH.Server をインストールする

まず、RDP でサーバに接続したら、PowerShell を管理者権限で起動します。

powershell-admin-image

以下コマンドを実行して、OpenSSH のインストール状況を確認します。

1
Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'

EC2 立ち上げ直後の筆者環境(Windows_Server-2022-English-Full-Base-2024.01.16)では、以下出力が得られました。

1
2
3
4
5
Name  : OpenSSH.Client~~~~0.0.1.0
State : Installed

Name : OpenSSH.Server~~~~0.0.1.0
State : NotPresent

State の値により OpenSSH.Server のインストール有無を確認し、NotPresent であれば、以下のインストールコマンドを実行します。

1
2
# Server
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0

インストールコマンドの実行後、OpenSSH.Server の State 値が Installed と表示されていれば、インストール作業は完了です。

1
2
3
4
5
6
7
$ Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'

Name : OpenSSH.Client~~~~0.0.1.0
State : Installed

Name : OpenSSH.Server~~~~0.0.1.0
State : Installed

sshd service を起動する

管理者権限で Powershell を起動した状態で、以下のコマンドを実行します。

1
2
3
4
5
# sshd の起動
Start-Service sshd

# sshd の起動を自動化
Set-Service -Name sshd -StartupType 'Automatic'

サービスの起動状況は、Get-Service <サービス名> により確認できます。

1
2
3
4
5
$ Get-Service sshd

Status Name DisplayName
------ ---- -----------
Running sshd OpenSSH SSH Server

続いて、ファイアウォールの設定に移ります。
以下コマンドにより、OpenSSH-Server-In-TCP の設定状況を確認してください。

1
Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue

ルールを設定済みであれば、以下の出力が得られます。
OpenSSH-Server-In-TCP が表示され、かつ、Enabled の値は True であれば、ルール追加は不要です。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue

Name : OpenSSH-Server-In-TCP
DisplayName : OpenSSH SSH Server (sshd)
Description : Inbound rule for OpenSSH SSH Server (sshd)
DisplayGroup : OpenSSH Server
Group : OpenSSH Server
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
RemoteDynamicKeywordAddresses : {}
PolicyAppId :

確認コマンドの実行後、以下の出力が得られた場合には、ルールを再作成してください。

  • Get-NetFirewallRule にて OpenSSH-Server-In-TCP のルールが表示されない
  • OpenSSH-Server-In-TCP のルールは表示されるが、Enabled の値は異なる
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# ルールの削除
$ Remove-NetFirewallRule -Name OpenSSH-Server-In-TCP

# ルールの追加
$ New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22

Name : OpenSSH-Server-In-TCP
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
RemoteDynamicKeywordAddresses : {}
PolicyAppId :

ルールの再作成後、以下の出力が得られたら、ファイアウォールルールの設定作業は完了です。

1
2
3
4
5
$ Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue | Select-Object Name, Enabled

Name Enabled
---- -------
OpenSSH-Server-In-TCP True

以上で sshd service の起動が完了しました。

Windows Server へ SSH で接続する

パスワード認証での接続

Windows Server にて sshd を起動済みのため、通常の ssh コマンドにて接続可能です。

1
ssh Administrator@<windows server ip>

公開鍵認証での接続

(オプション)公開鍵/秘密鍵のペアを作成する

※本ブログでは、説明の都合上「ログイン先サーバ(Windows Server)で生成した 秘密鍵 を、ログイン元サーバに scp で複製する」という手順を説明しております。しかし、実運用においては「ログイン元サーバで生成した 公開鍵 を、ログイン先サーバに scp で複製する」という方法を利用してください。

以下、Windows Server 内にて、公開鍵/秘密鍵のペアを作成する手順を説明します。

RDP により Windows Server へ接続後、PowerShell を起動します。
以下コマンドを実行して、公開鍵認証用のキーペアを作成してください。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ ssh-keygen.exe
Generating public/private rsa key pair.
Enter file in which to save the key (C:\Users\Administrator/.ssh/id_rsa):
Created directory 'C:\Users\Administrator/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in C:\Users\Administrator/.ssh/id_rsa.
Your public key has been saved in C:\Users\Administrator/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:++1ra2HvRWI5nt3XhJi8RZY0awFXUoTcDxr3nEJIF1k administrator@EC2AMAZ-HK9TUB0
The key's randomart image is:
+---[RSA 3072]----+
| ..o=XE+|
| .o==*.|
| .+*+o|
| ..B ++|
| S + O o|
| . o= Bo|
| . ..oo *|
| . .o ...|
| .+=+. |
+----[SHA256]-----+
1
2
3
4
5
6
7
8
$ ls ~/.ssh

Directory: C:\Users\Administrator\.ssh

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 1/23/2024 8:43 AM 2622 id_rsa
-a---- 1/23/2024 8:43 AM 584 id_rsa.pub

scp 等により秘密鍵(id_rsa)をログイン元のサーバへ複製した後、Windows Server 内からは削除してください。

1
2
3
4
5
# ex. Windows Server -> Linux に向けて、 scp により秘密鍵を複製する場合

$ scp Administrator@<windows server ip>:/Users/Administrator/.ssh/id_rsa ~/.ssh/id_rsa_windows_server
Administrator@<windows server ip>'s password:
id_rsa 100% 2622 2.6MB/s 00:00
1
2
3
4
5
6
7
8
9
10
# 秘密鍵を Windows Server 内から削除する
$ rm ~/.ssh/id_rsa

$ ls ~/.ssh

Directory: C:\Users\Administrator\.ssh

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 1/23/2024 8:43 AM 584 id_rsa.pub

公開鍵認証を有効にする

Windows Server 内の sshd_config ファイル内の設定項目を編集して、公開鍵認証をオンに変更します。

このファイルは、デフォルトでは C:\ProgramData\ssh\ 配下に配置されています。
RDP によりサーバへ接続後、メモ帳などで sshd_config を開き、関連項目を編集してください。

以下項目が、公開鍵認証の有効化・無効化に対応しています。

1
2
3
#PubkeyAuthentication yes

PubkeyAuthentication yes

パスワード認証が不要であれば、次の項目で設定可能です。

1
2
3
#PasswordAuthentication yes

PasswordAuthentication no

変更内容を反映させるために、sshd を再起動します。

1
Restart-Service sshd

administrators_authorized_keys に公開鍵を登録する

Administrator などの「管理ユーザ」を利用して、公開鍵認証により SSH 接続する場合、C:\ProgramData\ssh\ 配下の administrators_authorized_keys に公開鍵を登録する必要があります。

RDP により接続後、PowerShell にて administrators_authorized_keys を作成し、公開鍵を登録します。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# ローカル変数 $authorizedKey に、公開鍵(id_rsa.pub)の値を登録
$ $authorizedKey = Get-Content -Path $env:USERPROFILE\.ssh\id_rsa.pub

# $authorizedKey に登録された値を確認
$ Write-Output $authorizedKey
(略)

# $authorizedKey の値(公開鍵)を、administrators_authorized_keys に追記
$ Add-Content -Force -Path $env:ProgramData\ssh\administrators_authorized_keys -Value $authorizedKey

# administrators_authorized_keys の中身を確認
$ cat $env:ProgramData\ssh\administrators_authorized_keys
(略)

# ファイル権限の確認
$ icacls.exe $env:ProgramData\ssh\administrators_authorized_keys
C:\ProgramData\ssh\administrators_authorized_keys NT AUTHORITY\SYSTEM:(I)(F)
BUILTIN\Administrators:(I)(F)
NT AUTHORITY\Authenticated Users:(I)(RX)

Successfully processed 1 files; Failed processing 0 files

# ファイル権限の変更
$ icacls.exe $env:ProgramData\ssh\administrators_authorized_keys /inheritance:r /grant Administrators:F /grant SYSTEM:F
processed file: C:\ProgramData\ssh\administrators_authorized_keys
Successfully processed 1 files; Failed processing 0 files

# ファイル権限の再確認
icacls.exe $env:ProgramData\ssh\administrators_authorized_keys
C:\ProgramData\ssh\administrators_authorized_keys NT AUTHORITY\SYSTEM:(F)
BUILTIN\Administrators:(F)

Successfully processed 1 files; Failed processing 0 files

administrators_authorized_keys の権限が、上記ログの通りに表示されていれば、公開鍵の登録は完了です。

公開鍵の登録後、SSH による接続を確認します。

1
2
3
# ex. Linux -> Windows Server に向けた公開鍵認証による SSH 接続

ssh Administrator@<windows server ip> -i ~/.ssh/id_rsa_windows_server

以上で、Windows Server に向けた、公開鍵認証による SSH 接続設定は完了です。

tips

SSH 接続の動作検証時に役立つ tips を紹介します。

SSH 接続で起動するシェルを「PowerShell」にする

Windows Server への SSH 接続時、デフォルトでは「CommandPrompt(cmd.exe)」が起動します。

管理者特権で起動した PowerShell にて以下コマンドを実行することで、SSH 接続時に起動するシェルを「PowerShell(powershell.exe)」に変更できます。

1
2
3
4
5
6
7
8
$ New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -PropertyType String -Force

DefaultShell : C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
PSPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\OpenSSH
PSParentPath : Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE
PSChildName : OpenSSH
PSDrive : HKLM
PSProvider : Microsoft.PowerShell.Core\Registry

sshd のログ確認

sshd_config に記載された「# Logging」を編集することで、sshd ログの「出力内容」と「出力場所」を変更できます。

デフォルトでは以下内容が記載されています。

1
2
3
# Logging
#SyslogFacility AUTH
#LogLevel INFO

#(コメントアウト) を外して以下設定に変更することで、sshd がより詳細なログを出力するようになります。

1
2
SyslogFacility LOCAL0
LogLevel DEBUG3

SyslogFacility LOCAL0

  • デフォルトの値は AUTH です
  • LOCAL0 を指定すると、ログの出力先がC:\ProgramData\ssh\logs\ になります

LogLevel DEBUG3

  • デフォルトの値は INFO です
  • パラメータは複数あり、右側の値ほどログ出力内容が詳細になります
1
2
3
# LogLevel のパラメータ一覧

QUIET, FATAL, ERROR, INFO, VERBOSE, DEBUG, DEBUG1, DEBUG2, DEBUG3

sshd を再起動すると、sshd_config に指定したログが出力されるようになります。

参照サイト