授予 sudo 访问权限而不提示输入密码

授予 sudo 访问权限而不提示输入密码

我已经将用户名添加到 sudoer 文件,但如下所示,运行时sudo ls我必须输入密码。如何修复该问题以便为用户运行无密码 sudo 命令test

test@ubuntu:~$ sudo ls
[sudo] password for test:
test@ubuntu:~$ sudo cat /etc/sudoers
#
# 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
test    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:

#includedir /etc/sudoers.d
test@ubuntu:~$

更新:

设置完成后NOPASSWD:,仍然需要输入密码。见下图:

test@ubuntu:~$ sudo ls
[sudo] password for test:
test@ubuntu:~$ sudo cat /etc/sudoers
#
# 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
test    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
test@ubuntu:~$

答案1

您可以使用它test ALL=(ALL) NOPASSWD: ALL来让用户test无需密码即可访问

请注意,如果test用户在adminsudo组中,其设置将被组设置覆盖。为避免这种情况,请在组设置后写入用户设置:

%admin ALL=(ALL) ALL
%sudo   ALL=(ALL:ALL) ALL
...
test    ALL=(ALL:ALL) NOPASSWD: ALL

相关内容