如何将自己添加到 sudoers 列表?

osh*_*nen 7 linux debian sudo

我已使用以下命令将自己添加到 sudoers 用户列表中

root@debian:/home/oshirowanen#adduser oshirowanen sudo
Run Code Online (Sandbox Code Playgroud)

如果我再次尝试运行该命令,

root@debian:/home/oshirowanen# adduser oshirowanen sudo
The user `oshirowanen' is already a member of `sudo'.
root@debian:/home/oshirowanen# 
Run Code Online (Sandbox Code Playgroud)

到目前为止一切看起来都很好。

当我退出 root 用户并尝试使用我自己的帐户安装/删除/搜索某些内容时,它不起作用并抱怨我不是 sudoer ......例如

root@debian:/home/oshirowanen# exit
exit
oshirowanen@debian:~$ sudo aptitude search ice
[sudo] password for oshirowanen: 
oshirowanen is not in the sudoers file.  This incident will be reported.
oshirowanen@debian:~$ 
Run Code Online (Sandbox Code Playgroud)

为什么会这样?


这是我得到的 visudo

#
# 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"

# Host alias specification

# User alias specification

# 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

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

#includedir /etc/sudoers.d
Run Code Online (Sandbox Code Playgroud)

l0b*_*0b0 14

加入群组后需要重新登录才能获得正确的权限。

使用两个 shell 进行验证:

alice $ sudo adduser test
                                alice $ su - test
alice $ sudo adduser test sudo
                                test $ sudo ls
                                test is not in the sudoers file.  [...]
                                test $ exit
                                alice $ su - test
                                test $ sudo ls
                                examples.desktop
Run Code Online (Sandbox Code Playgroud)

澄清一下,在将用户添加到sudo之前打开的任何 shell都没有新权限。


Did*_*hen 6

这里有两件不同的事情在起作用:

  1. sudo 用户组。
  2. /etc/sudoers 文件。

在某些发行版中,sudoers 组在 sudoers 文件中配置为通过 sudo 运行所有内容。
要添加组,您可以通过以 root 身份运行来编辑文件:

visudo
Run Code Online (Sandbox Code Playgroud)

并添加以下内容(或取消注释):

%sudo ALL=(ALL) ALL
Run Code Online (Sandbox Code Playgroud)

% 符号表示它是一个组名,第一个“ALL”是它可以运行的主机,第二个是它可以模拟的用户,最后一个“ALL”是它可以通过 sudo 运行的命令。

此外,您可能需要重新登录才能使新的组成员身份生效。
要检查活动组成员身份,请运行:

id
Run Code Online (Sandbox Code Playgroud)