【问题标题】:Sudoers syntax errorsudoers 语法错误
【发布时间】:2018-02-16 19:19:13
【问题描述】:

每次我运行 sudo 命令时,都会收到以下错误消息:

/etc/sudoers: 第 1 行附近的语法错误

我在 /etc/ 中的 sudoers 文件是空的。我该如何解决这个问题?我在 Mac OS High Sierra 上

【问题讨论】:

  • 看看这是否有帮助:stackoverflow.com/questions/33003847/…
  • 当我在终端 ls -l /etc/sudoers 中输入此内容时,它会输出正确的权限 -r--r----- 1 root wheel 4 Feb 16 10:56 /etc/sudoers

标签: macos sudo


【解决方案1】:

您可以尝试此链接来获取一些 sudoer 文件示例。使用visudo 命令重新创建您的 sudoers 文件

http://www.softpanorama.org/Access_control/Sudo/sudoer_file_examples.shtml

如果链接断开,这里是该页面上描述的“默认”sudoer 文件:

# Sample /etc/sudoers file.
#
# This file MUST be edited with the 'visudo' command as root.
#
# See the sudoers man page for the details on how to write a sudoers file.
#

##
# User alias specification
##
User_Alias  FULLTIMERS = millert, mikef, dowdy
User_Alias  PARTTIMERS = bostley, jwfox, crawl
User_Alias  WEBMASTERS = will, wendy, wim

##
# Runas alias specification
##
Runas_Alias OP = root, operator
Runas_Alias DB = oracle, sybase

##
# Host alias specification
##
Host_Alias  SPARC = bigtime, eclipse, moet, anchor:\
        SGI = grolsch, dandelion, black:\
        ALPHA = widget, thalamus, foobar:\
        HPPA = boa, nag, python
Host_Alias  CUNETS = 128.138.0.0/255.255.0.0
Host_Alias  CSNETS = 128.138.243.0, 128.138.204.0/24, 128.138.242.0
Host_Alias  SERVERS = master, mail, www, ns
Host_Alias  CDROM = orion, perseus, hercules

##
# Cmnd alias specification
##
Cmnd_Alias  DUMPS = /usr/sbin/dump, /usr/sbin/rdump, /usr/sbin/restore, \
            /usr/sbin/rrestore, /usr/bin/mt
Cmnd_Alias  KILL = /usr/bin/kill
Cmnd_Alias  PRINTING = /usr/sbin/lpc, /usr/bin/lprm
Cmnd_Alias  SHUTDOWN = /usr/sbin/shutdown
Cmnd_Alias  HALT = /usr/sbin/halt
Cmnd_Alias  REBOOT = /usr/sbin/reboot
Cmnd_Alias  SHELLS = /sbin/sh, /usr/bin/sh, /usr/bin/csh, /usr/bin/ksh, \
             /usr/local/bin/tcsh, /usr/bin/rsh, \
             /usr/local/bin/zsh
Cmnd_Alias  SU = /usr/bin/su
Cmnd_Alias  VIPW = /usr/sbin/vipw, /usr/bin/passwd, /usr/bin/chsh, \
               /usr/bin/chfn

##
# Override built-in defaults
##
Defaults               syslog=auth
Defaults>root          !set_logname
Defaults:FULLTIMERS    !lecture
Defaults:millert       !authenticate
Defaults@SERVERS       log_year, logfile=/var/log/sudo.log

##
# User specification
##

# root and users in group wheel can run anything on any machine as any user
root        ALL = (ALL) ALL
%wheel      ALL = (ALL) ALL

# full time sysadmins can run anything on any machine without a password
FULLTIMERS  ALL = NOPASSWD: ALL

# part time sysadmins may run anything but need a password
PARTTIMERS  ALL = ALL

# jack may run anything on machines in CSNETS
jack        CSNETS = ALL

# lisa may run any command on any host in CUNETS (a class B network)
lisa        CUNETS = ALL

# operator may run maintenance commands and anything in /usr/oper/bin/
operator    ALL = DUMPS, KILL, SHUTDOWN, HALT, REBOOT, PRINTING,\
        sudoedit /etc/printcap, /usr/oper/bin/

# joe may su only to operator
joe     ALL = /usr/bin/su operator

# pete may change passwords for anyone but root on the hp snakes
pete        HPPA = /usr/bin/passwd [A-z]*, !/usr/bin/passwd root

# bob may run anything on the sparc and sgi machines as any user
# listed in the Runas_Alias "OP" (ie: root and operator)
bob     SPARC = (OP) ALL : SGI = (OP) ALL

# jim may run anything on machines in the biglab netgroup
jim     +biglab = ALL

# users in the secretaries netgroup need to help manage the printers
# as well as add and remove users
+secretaries    ALL = PRINTING, /usr/bin/adduser, /usr/bin/rmuser

# fred can run commands as oracle or sybase without a password
fred        ALL = (DB) NOPASSWD: ALL

# on the alphas, john may su to anyone but root and flags are not allowed
john        ALPHA = /usr/bin/su [!-]*, !/usr/bin/su *root*

# jen can run anything on all machines except the ones
# in the "SERVERS" Host_Alias
jen     ALL, !SERVERS = ALL

# jill can run any commands in the directory /usr/bin/, except for
# those in the SU and SHELLS aliases.
jill        SERVERS = /usr/bin/, !SU, !SHELLS

# steve can run any command in the directory /usr/local/op_commands/
# as user operator.
steve       CSNETS = (operator) /usr/local/op_commands/

# matt needs to be able to kill things on his workstation when
# they get hung.
matt        valkyrie = KILL

# users in the WEBMASTERS User_Alias (will, wendy, and wim)
# may run any command as user www (which owns the web pages)
# or simply su to www.
WEBMASTERS  www = (www) ALL, (root) /usr/bin/su www

# anyone can mount/unmount a cd-rom on the machines in the CDROM alias
ALL     CDROM = NOPASSWD: /sbin/umount /CDROM,\
        /sbin/mount -o nosuid\,nodev /dev/cd0a /CDROM

【讨论】:

  • 我复制并粘贴了默认的 sudo 文件,现在得到错误: > sudo: /etc/sudoers 由 gid 80 拥有,应该是 0 > sudo: 找不到有效的 sudoers 源,退出 > sudo:无法初始化策略插件
  • 这意味着你的文件属于错误的组,组0通常是root。您可以尝试使用命令chown root:root /etc/sudoerschmod 440 /etc/sudoers 确保您的sudoers 文件属于root,这会将所有权更改为root 用户和组,并将其设置为具有-r--r----- 权限。
  • 我刚刚阅读了您对这个问题的评论。看起来权限和用户所有权是正确的。所以这个错误只是表明你的组是唯一不对齐的部分
  • chown root:root /etc/sudoers 输出 chown: root: 非法组名chmod 440 /etc/sudoers i> 输出相同
  • 我还尝试使用 chmod 440 /etc/ 更改仅 /etc/ 目录的权限,输出为 无法更改 /etc/ 上的文件模式:不允许操作
【解决方案2】:

切勿使用普通编辑器打开 sudoer 文件。总是使用 visudo

只需输入

sudo visudo

这会将您带到/etc/sudoers,保存后将确保格式没有错误。

如果您在 sudoer 文件中出错,您将失去 sudo 访问权限,因此请始终使用visudo

【讨论】:

    【解决方案3】:

    如果遇到困难,可以使用 pkexec

    pkexec 允许您以另一个用户身份执行程序。如果您不指定用户,则程序将以 root 身份执行

    1. 根升级

    pkexec bash

    1. 修复语法错误

    visudo

    【讨论】:

      【解决方案4】:

      Ubuntu 20.04 在“sudo anycommand”时遇到此错误

      /etc/sudoers.d/sudoers:第 29 行附近的包含级别太多

      我的解决方案: 虽然不知道如何在 /etc/sudoers.d 中创建 sudoers 文件。 阅读 /etc/sudoers.d 中的 README 后,

      自述文件摘录

      注意,sudoers.d 目录下必须至少有一个文件(这个就可以了),并且该目录下的所有文件都应该是mode 0440。

      删除 /etc/sudoers.d 中的 sudoers

      pkexec rm /etc/sudoers.d/sudoers
      

      系统会提示输入用户密码。

      可以像往常一样执行sudo命令。

      【讨论】:

        猜你喜欢
        • 2015-04-16
        • 1970-01-01
        • 2021-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-16
        相关资源
        最近更新 更多