ダッシュで奪取

ゲーム、読書、人生

PermitRootLogin noに変更してもrootユーザーでログインできてしまう

環境
$ cat /etc/redhat-release
CentOS Stream release 9

$ ssh -V
OpenSSH_8.7p1, OpenSSL 3.0.7 1 Nov 2022

やりたいこと

root ユーザーでログインできないようにしたい

やったこと

$ sudo vim /etc/ssh/sshd_config
/etc/ssh/sshd_config

初期状態だとコメントアウトされていたので、有効にしてから no へ書き換え

- #PermitRootLogin prohibit-password
+ PermitRootLogin no

変更した設定を読み込むため、サービスを再起動

$ sudo systemctl restart sshd

自分のローカル PC から、root ユーザーでログインできるか試す → ログインできてしまった……!

$ ssh root@xxx.xxx.xxx.xxx

解決方法

/etc/ssh/sshd_config.d 配下にある設定が優先されるらしい ※この中で PermitRootLogin yes に上書きされていたようだ

デフォルトで存在しているファイルは変更しない方がよいと/etc/ssh/sshd_config 内に記載されていた(見てなかった)

# To modify the system-wide sshd configuration, create a  *.conf  file under
#  /etc/ssh/sshd_config.d/  which will be automatically included below
Include /etc/ssh/sshd_config.d/*.conf

システム全体の sshd 設定を変更するには、以下の場所に *.conf ファイルを作成します。 /etc/ssh/sshd_config.d/

ので、sshd_config を一旦戻す

$ sudo vim /etc/ssh/sshd_config
/etc/ssh/sshd_config

元に戻す

- PermitRootLogin no
+ #PermitRootLogin prohibit-password

01-permitrootlogin.conf というファイルが存在しており、こちらで設定しているようだったので編集(なければ自分で作成する)

$ sudo vim /etc/ssh/sshd_config.d/01-permitrootlogin.conf
/etc/ssh/sshd_config.d/01-permitrootlogin.conf
- PermitRootLogin yes
+ PermitRootLogin no

再起動

$ sudo systemctl restart sshd

もう一度ローカル PC から、root ユーザーでログインを試みる → ログインできなくなった OK

$ ssh root@xxx.xxx.xxx.xxx
root@xxx.xxx.xxx.xxx's password: 
Permission denied, please try again.

おわりに

参考URL