OpenSSH は、SSH プロトコルを使用するリモート ログイン用の接続ツールです。 クライアントとサーバーの間のすべてのトラフィックを暗号化して、盗聴、接続ハイジャック、その他の攻撃を排除します。
OpenSSH を使用して、OpenSSH Client がインストールされている Windows 10 (ビルド 1809 以降) または Windows Server 2019 デバイスから、OpenSSH Server がインストールされているデバイスに接続できます。
ということで、Oracle Cloud Infrastructure (OCI)の Windows Server に OpenSSH を設定して、MacBook から ssh接続してみてみます。
また、SSH接続時の既定の Windows コマンド シェル (cmd.exe) を PowerShell へ変更してみてみます。
■ 構成イメージ
事前に OCI へ Windows Server 2019 の Compute Server を設定し Remote Desktop アクセスできるようにしておきます
■ Remote Desktop アクセス
直接 Windows OS へアクセスできない場合、Remote Desktop等でアクセスします。
1) Microsoft Remote Desktop 起動
MacBook へ Microsoft Remote Desktopをインストールして、起動
2) Widows Server ログイン
Microsoft Remote Desktopから、Widows Server へログイン
Oracle Cloud Infrastructure (OCI)の場合 opcユーザーでログイン
■ PowerShell を使用して OpenSSH をインストール
● 管理者として PowerShell を実行
PowerShell を使用して OpenSSH をインストールするには、管理者として PowerShell を実行します。
PowerShellアイコンを右クリックし、[Run as administrator] をクリックして起動
● OpenSSH が使用可能になっていることを確認
OpenSSH が使用可能になっていることを確認するには、次のコマンドを実行します。
インストールされていない場合は、次の出力が返されます。
PS C:\> Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'
Name : OpenSSH.Client~~~~0.0.1.0
State : NotPresent
Name : OpenSSH.Server~~~~0.0.1.0
State : NotPresent
● サーバーまたはクライアント コンポーネントをインストール
1) Install the OpenSSH Client
次のコマンドで OpenSSH Client をインストール
PS C:\> Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
Path :
Online : True
RestartNeeded : False
2) Install the OpenSSH Server
次のコマンドで OpenSSH Server をインストール
PS C:\> Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
Path :
Online : True
RestartNeeded : False
3) OpenSSH Install 確認
PS C:\> 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
● OpenSSH Server の起動と構成
初めて使用するために OpenSSH Server を起動して構成するには、管理者として PowerShell を開き、次のコマンドを実行して sshd service を開始します。
1) Start the sshd service
エラーで起動できない場合は後述のGUIで起動します。
PS C:\> Start-Service sshd
Start-Service : Service 'OpenSSH SSH Server (sshd)' cannot be started due to the following error: Cannot start
service sshd on computer '.'.
At line:1 char:1
+ Start-Service sshd
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service],
ServiceCommandException
+ FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceCommand
2) OPTIONAL but recommended
PS C:\> Set-Service -Name sshd -StartupType 'Automatic'
3) Confirm the Firewall rule is configured. It should be created automatically by setup. Run the following to verify
PS C:\> if (!(Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue | Select-Object Name, Enabled)) {
>> Write-Output "Firewall Rule 'OpenSSH-Server-In-TCP' does not exist, creating it..."
>> New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
>> } else {
>> Write-Output "Firewall rule 'OpenSSH-Server-In-TCP' has been created and exists."
>> }
Firewall rule 'OpenSSH-Server-In-TCP' has been created and exists.
● GUIによる OpenSSH Server 起動設定
コマンドでOpenSSHサービスを起動できない場合、GUIで起動設定します。
1) Services起動
"Service"を検索して起動
2) Start設定
Services画面から [OpenSSH]を右クリックし、[Properties]をクリック
3) 自動起動設定と起動
[Start Type]を Automatic にして、[Start] をクリックして起動
● GUIによる Firewall 設定
sshを外部接続できるように Firewallを設定
1) WIndows Defender Firewall 起動
2) WIndows Defender Firewall画面
[Allow an App 〜]をクリック
3) Allow apps to communicate through Windows Defender Firewall画面
OpenSSH Serverへチェックし、[OK]をクリック
■ OpenSSH Server への接続
1) ssh インストール確認
sshコマンドが使えることを確認
PS C:\> ssh
usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-B bind_interface]
[-b bind_address] [-c cipher_spec] [-D [bind_address:]port]
[-E log_file] [-e escape_char] [-F configfile] [-I pkcs11]
[-i identity_file] [-J [user@]host[:port]] [-L address]
[-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
[-Q query_option] [-R address] [-S ctl_path] [-W host:port]
[-w local_tun[:remote_tun]] destination [command]
2) ssh接続 (Permission denied)
初期設定ではパスワード認証無効化されており、公開鍵認証のみ可能になっているためアクセスできません。
PS C:\> ssh opc@localhost
The authenticity of host 'localhost (::1)' can't be established.
ECDSA key fingerprint is SHA256:VuSnFnSDoFfKK1qngqhZTQu8rk0UUee8qmz61EAASR0.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.
opc@localhost: Permission denied (publickey,keyboard-interactive).
● OpenSSH Server 公開鍵認証設定
sshd_config ファイルから、パスワード認証無効化されており、公開鍵認証のみ可能であることを確認
・公開鍵認証有効: PubkeyAuthentication yes を設定
・パスワード認証無効: PasswordAuthentication no を設定
・設定確認
PS C:\Users\opc\.ssh> type C:\ProgramData\ssh\sshd_config
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.
#Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
#HostKey __PROGRAMDATA__/ssh/ssh_host_rsa_key
#HostKey __PROGRAMDATA__/ssh/ssh_host_dsa_key
#HostKey __PROGRAMDATA__/ssh/ssh_host_ecdsa_key
#HostKey __PROGRAMDATA__/ssh/ssh_host_ed25519_key
# Ciphers and keying
#RekeyLimit default none
# Logging
#SyslogFacility AUTH
#LogLevel INFO
# Authentication:
#LoginGraceTime 2m
#PermitRootLogin prohibit-password
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
PubkeyAuthentication yes
# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile .ssh/authorized_keys
#AuthorizedPrincipalsFile none
# For this to work you will also need host keys in %programData%/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes
# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication no
#PermitEmptyPasswords no
#AllowAgentForwarding yes
#AllowTcpForwarding yes
#GatewayPorts no
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#PermitUserEnvironment no
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS no
#PidFile /var/run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none
# no default banner path
#Banner none
# override default of no subsystems
Subsystem sftp sftp-server.exe
# Example of overriding settings on a per-user basis
#Match User anoncvs
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server
#Match Group administrators
#AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys
● authorized_keys 設定
ユーザー配下に $HOME.ssh\authorized_keys ファイルを作成
1) authorized_keysファイル作成
PS C:\Users\opc> mkdir $HOME\.ssh
PS C:\Users\opc> cd $HOME\.ssh
PS C:\Users\opc\.ssh> pwd
Path
----
C:\Users\opc\.ssh
PS C:\Users\opc\.ssh> New-Item -type file authorized_keys
Directory: C:\Users\opc\.ssh
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 10/15/2021 5:24 AM 0 authorized_keys
PS C:\Users\opc\.ssh> ls
Directory: C:\Users\opc\.ssh
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 10/15/2021 5:24 AM 0 authorized_keys
-a---- 10/15/2021 5:15 AM 172 known_hosts
2) authorized_keys ファイル公開鍵設定
authorized_keys ファイルへ SSH接続するための公開鍵を登録
公開鍵はOCI Computeインスタンスを作成したときに出力もしくは使用したものを使用
PS C:\Users\opc\.ssh> Set-Content -Path $HOME/.ssh/authorized_keys -Value "ssh-rsa.pubの公開鍵の接続文字列"
3) authorized_keys ファイル公開鍵設定確認
PS C:\Users\opc\.ssh> type .\authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDLYXNhFZYyzBFlYCXUMBGKLGuh+Km+2cufP/aWF4bN+raQ+bYpHXI65iH8WqAbPUBliE8BLUF8tjqMamaBGFZuOMY ssh-key-opc
■ ssh接続
自宅の MacBookから Windows Server へ SSH秘密鍵を使用して接続
秘密鍵はOCI Computeインスタンスを作成したときに出力もしくは使用したものを使用
1) ssh接続
mac-book:~ user$ ssh -i id_rsa opc@10.10.0.2
Microsoft Windows [Version 10.0.17763.2183]
(c) 2018 Microsoft Corporation. All rights reserved.
opc@BASTION C:\Users\opc>
2) 接続ホスト名確認
hostnameコマンドで、ホスト名を確認
opc@BASTION C:\Users\opc>hostname
bastion
■ SSH接続時の既定のシェルを PowerShell へ設定
SSH接続時の既定の Windows コマンド シェル (cmd.exe) を PowerShellへ変更してみます
● 設定コマンド
既定の ssh シェルの構成は、シェルの実行可能ファイルへの完全なパス Computer\HKEY_LOCAL_MACHINE\SOFTWARE\OpenSSH を、Windows レジストリの文字列値 DefaultShell に追加することで行われます。
例として、次の Powershell コマンドによって、既定のシェルが PowerShell.exe に設定されます。
PS C:\> 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
● CLI設定確認
Get-ItemPropertyコマンドで設定確認
PS C:\> Get-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH"
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
● GUI設定確認
Registry Editorで設定確認
● ssh接続確認
sshでWindowsへアクセスして、PowerShellになっていることを確認
-
ssh接続
mac-book:~ user$ ssh -i id_rsa opc@10.10.0.2
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.PS C:\Users\opc> hostname
bastion -
PowerShell Version確認
PS C:\Users\opc> Get-Host
Name : ConsoleHost Version : 5.1.17763.2183 InstanceId : cc4eebe4-0bc3-4d2c-82eb-ec38f4 UI : System.Management.Automation.Internal.Host.InternalHostUserInterface CurrentCulture : en-US CurrentUICulture : en-US PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy DebuggerEnabled : True IsRunspacePushed : False Runspace : System.Management.Automation.Runspaces.LocalRunspace
■ 参考
● Microsoft 技術ドキュメント(docs.microsoft.com)
・Windows での OpenSSH
・Windows での OpenSSH: OpenSSH をインストールする
・Windows 10 1809 および Server 2019 用 OpenSSH Server 構成
・Windows での OpenSSH 用の既定のシェルの構成
・PowerShell を知る
● Oracle Cloud Infrastructureドキュメント
・Windowsインスタンスへの接続