概要
PasswordAuthentication no
を設定してsudo systemctl restart sshd
を実行しているのにパスワードの入力が求められてしまう。
環境
- raspberryPi上でUbuntuserverを動かしている
- Ubuntu 25.04
- OpenSSH_9.9p1 Ubuntu-3ubuntu3.1, OpenSSL 3.4.1 11 Feb 2025
結論
/etc/ssh/sshd_config.d/50-cloud-init.conf
の中に
PasswordAuthentication yes
という文字列が残っていた
PasswordAuthentication no
にすることで解決された。
hoge@hoge.hoge.hoge.hoge: Permission denied (publickey).
と表示され想定通りの環境になった
解説
原因:Includeによる設定の上書き
/etc/ssh/sshd_config ファイルの中にこのような行がありました
Include /etc/ssh/sshd_config.d/*.conf
これは、「この位置で /etc/ssh/sshd_config.d/ フォルダ内の すべての .conf ファイル を読み込んで設定に適用する」という意味があるらしいです。
わたしの場合では
sshd_config の下部にこう書いていた:
PasswordAuthentication no
しかしファイルの冒頭に
Include /etc/ssh/sshd_config.d/*.conf
があった。
sshd_config.d/50-cloud-init.conf
(場合よってファイル名が違うっぽい)にこう書かれていた:
PasswordAuthentication yes
この結果、「後から読み込まれた sshd_config.d/*.conf
が PasswordAuthentication yes
を有効化し、設定全体に影響した」ということらしい
訂正
ssh_config/sshd_config
では同じ設定項目が複数あった場合、先にある方が適用され、後ろの方は無視される。
今回の場合、sshd_configのInclude行でsshd_config.d/50-cloud-init.conf
が読み込まれてPasswordAuthentication yes
が設定され、sshd_configの中で後ろにあるPasswordAuthentication no
が無視される事になる。
なので、sshd_configのInclude行より前にPasswordAuthentication no
を書いてあげるとsshd_config.d/50-cloud-init.conf
を書き換えずにパスワード認証を無効化できる。