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?

rootアカウントのセキュリティ

Last updated at Posted at 2025-05-22

rootアカウントの推奨される使用方法

  1. rootを管理者個人のアカウントとして使用しない。(常用しない)
  2. 運用時はrootで直接ログインせず、一般ユーザーでログインし作業をする。
  3. root権限が必要な時はsuで権限昇格し作業を行う。(suコマンドについても権限を厳しく行う)
    1. 3よりもセキュリティレベルを向上させる手段として、suを全面的に禁止し、sudo rootを特定のユーザーのみ行えるようにする。

今回はrootアカウントに対して様々な制限を行っていきます

ローカルログイン制限

/etc/pam.d/loginファイルに対してpam_securetty.soを設定
以下の行を追加する

/etc/pam.d/login
auth     required     pam_securetty.so

pam_securetty,so/etc/securettyを読み込み、ファイル内に書かれている端末(例:tty1)からのrootログインを許可する

SSHログイン制限

一般的にリモートログイン用に、OpenSSH(sshd)がインストールされており、デフォルトではrootログインが許可されていますが、サーバ運用時にはセキュリティ的に禁止にすべきです。

/etc/ssh/sshd_config
ParmitRootLogin=yes
↓
PermitRootLogin=no

設定変更後はsshdを再起動します

bash
$ sudo systemctl restart sshd

suコマンドの制限

今回はLinuxデフォルトのwheelグループを使用して制限していきます。
1 wheelグループ以外はsuコマンド使用不可にする

/etc/pam.d/su
#auth     ewquired     pam_wheel.so use_uid
↓ #を外す
auth     ewquired     pam_wheel.so use_uid

2 suコマンドを使用させるユーザをwheelグループに追加する

bash
usermod -aG wheel user01

suコマンドのログは以下で確認可能

bash
$ journalctl /usr/bin/su
又は、
$ grep "su" /var/log/secure

sudoコマンドの制限

sudoコマンドはsu代替として使用できコマンドです。不適切な権限の不利かたをするととても危険な状態に陥ります
susudoの違い】

su sudo
設定の範囲 ユーザ単位 ユーザ単位、コマンド単位
入力するパスワード rootのパスワード ユーザのパスワード

0 設定ファイル
sudoコマンドの全体の設定ファイルは/etc/sudoersです。
書き込む際の書式は

/etc/sudoers
ユーザー名 ホスト名=(実行設定ユーザ名) コマンド

各項目の書式

項目 概要 書式
ユーザ名 コマンドの実行を許可するユーザ、グループの指定 user01,%gp01,#uid,ALL
ホスト名 実行を許可するホスト host.com,8.8.8.8,8.8.8.0/24,ALL
実行設定ユーザ名 コマンド実行時のユーザ設定を指定 user01,%gp01,#uid,ALL
コマンド 実行を許可するコマンドの指定 /usr/bin/passwd

1 デフォルトの設定
デフォルトの設定として、以下のようにwheelグループはすべての権限が与えられている。

/etc/sudoers
%wheel    ALL=(ALL)   ALL

2 個別設定
ここでは例としてuser01sudoコマンドは使えるがsudo passwd rootは使えないように設定する。

bash
$ echo 'user01 ALL=(ALL) NOPASSWD:ALL,!/usr/bin/passwd root' >> /etc/sudoers/user01
$ chmod 440 /etc/sudoers.d/user01 

このコマンドで設定することができる。
/etc/sudoersに直接書き込むコマンドとして、visudoがあるが筆者的におおもとの設定ファイルを触るのは怖かったので、.dの補助ファイルを使用した。
設定を消すときは、/etc/sudoers.d/user01を消すだけなのでとても楽。

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?