この記事について
sudoコマンドやその設定ファイルについて整理する。
一般ユーザーへ管理用コマンド実行権限を委譲する
通常、管理用コマンドは一般ユーザーでは実行することができない。
/etc/sudoers
を編集することで、sudoコマンドを用いた一般ユーザーでの管理用コマンドの実行が可能となる。
- rootユーザーで
visudo
コマンドを実行し、/etc/sudoers
の編集画面に入る。
[root@localhost ~]# visudo
- 以下の行を追加する。これにより、
sudo
コマンドを用いた、一般ユーザーでのsystemctl
コマンドの実行ができるようになる。
Kotaro ALL=NOPASSWD: /usr/bin/systemctl
NOPASSWD の指定により、パスワード入力をせずにコマンドの実行ができるようになる。
- 当該一般ユーザーで以下のコマンドを実行する。
パスワードの入力をせずに、sudo
コマンドを用いた管理用コマンドの実行ができた。
[Kotaro@localhost ~]$ sudo systemctl status chronyd
● chronyd.service - NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2025-04-14 10:14:03 JST; 2 days ago
Docs: man:chronyd(8)
man:chrony.conf(5)
Process: 861 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
Process: 839 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 846 (chronyd)
Tasks: 1 (limit: 23216)
Memory: 2.2M
CGroup: /system.slice/chronyd.service
mq846 /usr/sbin/chronyd
実行可能コマンドの確認
- 一般ユーザーで以下のコマンドを実行する。
[Kotaro@localhost ~]$ sudo -l
- 以下行より、当一般ユーザーが実行できる管理用コマンドを確認できる。
User Kotaro may run the following commands on localhost:
(root) NOPASSWD: /usr/bin/systemctl
以上。