sudo でパスワードを聞かれないようにする (NOPASSWDを設定しても聞かれる?)

sudoでパスワードを聞かれない様に設定

sudo コマンド実行時にパスワードを聞かれない様にするには

/etc/sudoers

というファイルに設定する。
例えば、may というアカウントで、すべてのコマンドでパスワードを聞かれない状態にするには、以下の様に記述すれば良い。

may ALL=(ALL:ALL) NOPASSWD: ALL

編集は以下の様に行うこともできますが

vi /etc/sudoers

以下のコマンドを使うことが推奨されています。

visudo

NOPASSWD設定したのにパスワードを聞かれる場合

ただし、上記の記述(may ALL=(ALL:ALL) NOPASSWD: ALL)の下に別の記述があると、設定が上書きされてしまいます。

例えば、下記↓の様に設定していて、アカウントmayがグループsudoに入っていると、%sudo ALL=(ALL:ALL) ALL の設定が上書きされてしまい、アカウントmayNOPASSWDでなくなってしまいます。つまりパスワード聞かれます。

#
# 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.
#
# See the man page for details on how to write a sudoers file.
#
Defaults        env_reset
Defaults        mail_badpass
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

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

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

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

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

#includedir /etc/sudoers.d

下記↓の様に設定すれば、アカウントmayがグループsudoに入っていても、アカウントmayNOPASSWDになります。つまりパスワード聞かれません。

#
# 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.
#
# See the man page for details on how to write a sudoers file.
#
Defaults        env_reset
Defaults        mail_badpass
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
# Host alias specification
# User alias specification
# Cmnd alias specification
# User privilege specification
root    ALL=(ALL:ALL) ALL
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL
# See sudoers(5) for more information on "#include" directives:
may    ALL=(ALL:ALL) NOPASSWD: ALL
#includedir /etc/sudoers.d


この記事が気に入ったらサポートをしてみませんか?