ssh で接続するとき、鍵は変更していないのにPermission denied (publickey) というエラーがでることがあります。
ここではOpenSSH のバージョンが新しくなったことが原因の場合の解決法を説明します。
今回扱う発生条件#
SSH 接続エラーはいろんな原因で発生しますが、今回は OpenSSHのバージョンを新しく更新したときの解決法を説明します。
具体的にはバージョン8.8 以降に新しくした場合について説明します。
これはたとえば Ubuntu を Ubuntu18.04(OpenSSH 7.6p1)から Ubuntu20.04(OpenSSH 8.9p1)にバージョンアップした場合に起こります。
コマンド例#
今回は例として git clone を ssh 接続した場合を例に説明します。
例えば下記のようにエラーが発生したとします。ユーザー名やサーバは架空のものです。
Permission denied (publickey).といわれていますね。
1
2
3
4
5
6
7
| $ git clone ssh://shinya@hogehoge.com/sample.git
Cloning into 'sample'...
shinya@sample.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
|
ssh のデバッグ情報を出力#
.ssh ディレクトリ以下の鍵ファイルのパーミッションや .ssh/config におかしなところがないかを確認して、問題がなさそうなら ssh をデバッグ情報つきで実行します。
-vT オプションでOKでしょう。すると下記のようにたくさんの情報が出力されます。
1
2
3
4
5
6
7
8
9
10
11
12
13
| $ ssh -vT shinya@hogehoge.com
OpenSSH_8.9p1 Ubuntu-3ubuntu0.1, OpenSSL 3.0.2 15 Mar 2022
debug1: Reading configuration data /home/shinya/.ssh/config
debug1: /home/shinya/.ssh/config line 1: Applying options for hogehoge.com
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
略
debug1: send_pubkey_test: no mutual signature algorithm
debug1: No more authentication methods to try.
shinya@hogehoge.com: Permission denied (publickey).
|
この最後の方にエラーの原因が書いてあるはずです。今回の例では debug1: send_pubkey_test: no mutual signature algorithm と怒られています。
なにやらアルゴリズムがだめそうです。
エラーの原因#
ssh-keygen で鍵を作成する場合、デフォルトで RSA 暗号化形式で作成されてます。
しかしこの暗号アルゴリズム RSA が脆弱性のため非推奨になったため、
OpenSSH 8.8 以降から RSA を受け付けないようになっているようです。
このため OpenSSH のバージョンが新しくなると上述のエラーが発生してしまいます。
もしつかっている鍵のアルゴリズムがよくわからなければ下記のようにssh-keygen で確認できます。
1
2
| $ ssh-keygen -l -f ~/.ssh/hoge_key.pub
2048 SHA256:8woLDZ0ab+nyIR4jvoGiHb+LEtaQw2/uwIf5zhz1BVw shinya@sample.com (RSA)
|
回避方法#
正しい解決方法はRSAでなくもっと強度の強い暗号アルゴリズムで鍵を再作成することです。しかしいろんな事情からそれができない場合もあるかと思います。
そこでひとまずRSA のままでSSHを動かすワークアラウンドの方法を書いておきます。
.ssh/config の修正#
.ssh/config に RSA を使用するように HostKeyAlgorithms と PubkeyAcceptedKeyTypes を下記のように追記します。
これで hogehoge.com に接続するときは RSA が使えるようになります。
1
2
3
4
| $ vim .ssh/config
Host hogehoge.com
HostKeyAlgorithms +ssh-rsa
PubkeyAcceptedKeyTypes +ssh-rsa
|
複数のサーバに対して
RSAを使えるようにした場合は、同様に上記2つの設定を追加してください。
接続確認#
再度 ssh を実行して接続できればOKです。
もし下記のようなメッセージが表示された場合はもうちょっと作業が必要です。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| $ ssh -vT shinya@hogehoge
debug1: Reading configuration data /home/shinya/.ssh/config
debug1: /home/shinya/.ssh/config line 1: Applying options for hogehoge.com
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
略
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
SHA256:h4p/Jl86nBeejcE4WsGPleemG95dIzJigiIlcgQhyyM.
Please contact your system administrator.
Add correct host key in /home/shinya/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /home/shinya/.ssh/known_hosts:5
remove with:
ssh-keygen -f "/home/shinya/.ssh/known_hosts" -R "shinya@hogehoge.com"
Host key for hogehoge.com has changed and you have requested strict checking.
Host key verification failed.
|
デバッグ情報を見てみると .ssh/known_hosts
がなにやらまずいようです。この場合は
Offending ECDSA key in /home/shinya/.ssh/known_hosts:5 とあるので、.ssh/known_hosts の5行目で、RSA でなく ECDSA アルゴリズムを指定してしまっているようです。
いまは RSA で接続したいので、5行目を削除してしまいましょう。
メッセージにある通り下記のコマンドで削除できます。
1
| ssh-keygen -f "/home/shinya/.ssh/known_hosts" -R "shinya@hogehoge.com"
|
もう一度 ssh を実行してみてください。
known_hosts から削除したので途中で Are you sure you want to continue connecting と尋ねられますので yes と答えておきます。
あとは最後に Exit Status 0 と表示されれば接続成功です。 お疲れ様でした。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| $ ssh -vT shinya@hogehoge
OpenSSH_8.9p1 Ubuntu-3ubuntu0.1, OpenSSL 3.0.2 15 Mar 2022
debug1: Reading configuration data /home/shinya/.ssh/config
debug1: /home/shinya/.ssh/config line 1: Applying options for hogehoge.com
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
略
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'hogehoge.com' (RSA) to the list of known hosts.
略
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 2472, received 66728 bytes, in 1.0 seconds
Bytes per second: sent 2599.2, received 70161.2
debug1: Exit status 0
|
正攻法#
上記の回避方法はあくまでワークアラウンドです。強度の弱いRSAを使い続けることはセキュリティ的に好ましくありません。
もっと強度の強い暗号で鍵を再作成するのが正しい対応です。サーバへの公開鍵の登録などはやり直しになりますが。
どの暗号アルゴリズムを使うかですが、 いま時点では Ed25519 を使うのが良いようです。これなら当分は大丈夫でしょう。
OpenSSH 8.8 リリースノート#
参考までに OpenSSH 8.8 Release Note のリンクを貼っっておきます。
「Potentially-incompatible changes」というところが今回のRSAについてです。
まとめ#
今回は OpenSSH をバージョンアップした際におこるエラーの対策について説明しまいた。
ワークアラウンドでひとまず逃げる方法も書いておきました。正しくは強度の強い暗号アルゴリズムで鍵を再作成することです。ご参考になれば。