xxx is not in the sudoers file. This incident will be reported.
ubuntu下使用sudo命令遇到如下问题:
xxx is not in the sudoers file. This incident will be reported.
解决方案如下:
使用
su -
命令切换到root用户(注意有- ,这和su是不同的,在用命令su
的时候只是切换到root,但没有把root的环境变量传过去,还是当前用户的环境变量,用su -
命令将环境变量也一起带过去,就像和root登录一样),输入root的密码。输入
visudo -f /etc/sudoers
打开sudoers文件,可以看到前面有这样一行注释:# This file MUST be edited with the ‘visudo’ command as root.
这是因为
visudo
会检查语法错误,而vi
或者nano
之类的命令不会检查错误。将光标移到最下面,查找有没有这样的一行文本
#includedir /etc/sudoers.d
,如果有的话继续第3步,否则跳到第4步,对于前者,文件开始处一般会有这样的注释,提示你最好不要直接修改/etc/sudoers文件:# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.对于存在
#includedir /etc/sudoers.d
的情况,把修改内容添加到新的文件。Ctrl + X
退出当前文件,执行cp /etc/sudoers.d/README /etc/sudoers.d/my_sudoers
复制一个文件,文件名可以自己定,这里是my_sudoers
,使用visudo -f /etc/sudoers.d/my_sudoers
打开文件,可以看到文件开始有这样的注释(来自README文件):# Note that there must be at least one file in the sudoers.d directory (this
# one will do), and all files in this directory should be mode 0440.我们的配置文件是从README复制过来的,所以满足这里的条件,把以下代码加入文件末尾(其中xxx是你的用户名),
Ctrl + X
再Y
再Enter
保存退出即可:# User privilege specification
xxx ALL=(ALL:ALL) ALL对于不存在
#includedir /etc/sudoers.d
的情况,找到root ALL=(ALL:ALL) ALL
,复制一行放在下面,把前面的root改成你的用户名,Ctrl + X
再Y
再Enter
保存退出即可。