一般ユーザーでroot権限のコマンドを実行するには


図解60p「Linuxサーバー構築入門マニュアル」無料
登録10秒/自動返信でDL/合わなければ解除3秒
HOMELinux技術 リナックスマスター.JP(Linuxマスター.JP)LinuxtipsLinuxtips, ユーザー管理 > 一般ユーザーでroot権限のコマンドを実行するには

あるコマンドを実行するときにのみ、root権限のコマンドを使用したいという場合があります。そのようなときには、sudoコマンドを使うと便利です。

sudoコマンドの引数に実行したいコマンドを指定して実行することで、rootコマンドを実行することができます。しかし、sudoコマンドを実行する際には、あらかじめ/etc/sudoersファイルに実行を許可したいユーザを設定しておく必要があります。

この/etc/sudoersファイルに記述するユーザーは信頼のあるユーザーを設定する必要があります。

また、sudoコマンドを実行したログはsyslogデーモンを経由して、ログファイルに記述されるので、セキュリティ管理上のメリットがあります。誰がいつどんなコマンドをsudoコマンドを使用したのかを詳しく知ることができます。

sudo -u <ユーザー名> <実行するコマンド>


一般ユーザーで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)



無料プレゼント
図解60p「Linuxサーバー構築入門マニュアル」
独学で詰まる前に、“型(手順書)”で最初の環境構築をサクッと終わらせましょう。
登録10秒/自動返信でDL/合わなければ解除3秒
無料で受け取る ※メールアドレスだけでもOK(必須項目は最小限)

宮崎 智広

この記事を書いた人

宮崎 智広(みやざき ともひろ)

株式会社イーネットマーキュリー代表。現役のLinuxサーバー管理者として15年以上の実務経験を持ち、これまでに累計3,100名以上のエンジニアを指導してきたLinux教育のプロフェッショナル。「現場で本当に使える技術」を体系的に伝えることをモットーに、実践型のLinuxセミナーの開催や無料マニュアルの配布を通じてLinux人材の育成に取り組んでいる。

<<関連記事>>
・グループパスワードを削除する
・グループからメンバーを外す
・グループにメンバーを追加する
・グループに管理者を設定する
・グループにパスワードを設定する

図解60pのLinux無料マニュアル
登録10秒/自動返信でDL
無料で受け取る