それマグで!

知識はカップより、マグでゆっくり頂きます。 takuya_1stのブログ

習慣に早くから配慮した者は、 おそらく人生の実りも大きい。

sudoersの設定を分割する /etc/sudoers.d を使おう

/etc/sudoers.d で分割できる。

/etc/sudoers.d を使うと、必要なものを覚えやすいファイル名で書いておいてバージョン管理することが出来る。

編集方法

編集方法がめんどくさい。分割して管理できるが、各ファイルを編集するのにvisudoを使う必要があり、これがめんどくさい。

visudo -f /etc/sudoers.d/my_conf

ただしく読み込まれたか

もし直接パスを指定して、編集し、手作業で読み込んだら、一旦パースしてテストするしか無いかもね。

sudo vim  /etc/sudoers.d/my_conf
visudo -x - | jq  

読み込む仕組み

どのタイミングで読み込むのかは /etc/sudoers を見ればわかります。

/etc/sudoers

sudoersには次のように書いてあります。最終行の #includedir に注目。 なのですべての一番最後に読み込む様になっています。

#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL
takuya  ALL=(ALL) NOPASSWD: ALL

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

#includedir /etc/sudoers.d

#includedir はコメントじゃないの?

コメントではありません。

#includedir /etc/sudoers.d  # ←これは正しい
includedir /etc/sudoers.d  # ←これは間違い

一見すると、コメントに見えて、ハッシュ(#)を消そうと思うかもしれませんが。これは明確にディレクティブですね。*1

sudoersのファイルを見ていると、コメントでは、 '# ' と空白文字を入れて区別しているようですね。

# comment 
#include

参考資料

https://wiki.debian.org/sudo