【已解决】开工大吉:折腾当前管理员账号不在 sudoer 列表里的问题
在小程序里查看本文
问题
春节前一天,升级了系统到 macOS Monterey。
今天虎年开工第一天,正常开机看邮件什么的,没觉得有什么问题。直到自己安装软件时,碰到了这个错误:
brew install --cask wireshark
...
==> Moving App 'Wireshark.app' to '/Applications/Wireshark.app'
==> Backing App 'Wireshark.app' up to '/usr/local/Caskroom/wireshark/3.6.1/Wireshark.app'
==> Removing App '/Applications/Wireshark.app'
jietian is not in the sudoers file. This incident will be reported.
==> Purging files for version 3.6.1 of Cask wireshark
Error: It seems there is already a Binary at '/usr/local/bin/editcap'.
就是说,明明将软件已经安装好了后,又自动被移除了。注意看报错第 4 行,说我不在 suoers 文件里。
排查
很自然地想看看 /etc/sudoers 文件里的内容,就执行了:
sudo cat /etc/sudoers
jietian is not in the sudoers file. This incident will be reported.
sudo visudo
jietian is not in the sudoers file. This incident will be reported.
依然得到同样的答复。
我心里一凉,以为自己的管理员权限被公司收回了!赶紧打开账号界面确认了一下,居然还在呀:
又用 id -a
命令行看了一下:
id -a
uid=239330239(jietian) gid=1049672903 groups=1049672903,12(everyone),20(staff),62(netaccounts),80(admin),701(com.apple.sharepoint.group.1),33(_appstore),98(_lpadmin),100(_lpoperator),204(_developer),250(_analyticsusers),395(com.apple.access_ftp),398(com.apple.access_screensharing),101(com.apple.access_ssh-disabled),400(com.apple.access_remote_ae)
注意看 80(admin),再次确认自己的账号的确是管理员权限。
网上搜了一下,这是因为 /etc/sudoers 文件被损坏了导致的……
但是网上给的如何修复的命令行建议,试了后都不行,因为死循环。因为要编辑 /etc/sudoers 文件,得先 sudo,但是 sudo 时,就会给到当前用户不在 sudoers 里的报错。
最终解决
在网上找到了一个人提出来,用 GUI 去修改 /etc/sudoers 的思路,这是我最终解决问题的突破口。但是这个人只说了这么想法,具体改什么内容没说。而且改了之后,还是有其他问题,总之又一个问题一个问题的尝试和搜索,最终完美解决了。赶紧记录下来。
一、将 /etc/sudoers 文件,使用 GUI(Finder)复制粘贴到桌面
要求输入密码或者按指纹时,用当前账号的密码和指纹解锁即可。
二、使用 GUI 直接修改桌面上的 /etc/sudoers 文件
把自己的账号信息添加到 root 账号下面,即:
#
User specification
#
# root and users in group wheel can run anything on any machine as any user
root ALL = (ALL) ALL
+ jietian ALL = (ALL) ALL
admin ALL = (ALL) ALL
## Read drop-in files from /private/etc/sudoers.
保存,然后再将这个修改后的 sudoers 文件粘贴回 /etc/ 目录下,替换掉原来的文件。
三、执行一段脚本,将 sudoers 文件的拥有者改回 root
第二步完成后,再次尝试安装软件,或者任何需要 sudo 的命令,发现会报这个错:
sudo cat /etc/sudoers
sudo: /etc/sudoers is owned by uid 239330239, should be 0
sudo: no valid sudoers sources found, quitting
sudo: error initializing audit plugin sudoers_audit
这并不能通过直接 chown
命令解决,因为会碰到 Operation not permitted
错误:
chown root:wheel /etc/sudoers
chown: /etc/sudoers: Operation not permitted
但是,在网上居然找到了这样一条神奇的方法,即通过执行 AppleScript 来解决。这样不仅行得通,而且连重启甚至登出或者连开新的 shell 都不用!
osascript -e 'do shell script "chown root:wheel /etc/sudoers; chmod 440 /etc/sudoers; chmod -N /etc/sudoers" with administrator privileges'
在以上命令成功执行后,就一切正常了!
osascript -e 'do shell script "chown root:wheel /etc/sudoers; chmod 440 /etc/sudoers; chmod -N /etc/sudoers" with administrator privileges'
sudo cat /etc/sudoers
Password:
#
# 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.
##
# Override built-in defaults
##
...
总结与回顾
升级系统有风险,但是还好,这个风险可以完美解决。也算是开工大吉的第一吉吧!
升级到 Monterey 之后,苹果系统的 shell 和 GUI 似乎产生了脑裂问题,shell 不认为你是管理员了,但是 GUI 还认!也因此可以通过 GUI 的方式修复了 sudoers 文件。
在使用 GUI 修改了 sudoers 文件后,该文件的拥有者也被从 root 改成了当前用户了,要改回去,通常的 chown 不行,还是因为脑裂。但是通过 AppleScript 却可以解决,大概因为 AppleScript 也算是 GUI 的一部分吧。
彩蛋
据说 osascript
可以通过 do shell script
的方式执行较短的 AppleScript 脚本,即产生了一个间接的调用:
shell 调用 AppleScript,然后这个被调用的 AppleScript 再调用 shell。这种方式可以使用到 GUI 的管理员权限,这种方式在很多地方可以做和 sudo
同样的事情,并且在 /etc/sudoers 文件完全乱掉的情况下,依然可用!
所以,以后你碰到使用 sudo
命令不顺利的情况下,可以再尝试一下使用 shell -> AppleScript -> shell 的方式!当然,前提是自己的账号是管理员。