特定グループでのみ dmesg command を有効にする

Debian の linux 4.8.0 以降で一般ユーザによる dmesg が無効化されたので,kernelパラメータの kernel.dmesg_restrict を修正して dmesg command を一般ユーザでも実行できるようにしましたがこれだと全ユーザで実行できてしまいます.sudo を設定してログの読める adm ユーザだけが dmesg command を実行できるようにしてみました.

dmesgを実行したいユーザをadm group に登録する(loginし直して反映しておく)

$ sudo addgroup matoken adm
$ exit

visudo command で /etc/sudoers を編集して adm group は NOPASSWD で dmesg command を実行できるようにする(念の為編集前にもう1枚端末を開いて sudo -s しておいたり,at等で5分後に /etc/sudoers を巻き戻すようにしておくとミスっても安心)

$ sudo visudo
$ sudo git diff /etc/sudoers
diff --git a/sudoers b/sudoers
index d4cc632..ac3bd77 100644
--- a/sudoers
+++ b/sudoers
@@ -21,6 +21,7 @@ root  ALL=(ALL:ALL) ALL

 # Allow members of group sudo to execute any command
 %sudo  ALL=(ALL:ALL) ALL
+%adm   ALL=NOPASSWD: /usr/bin/dmesg

 # See sudoers(5) for more information on "#include" directives:

adm group のユーザでdmesg コマンドが追加されているのを確認

$ sudo -l | grep dmesg
    (root) NOPASSWD: /usr/bin/dmesg

adm group のユーザが認証無しで sudo dmesg が実行できるのを確認

$ id | grep -o adm
adm
$ sudo dmesg -H | tail -1
[  +0.015080] smsc95xx 1-1.1:1.0 eth0: link up, 100Mbps, full-duplex, lpa 0xCDE1

このとき kernel.dmesg_restrict = 1 で sudo を通さないと dmesg は実行できない

$ cat /proc/sys/kernel/dmesg_restrict
1
$ dmesg 
dmesg: read kernel buffer failed: Operation not permitted
$ LC_MESSAGES=ja_JP.UTF-8 dmesg 
dmesg: read kernel buffer failed: 許可されていない操作です

~/.profile にalias を設定

$ echo 'alias dmesg="sudo dmesg"' | tee -a ~/.profile                                                                                                                                   
alias dmesg="sudo dmesg"
$ source ~/.profile
$ dmesg -H | tail -1
[  +0.015080] smsc95xx 1-1.1:1.0 eth0: link up, 100Mbps, full-duplex, lpa 0xCDE1

環境

$ dpkg-query -W sudo util-linux linux-image-*
linux-image-4.19.0-2-arm64      4.19.16-1
linux-image-4.19.0-2-arm64-unsigned
linux-image-arm64       4.19+102
sudo    1.8.27-1
util-linux      2.33.1-0.1
$ lsb_release -dr
Description:    Debian GNU/Linux buster/sid
Release:        testing
$ uname -a
Linux rpi3 4.19.0-2-arm64 #1 SMP Debian 4.19.16-1 (2019-01-17) aarch64 GNU/Linux
$ cat /proc/device-tree/model ;echo
Raspberry Pi 3 Model B Rev 1.2

One thought to “特定グループでのみ dmesg command を有効にする”

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です