一般ユーザーで root ユーザー権限のコマンドの実行を許可する方法
Environment
- Red Hat Enterprise Linux (RHEL)
Issue
- 一般ユーザーで root ユーザー権限のコマンドの実行を許可する方法は?
Resolution
sudo コマンドを使用すると、Red Hat Enterprise Linux 上で他のユーザー権限でタスクを実行することができます。
sudo コマンドは su コマンドと異なり、より柔軟で安全です。一つの大きな利点としては、sudo コマンドの実行ログがデフォルトで /var/log/secure に記録されます。
sudo プログラムは、/etc/sudoers 設定ファイルを使用して、あるユーザー (グループ) による特権 (root) コマンドの許可/拒否のルールを設定します。このファイルは直接編集せず、sudo パッケージに含まれている visudo コマンドを使用して編集して下さい。
以下の例では、normaluser というユーザーによる特権コマンドの実行を許可します。最初に、sudo を使用して特権コマンドの実行を試します。
$ sudo /sbin/service sendmail restart
Password:
normaluser is not in the sudoers file.This incident will be reported.
sudo コマンドの実行結果が、/var/log/secure ファイルに記録されます。
# tail /var/log/secure
...
Aug 2 14:37:49 somehost sudo:normaluser : user NOT in sudoers ;
TTY=pts/2 ; PWD=/home/normaluser ; USER=root ;
COMMAND=/sbin/service sendmail restart
Red Hat Enterprise Linux には、特権処理に使用する特別なグループ wheel グループが存在します。
このグループに特権コマンドの実行を許可したいユーザーを追加します。特権コマンドの実行を許可したいユーザー (normaluser) を wheel グループに追加します。注意: このコマンドは root ユーザーで実行する必要があります。
# usermod -aG wheel normaluser
追加したユーザーが wheel グループのメンバーであることを確認します。
# groups normaluser
normaluser : normaluser wheel
visudo コマンドを使用して、/etc/sudoers ファイルを編集します。
# sudoers file.
#
# This file MUST be edited with the 'visudo' command as root.
#
# See the sudoers man page for the details on how to write a sudoers file.
#
# Host alias specification
# User alias specification
# Cmnd alias specification
# Defaults specification
# User privilege specification
root ALL=(ALL) ALL
# Uncomment to allow people in group wheel to run all commands
# %wheel ALL=(ALL) ALL
# Same thing without a password
# %wheel ALL=(ALL) NOPASSWD:ALL
# Samples
# %users ALL=/sbin/mount /cdrom,/sbin/umount /cdrom
# %users localhost=/sbin/shutdown -h now
/etc/sudoers ファイルには、設定例およびコメントが記載されています。wheel グループによる特権コマンドの実行を許可するには、以下の '%wheel' の行のコメントを外します。
...
# Uncomment to allow people in group wheel to run all commands
%wheel ALL=(ALL) ALL
...
visudo は vi エディタの機能を使用します。ファイルを編集するには、i キーを押して編集モードにします。カーソルキーを使用して、カーソルを行先頭の # に合わせ、Delete キーを押して # マークを削除します。
ESC キーを押し、編集モードから抜けます。編集モードから抜けたら、:x で変更を保存し、編集を終了します。
...
# Uncomment to allow people in group wheel to run all commands
%wheel ALL=(ALL) ALL
#Same thing without a password
# %wheel ALL=(ALL) NOPASSWD:ALL
# Samples
# %users ALL=/sbin/mount /cdrom,/sbin/umount /cdrom
# %users localhost=/sbin/shutdown -h now
# ALL ALL = NOPASSWD:/usr/bin/mindspring
一般ユーザーとして、特権コマンドを実行して下さい。
$ sudo /sbin/service sendmail restart
Password:
Shutting down sendmail: [ OK ]
Shutting down sm-client: [ OK ]
Starting sendmail: [ OK ]
Starting sm-client: [ OK ]
sudo コマンドの実行が成功した旨のログが、/var/log/secure ファイルに記録されます。
# tail /var/log/secure
...
Aug 2 15:05:49 somehost sudo:normaluser :TTY=pts/2 ;
PWD=/home/normaluser ; USER=root ;
COMMAND=/sbin/service sendmail restart
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.
Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.
