鍵ペアを作成したいユーザーの作成
今回は管理者権限を持ったローカルユーザーを作成します
[サーバーマネージャー] → [ツール] → [コンピューターの管理] → [ローカルユーザーとグループ] → [ユーザー] を右クリックして [新しいユーザー] を選択

ユーザー名(今回はhuong)とパスワードを入力して [作成] をクリック

ユーザーリストに表示されます。

作成したアカウントを選択して右クリックし [プロパティ] を開く





作成したユーザーが登録されていることを確認後、[OK]をクリック

SSH クライアント用の秘密鍵と SSH サーバー用の公開鍵の鍵ペアを作成
デフォルト設定では [Administrators] グループのみ、公開鍵 (authorized_keys) の場所が OpenSSH デフォルトの [.ssh\authorized_keys] ではなく、 [/ssh/administrators_authorized_keys]に設定されている。事前に、管理者権限で設定ファイルの最終 2行をコメントにしてサービスを再起動しておく必要がある。
設定ファイル [C:\ProgramData\ssh\sshd_config]をメモ帳等で開く
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# 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 yes #PermitEmptyPasswords no # GSSAPI options #GSSAPIAuthentication 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 |
SSH 鍵ペアを作成するユーザーでログオン(今回は上記で作成したhuong)
Windows PowerShellを起動し[ssh-keygen]と入力する。
3行目は[Enter]を押すとホームフォルダーに[.ssh]フォルダーが作成される。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
PS C:\Users\huong> ssh-keygen Generating public/private ed25519 key pair. Enter file in which to save the key (C:\Users\huong/.ssh/id_ed25519): Created directory 'C:\\Users\\huong/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in C:\Users\huong/.ssh/id_ed25519 Your public key has been saved in C:\Users\huong/.ssh/id_ed25519.pub The key fingerprint is: SHA256:QawzAcpR+Mdo9uIiHYpbDVm+Q6r0suntcL9Tq3qdNLw huong@WIN-SERVER The key's randomart image is: +--[ED25519 256]--+ | .oo. .. | | ..o ... | | o..o o. | | += * . | | oooo.oS | | .=...= | |.=o++.+ = | |=+O o+ E | |+*+*o++ | +----[SHA256]-----+ PS C:\Users\huong> |
1 2 3 4 5 6 7 8 9 10 11 12 |
PS C:\Users\huong> cd .ssh PS C:\Users\huong\.ssh> mv id_ed25519.pub authorized_keys PS C:\Users\huong\.ssh> ls ディレクトリ: C:\Users\huong\.ssh Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 2025/01/19 13:11 99 authorized_keys -a---- 2025/01/19 13:11 464 id_ed25519 |
公開鍵 [authorized_keys] のパーミションを変更
[authorized_keys] の現状のパーミション確認すると下記のとおりEveryone に読み取り権限がある
1 2 3 4 5 6 7 |
PS C:\Users\huong\.ssh> icacls authorized_keys authorized_keys BUILTIN\Administrators:(F) NT AUTHORITY\SYSTEM:(F) WIN-SERVER\huong:(M) Everyone:(RX) 1 個のファイルが正常に処理されました。0 個のファイルを処理できませんでした |
Everyone の読み取り権限を削除する
1 2 3 4 5 6 7 8 9 |
PS C:\Users\huong\.ssh> icacls authorized_keys /remove Everyone 処理ファイル: authorized_keys 1 個のファイルが正常に処理されました。0 個のファイルを処理できませんでした PS C:\Users\huong\.ssh> icacls authorized_keys authorized_keys BUILTIN\Administrators:(F) NT AUTHORITY\SYSTEM:(F) WIN-SERVER\huong:(M) 1 個のファイルが正常に処理されました。0 個のファイルを処理できませんでした |
Everyoneが削除されている
SSH クライアントホスト(Windows 11)のログオンユーザー直下の [.ssh] フォルダーに SSH サーバー側から秘密鍵を転送する
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
C:\Users\mhori>cd .ssh C:\Users\mhori\.ssh> sftp huong@192.168.11.83 huong@192.168.11.83's password: Connected to 192.168.11.83. sftp> cd .ssh sftp> get id_ed25519 Fetching /C:/Users/huong/.ssh/id_ed25519 to id_ed25519 id_ed25519 100% 464 226.6KB/s 00:00 sftp> exit C:\Users\mhori\.ssh>dir ドライブ C のボリューム ラベルは WIN11 です ボリューム シリアル番号は 541B-F233 です C:\Users\mhori\.ssh のディレクトリ 2025/01/19 13:19 <DIR> . 2025/01/11 19:03 <DIR> .. 2025/01/19 13:19 464 id_ed25519 2025/01/19 10:38 842 known_hosts 2025/01/19 10:38 98 known_hosts.old |
SSH クライアントホスト(Windows 11)からSSH接続してみる(パスワード入力不要でログインできる)
ただし、鍵の作成時に設定したパスワードの入力は求められる
1 2 3 4 5 6 7 8 |
(c) Microsoft Corporation. All rights reserved. C:\Users\mhori> ssh huong@192.168.11.83 Enter passphrase for key 'C:\Users\mhori/.ssh/id_ed25519': Microsoft Windows [Version 10.0.20348.3091] (c) Microsoft Corporation. All rights reserved. huong@WIN-SERVER C:\Users\huong> |