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?

SSHで、rootユーザーのログインを禁止する方法

Last updated at Posted at 2025-04-10

テーマは、表題の通りrootユーザーのアクセスを禁止する方法です。

そもそも、なぜrootユーザーで、アクセスをできるのがいけないのかですが、これは、とてもシンプルな理由で、rootユーザーは、

とても強大な権限を有しているため、ログインが可能であると、サーバーにあらゆる操作ができてしまい、乗っ取られてしまうからです。

では、そのようにできないようにするために、どのようにすればいいのかですが、それは、以下の手順を踏んでください。

1、rootユーザー以外のユーザーを新規作成

この手順は、この記事で触れていますので、こちらを参照ください。

2、1で作成したユーザーで、念のために、SSH接続できることを確認

これは、以下のコマンドで可能です。実行後にパスワードを求められるので、入力してください。

ssh 新規作成のユーザー名@ipアドレス

3、cpコマンドを使って以下のパスのファイルのバックアップを取る。(これも、念のためです。今回の件に限らず設定ファイルはバックアップを取った方がいいです。)

cp /etc/ssh/sshd_config /etc/ssh/sshd_config_org

4、sshd_configファイルをvim(エディターなら何でもよい)で、PermitRootLoginがnoになっているのをyesに変更する。

5、diffコマンドを使って、3のcpコマンドで作成した、コピーと比較する。

以下のコマンドで、できます。

diff sshd_config sshd_config_org

6、sshプロセスを再起動する

以下のコマンドで、できます。

systemctl restart sshd

7、rootユーザーで、SSH接続できないことを確認する。

Permision denied, Please try againと表示されれば、OKです。

これらの手順がrootユーザーログイン禁止の方法になります。

今日は、ここまで。

0
0
0

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?