0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Windows の ssh コマンドに秘密鍵を無視された際のシンプルな対処方法

Posted at

Windows 11 では、SSH 接続を行なう ssh コマンドを用いることができる。
この際、-i オプションを用いることで、接続に用いる秘密鍵ファイルを指定することができる。
しかし、このコマンドは、秘密鍵ファイルを指定してもいちゃもんをつけて無視してきやがることがある。
今回は、そんな困った仕様への対処法を紹介する。

事象の確認

まず、Tera Term で適当な鍵を生成する。
今回のデモでは、パスフレーズは空にしておく。

鍵生成画面

生成した鍵を保存して、公開鍵を接続先の authorized_keys に書き込む。
そして、ssh コマンドで接続を試みる。
すると……

接続を試みた結果

D:\temp>ssh -i id_ed25519 -p 22222 ubuntu@localhost
The authenticity of host '[localhost]:22222 ([::1]:22222)' can't be established.
ED25519 key fingerprint is SHA256:OA1A4YUgxPDH3wLqwgMUSY9DjmFX2Q8m4LP3yu9Z6Jw.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[localhost]:22222' (ED25519) to the list of known hosts.
Bad permissions. Try removing permissions for user: NT AUTHORITY\\Authenticated Users (S-1-5-11) on file D:/temp/id_ed25519.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions for 'id_ed25519' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "id_ed25519": bad permissions
ubuntu@localhost: Permission denied (publickey).

秘密鍵ファイルを指定したのに無視され、サーバーへの接続にも失敗してしまった。

対処方法

メッセージにそって permissions をいじることでも対処できるかもしれないが、今回は別の方法で回避を行う。
その方法とは

  1. ssh-keygen コマンドで、適当な鍵を生成する
  2. 1 で生成した秘密鍵ファイルに、用いる秘密鍵のデータを上書きする
  3. 1 で生成した公開鍵ファイルを削除する
  4. 2 で上書きした秘密鍵ファイルを指定し、SSH 接続を行う

である。

まず、接続用の秘密鍵ファイルを用意する。

対処方法の実行

D:\temp>ssh-keygen -f temp -C temp
Generating public/private ed25519 key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in temp
Your public key has been saved in temp.pub
The key fingerprint is:
SHA256:E76aJmEM74e5xestTD9PCPsMsSGnPy5aiDinHkqSkTY temp
The key's randomart image is:
+--[ED25519 256]--+
|                 |
|                 |
|        .        |
| ..    . .       |
|oE +. = S        |
|o+..==.* +       |
|*oooo**.o .      |
|++..*o*Oo.       |
|+....OBo+o.      |
+----[SHA256]-----+

D:\temp>type id_ed25519 > temp

D:\temp>del temp.pub

-f temp コマンドは、出力先のファイル名を指定する。
これを指定しない場合、実行時に入力を求められる。
-C temp コマンドは、公開鍵ファイルに書き込むコメントを指定する。
これを指定しない場合、自動で生成される。

今回のデモでは、これらを指定しないと実行結果にユーザー名が出てしまうため、指定した。
実際に対処を行う際は、指定しなくてもよい。

ここで生成した公開鍵ファイルを削除していない状態では、秘密鍵ファイルを上書きして元々のものと同じデータになった状態でも、なぜか SSH サーバへの接続に失敗した。
生成した公開鍵ファイルは忘れずに削除すること。

次に、この秘密鍵ファイルを指定して SSH 接続を行う。
すると、接続に成功した。

SSH接続に成功した様子

D:\temp>ssh -i temp -p 22222 ubuntu@localhost
Welcome to Ubuntu 24.04.3 LTS (GNU/Linux 6.14.0-1011-aws x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/pro

 System information as of Sun Oct 12 14:34:22 UTC 2025

  System load:  0.0               Temperature:           -273.1 C
  Usage of /:   31.1% of 6.71GB   Processes:             111
  Memory usage: 26%               Users logged in:       1
  Swap usage:   0%                IPv4 address for ens5: 172.31.33.113


Expanded Security Maintenance for Applications is not enabled.

9 updates can be applied immediately.
9 of these updates are standard security updates.
To see these additional updates run: apt list --upgradable

Enable ESM Apps to receive additional future security updates.
See https://ubuntu.com/esm or run: sudo pro status


*** System restart required ***
Last login: Sun Oct 12 14:27:42 2025 from 127.0.0.1
ubuntu@ip-172-31-33-113:~$

最初に秘密鍵を無視された際にホスト情報が記録されているため、ここでは新規ホストへの接続時の確認は出なかった。

まとめ

Windows 11 の ssh コマンドは、Tera Term など他の SSH クライアントでは普通に使用できる秘密鍵ファイルでも、指定しても無視することがある。
ssh-keygen コマンドを用いることで、ssh コマンドが気に入る条件の秘密鍵ファイルができるので、これに用いる秘密鍵のデータを上書きすることで、ssh コマンドに無視されない秘密鍵ファイルを作ることができる。
ただし、このとき一緒に作られる公開鍵ファイルを削除しておかないと、なぜかサーバーへの接続に失敗した。

この ssh コマンドには、ほかにもたとえば「サーバーに接続すると、勝手に known hosts のリストに追加される」(Amazon EC2 で使い捨てるインスタンスに接続する際など、記録しても無駄なこともあるのに) という欠点もある。
使いやすい他の SSH クライアントが使える環境では、そっちを使うのがいいだろう。

ちなみに、known hosts のリストへの追加は、-o UserKnownHostsFile=NUL オプション (known hosts のリストとして情報を捨てる用の特殊ファイルを指定する) で回避できる。

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?