あるコマンドを実行するときにのみ、root権限のコマンドを使用したいという場合があります。そのようなときには、sudoコマンドを使うと便利です。
sudoコマンドの引数に実行したいコマンドを指定して実行することで、rootコマンドを実行することができます。しかし、sudoコマンドを実行する際には、あらかじめ/etc/sudoersファイルに実行を許可したいユーザを設定しておく必要があります。
この/etc/sudoersファイルに記述するユーザーは信頼のあるユーザーを設定する必要があります。
また、sudoコマンドを実行したログはsyslogデーモンを経由して、ログファイルに記述されるので、セキュリティ管理上のメリットがあります。誰がいつどんなコマンドをsudoコマンドを使用したのかを詳しく知ることができます。
sudo -u <ユーザー名> <実行するコマンド>
あなたは、Linuxでこんな間違いしていませんか?
👉今すぐ詳細を見る
一般ユーザーでroot権限のコマンドを実行するには
[pakira@tiger pakira]$ su -
Password:
[root@tiger root]# 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
# Runas alias specification
# User privilege specification
root ALL=(ALL) ALL
pakira 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
------------------------------------------------------------
:wqで保存終了します。
sudoコマンドを実行してみる
[pakira@tiger pakira]$ sudo su - ←一般ユーザーでsuコマンドを実行します。
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.
Password: ←一般ユーザーのパスワードを入力します。
[root@tiger root]# ←root権限になります。
ログを確認する
[root@tiger root]# tail /var/log/messages
Oct 4 10:53:45 tiger iptables: Flushing all current rules and user defined chains: succeeded
Oct 4 10:53:45 tiger iptables: Clearing all current rules and user defined chains: succeeded
Oct 4 10:53:45 tiger iptables: Flushing all current rules and user defined chains: succeeded
Oct 4 10:53:45 tiger iptables: Clearing all current rules and user defined chains: succeeded
Oct 4 10:53:45 tiger iptables: Applying iptables firewall rules succeeded
Oct 4 10:55:23 tiger sudo: pakira : user NOT in sudoers ; TTY=pts/0 ; PWD=/home/pakira ; USER=pakira ; COMMAND=adduser
Oct 4 11:01:36 tiger su(pam_unix)[1335]: session opened for user root by pakira(uid=500)
Oct 4 11:06:40 tiger su(pam_unix)[1335]: session closed for user root
Oct 4 11:06:48 tiger sudo: pakira : TTY=pts/0 ; PWD=/home/pakira ; USER=root ; COMMAND=/bin/su -
Oct 4 11:06:48 tiger su(pam_unix)[1369]: session opened for user root by pakira(uid=0)
P.S
あなたは、Linuxでこんな間違いしていませんか?
👉今すぐ詳細を見る
<<関連記事>>
・グループパスワードを削除する
・グループからメンバーを外す
・グループにメンバーを追加する
・グループに管理者を設定する
・グループにパスワードを設定する