Windows Server には Microsoft が移植した OpenSSH Server が含まれており、Linux と同じ感覚で Windows マシンを管理できます。リモートデスクトップセッションを開かずに、SSH 経由で PowerShell コマンドを実行したり、スクリプトから作業を自動化したり、scp や WinSCP、FileZilla などの SFTP クライアントで安全にファイルをコピーしたりできます。この記事では、Windows Server 2019・2022・2025 での SSH サーバーのインストールと起動、PowerShell を既定のシェルにする設定、管理者アカウントでの鍵認証によるログイン、ポートの変更、ファイル転送を解説します。
管理者として PowerShell を開きます。次のコマンドで、クライアントとサーバーがインストールされているかを確認できます。Windows Server 2025 では通常サーバーがすでに含まれており、有効にするだけで使えます。
Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'
# Install the server if its State is NotPresent
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
OpenSSH が含まれていない Windows Server 2016 では、GitHub にある Microsoft の PowerShell/Win32-OpenSSH のリリースから Win32-OpenSSH の MSI パッケージをインストールしてください。
# Start sshd now and at every boot
Start-Service sshd
Set-Service -Name sshd -StartupType Automatic
# The installer normally creates an inbound rule for TCP 22; create it if it is missing
if (-not (Get-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -ErrorAction SilentlyContinue)) {
New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' `
-Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
}
Get-Service sshd
netstat -ano | findstr ":22"
手元のコンピューターから ssh [email protected] で接続し、Administrator のパスワードを入力します(Windows 10/11、macOS、Linux にはいずれも SSH クライアントが標準で入っています)。既定では cmd.exe のプロンプトが表示されます。
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell `
-Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -PropertyType String -Force
これで新しい SSH セッションは PowerShell で開始されます。PowerShell 7 をインストールしている場合は、代わりに pwsh.exe のパスを指定してください。
Administrators グループに属するアカウントの場合、Windows の OpenSSH はユーザー自身の .ssh フォルダーではなく、共有ファイル C:\ProgramData\ssh\administrators_authorized_keys から公開鍵を読み込みます。このファイルには Administrators と SYSTEM だけがアクセスできるようにしておく必要があり、そうでないと sshd はファイルを無視します。
# On your own computer: create a key pair if you do not have one yet
ssh-keygen -t ed25519
# On the server (PowerShell as Administrator): paste your PUBLIC key (id_ed25519.pub)
Add-Content -Path "C:\ProgramData\ssh\administrators_authorized_keys" -Value "ssh-ed25519 AAAA...your-key... you@laptop"
icacls.exe "C:\ProgramData\ssh\administrators_authorized_keys" /inheritance:r /grant "Administrators:F" /grant "SYSTEM:F"
新しいウィンドウで鍵によるログインをテストします。問題なくログインできたら、メモ帳で C:\ProgramData\ssh\sshd_config を開き、次のように設定します。
PubkeyAuthentication yes
PasswordAuthentication no
Restart-Service sshd
新しく鍵認証でログインできることを確認するまで、現在のセッションは開いたままにしておいてください。一般ユーザー(管理者以外)の場合は、代わりに C:\Users\USERNAME\.ssh\authorized_keys に鍵を配置します。
# In C:\ProgramData\ssh\sshd_config set: Port 2222
New-NetFirewallRule -Name 'OpenSSH-2222' -DisplayName 'OpenSSH Server 2222' `
-Direction Inbound -Protocol TCP -LocalPort 2222 -Action Allow
Restart-Service sshd
Disable-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' # close 22 after testing 2222
自分のアドレスからのみ SSH を許可するには、ファイアウォールルールに -RemoteAddress 198.51.100.20 を追加します。Windows ファイアウォールのルールについて詳しくはWindows ファイアウォールでポートを開放・閉鎖する方法を参照してください。
# Upload a file to the Administrator desktop (use forward slashes in Windows paths)
scp .\site-backup.zip [email protected]:C:/Users/Administrator/Desktop/
# Download a folder recursively
scp -r [email protected]:C:/inetpub/wwwroot ./wwwroot-copy
# Interactive SFTP session
sftp -P 2222 [email protected]
WinSCP や FileZilla などの GUI クライアントも使えます。プロトコルに SFTP を選び、サーバーの IP とポート、鍵またはパスワードを指定します。大きなファイルの場合、リモートデスクトップのクリップボード経由でコピーするよりはるかに高速で確実です。
管理者アカウントの場合、鍵は administrators_authorized_keys に登録し、ステップ4のとおりのアクセス権を設定しておく必要があります。イベント ビューアー → アプリケーションとサービス ログ → OpenSSH → Operational(Event Viewer → Applications and Services Logs → OpenSSH → Operational)のログを確認してください。所有者やアクセス権の不備など、たいてい原因が記録されています。
sshd サービスが起動していない、ファイアウォールルールがない、または接続先のポートが間違っている、のいずれかです。手元のコンピューターで Test-NetConnection 203.0.113.10 -Port 22 を、サーバー上で Get-Service sshd を実行して確認してください。
はい。SSH とリモートデスクトップは併用できます。スクリプトやファイル転送には SSH を使い、GUI ツールにはリモートデスクトップを使う管理者も多くいます。
上記の手順で解決しない場合は、サポートチケットを送信して IMIDC の 24 時間 365 日対応テクニカルサポートへご連絡ください。サーバー IP、Windows Server のバージョン、実行した SSH コマンド、エラー出力の全文(ssh -v で詳細を確認できます)を添えていただくと、より迅速に調査できます。