LoginSignup
29
36

More than 1 year has passed since last update.

一般ユーザでもroot権限が必要なコマンドを実行できるようにする方法

Last updated at Posted at 2018-04-14

#1 環境
VMware Workstation 14 Playerでゲストマシンを作成しました。
ゲストマシンのOSは以下のとおりです。

[root@centos74 ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)

[root@centos74 ~]# uname -r
3.10.0-693.el7.x86_64

#2 事前準備

##2.1 ユーザ(test1)の追加

root権限でユーザ(test1)を追加する。
[root@centos74 ~]# useradd test1
[root@centos74 ~]# passwd test1
ユーザー test1 のパスワードを変更。
新しいパスワード:
よくないパスワード: このパスワードは 8 未満の文字列です。
新しいパスワードを再入力してください:
passwd: すべての認証トークンが正しく更新できました。

#3 sudoersファイルの書式

sudoersファイル書式(その1)
ユーザ名 ホスト名=(実行ユーザ名) コマンド1,コマンド2,...
sudoersファイル書式(その2)
%グループ名 ホスト名=(実行ユーザ名) コマンド1,コマンド2,...

下記定義は、次のように解釈します。
ユーザtest1は、全てのホスト(ALL)で、root権限で、catコマンドを実行できます。

[root@server ~]# cat /etc/sudoers.d/test1
test1 ALL=(root) /usr/bin/cat

#4 特定ユーザに全てのコマンドの実行を許可する方法
ここでは、test1ユーザに対して、全てのコマンドの実行を許可してみます。

sudoersの設定
自分自身を確認する。rootユーザであることがわかる。
[root@centos74 ~]# id
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

[root@centos74 ~]# visudo -f /etc/sudoers.d/test
test1 ALL=(ALL) ALL

[root@centos74 ~]# cat /etc/sudoers.d/test
test1 ALL=(ALL) ALL
tcpdumpの実行結果(実行できる)
[root@centos74 ~]# su - test1
最終ログイン: 2018/04/14 (土) 19:10:53 JST日時 pts/0

自分自身を確認する。test1ユーザであることがわかる。
[test1@centos74 ~]$ id
uid=1001(test1) gid=1001(test1) groups=1001(test1) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

[test1@centos74 ~]$ sudo tcpdump -i ens33 icmp
[sudo] test1 のパスワード:
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes

#5 パスワード入力を要求しないようにする方法(NOPASSWD)
ここでは、test1ユーザに対して、全てのコマンドの実行を許可してみます。
さらに、test1がコマンドを実行する際、パスワードの入力を要求しないようにしてみます。

sudoersの設定
[root@centos74 ~]# visudo -f /etc/sudoers.d/test
test1 ALL=(ALL) NOPASSWD:ALL

[root@centos74 ~]# cat /etc/sudoers.d/test
test1 ALL=(ALL) NOPASSWD:ALL
tcpdumpの実行結果(実行できる)
[root@centos74 ~]# su - test1
最終ログイン: 2018/04/14 (土) 19:19:07 JST日時 pts/0

[test1@centos74 ~]$ id
uid=1001(test1) gid=1001(test1) groups=1001(test1) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

[test1@centos74 ~]$ sudo tcpdump -i ens33 icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes

#6 特定ユーザに特定コマンドの実行を許可する方法

##6.1 単一コマンドの実行を許可する方法
ここでは、test1ユーザに対して、tcpdumpの実行を許可してみます。

sudoersの設定
[root@centos74 ~]# visudo -f /etc/sudoers.d/test
test1 ALL=(ALL) NOPASSWD:/usr/sbin/tcpdump

[root@centos74 ~]# cat /etc/sudoers.d/test
test1 ALL=(ALL) NOPASSWD:/usr/sbin/tcpdump
tcpdumpの実行結果(実行できる)
[root@centos74 ~]# su - test1
最終ログイン: 2018/04/14 (土) 19:21:55 JST日時 pts/0

[test1@centos74 ~]$ id
uid=1001(test1) gid=1001(test1) groups=1001(test1) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

[test1@centos74 ~]$ sudo tcpdump -i ens33 icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
useraddの実行結果(実行できない)
[test1@centos74 ~]$ sudo useradd test2
[sudo] test1 のパスワード:
ユーザー test1 は'/sbin/useradd test2' を root として centos74 上で実行することは許可されていません。すみません。

##6.2 複数コマンドの実行を許可する方法
ここでは、test1ユーザに対して、tcpdump,useraddの実行を許可してみます。

sudoersの設定
[root@centos74 ~]# id
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

[root@centos74 ~]# visudo -f /etc/sudoers.d/test
test1 ALL=(ALL) NOPASSWD:/usr/sbin/tcpdump,/usr/sbin/useradd

[root@centos74 ~]# cat /etc/sudoers.d/test
test1 ALL=(ALL) NOPASSWD:/usr/sbin/tcpdump,/usr/sbin/useradd
tcpdumpの実行結果(実行できる)
[root@centos74 ~]# su - test1
最終ログイン: 2018/04/15 (日) 08:47:35 JST日時 pts/0

[test1@centos74 ~]$ sudo tcpdump -i ens33 icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
useraddの実行結果(実行できる)
[test1@centos74 ~]$ sudo useradd test2
[test1@centos74 ~]$ id test2
uid=1002(test2) gid=1002(test2) groups=1002(test2)

#7 グループに対してコマンドの実行を許可する方法
ユーザtest1,test2を作成します。
それぞれがtestというプライマリグループに所属するようにします。
そして、testグループに対してtcpdumpの実行を許可してみます。

##7.1 事前準備(ユーザ、グループの作成)

[root@centos74 ~]# groupadd test
[root@centos74 ~]# useradd test1
[root@centos74 ~]# useradd test2

test1のプライマリグループをtestに変更する。
[root@centos74 ~]# usermod -g test test1

test2のプライマリグループをtestに変更する。
[root@centos74 ~]# usermod -g test test2

test1のプライマリグループ(gid)を確認する。
[root@centos74 ~]# id test1
uid=1001(test1) gid=1001(test) groups=1001(test)

test2のプライマリグループ(gid)を確認する。
[root@centos74 ~]# id test2
uid=1002(test2) gid=1001(test) groups=1001(test)

##7.2 実行結果
グループを定義する場合、グループ名の前に%を付けます。

sudoersの編集
[root@centos74 ~]# visudo -f /etc/sudoers.d/test
%test ALL=(ALL) NOPASSWD:/usr/sbin/tcpdump

[root@centos74 ~]# cat /etc/sudoers.d/test
%test ALL=(ALL) NOPASSWD:/usr/sbin/tcpdump
test1ユーザの実行結果(実行できる)
[root@centos74 ~]# su - test1
[test1@centos74 ~]$ sudo tcpdump -i ens33 icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
test2ユーザの実行結果(実行できる)
[root@centos74 ~]# su - test2
[test2@centos74 ~]$ sudo tcpdump -i ens33 icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes

##7.3 あと始末
作成したユーザ、グループを削除します。

[root@centos74 ~]# groupdel test1
[root@centos74 ~]# groupdel test2
[root@centos74 ~]# userdel -r test1
[root@centos74 ~]# userdel -r test2
[root@centos74 ~]# groupdel test

#8 エイリアス
エイリアスは、定義ファイルを簡潔に記述するためのものです。
/etc/sudoersに登録されていて、ホスト、ユーザ、コマンド用のものがあります。
それぞれ、Host_Alias,User_Alias,Cmnd_Aliasを用いて定義します。
また、エイリアスを自身で定義することもできます。

/etc/sudoersより抜粋したエイリアス
左端の'#'はコメント行を表します。
# Host_Alias     FILESERVERS = fs1, fs2
# User_Alias ADMINS = jsmith, mikem
# Cmnd_Alias SOFTWARE = /bin/rpm, /usr/bin/up2date, /usr/bin/yum

##8.1 コマンドエイリアス
コマンドエイリアスは、複数のコマンドを1つにまとめたものです。

エイリアス名 エイリアスに含まれるコマンド
NETWORKING route,ifconfig,ping,dhclient,net,iptables, rfcomm,wvdial,iwconfig,mii-tool
SOFTWARE rpm,up2date,yum
SERVICES service,chkconfig,systemctl start,stop等
LOCATE updatedb
STORAGE fdisk,sfdisk,parted,partprobe,mount,umount
DELEGATING visudo,chown,chmod,chgrp
PROCESSES nice,kill,killall
DRIVERS modprobe

##8.2 実行結果
ここでは、SOFTWAREエイリアスを使ってみます。
SOFTWAREエイリアスを有効にすることで、rpm,yum,up2dateが実行できるようになります。

sudoersの編集
[root@centos74 ~]# id
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

visudoコマンドを使って、/etc/sudoersのSOFTWAREを有効('#'を削除する)にします。
[root@centos74 ~]# visudo
#Cmnd_Alias SOFTWARE = /bin/rpm, /usr/bin/up2date, /usr/bin/yum
Cmnd_Alias SOFTWARE = /bin/rpm, /usr/bin/up2date, /usr/bin/yum

test1ユーザに対して、SOFTWAREコマンドエイリアスの実行を許可する。
Warningがでますが、man visudoによると、問題ないようです。
[root@centos74 ~]# visudo -f /etc/sudoers.d/test
test1 ALL=(ALL) NOPASSWD:SOFTWARE
Warning: /etc/sudoers.d/test:1 Cmnd_Alias "SOFTWARE" referenced but not defined

[root@centos74 ~]# cat /etc/sudoers.d/test
test1 ALL=(ALL) NOPASSWD:SOFTWARE
yumコマンドの実行結果
[root@centos74 ~]# su - test1
最終ログイン: 2018/04/14 (土) 20:20:58 JST日時 pts/0

[test1@centos74 ~]$ sudo yum -y install bc
[test1@centos74 ~]$ rpm -qa|grep -w bc
bc-1.06.95-13.el7.x86_64

[test1@centos74 ~]$ sudo yum -y remove bc
[test1@centos74 ~]$ rpm -qa|grep -w bc
[test1@centos74 ~]$
rpmコマンドの実行結果
ソースパッケージをダウンロードする。
[test1@centos74 ~]$ yumdownloader bc
[test1@centos74 ~]$ ls bc-1.06.95-13.el7.x86_64.rpm
bc-1.06.95-13.el7.x86_64.rpm

bcパッケージをインストールする。
[test1@centos74 ~]$ sudo rpm -ivh bc-1.06.95-13.el7.x86_64.rpm
[test1@centos74 ~]$ rpm -qa|grep -w bc
bc-1.06.95-13.el7.x86_64

bcパッケージをアンインストールする。
[test1@centos74 ~]$ sudo rpm -e bc
[test1@centos74 ~]$ rpm -qa|grep -w bc
[test1@centos74 ~]$

#9 ログの保存場所
sudoコマンドの実行結果は/var/log/secureに保存されます。

#x 参考情報
Sudo を設定する
/etc/sudoers をすっきりさせる

29
36
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
29
36