关于homebrew:macOS-下usrlocal目录下Operation-not-permitted的问题

9次阅读

共计 1491 个字符,预计需要花费 4 分钟才能阅读完成。

问题

这个状况在尤其是在 brew 的应用过程中会呈现,具体表现为:

Error: /usr/local is not writable. You should change the ownership
and permissions of /usr/local back to your user account:
  sudo chown -R $(whoami) /usr/local

如果你依照他的要求执行 chown -R,很大概率下你会遇到下列谬误

chown: /usr/local: Operation not permitted
chown: /usr/local/Cellar: Operation not permitted
......

起因和解决办法

造成这个问题的理论起因是 Apple 的 Rootless(System Integrity Protection) 策略。

“Operation not permitted”并不是因为权限问题(权限问题能够用 chown 或者 chmod 来解决),而是触发了 macOS 的 SIP 爱护。

SIP 标识符:restricted

遇到这个问题时,如果你应用 ls -lO 查看 /usr/local,你会发现上面的大部分文件都被打上了 restricted 标记。

total 0
drwxrwxrwx    2 root  wheel  restricted   64 10  9 18:07 Caskroom
drwxrwxrwx   53 root  wheel  restricted   1696 10 10 17:13 Cellar
drwxrwxrwx    4 root  wheel  restricted   128 11 19  2019 Frameworks
drwxrwxrwx   20 root  wheel  -            640 10 10 17:12 Homebrew
drwxrwxrwx  243 root  wheel  restricted   7776 10 10 17:13 bin
drwxrwxrwx   26 root  wheel  -            832 10  9 18:20 etc
drwxrwxrwx   67 root  wheel  restricted   2144 10 10 17:13 include
drwxrwxrwx  196 root  wheel  restricted   6272 10 10 17:13 lib
drwxrwxrwx    4 root  wheel  -            128 10 10 16:24 libexec
drwxrwxrwx   75 root  wheel  -            2400 10 10 17:13 opt
drwxrwxrwx    9 root  wheel  restricted   288 10  9 18:16 sbin
drwxrwxrwx   29 root  wheel  restricted   928 10 10 17:13 share
drwxrwxrwx    3 mzy   wheel  -            96  4 16  2018 texlive
drwxrwxrwx    8 root  wheel  -            256 10  9 18:23 var

这个标记的用处就是通知 macOS 的 SIP,这个文件受到零碎爱护,不能更改。

解除爱护的命令就一句话
chflags -R norestricted .
便能够把当前目录以及他们子目录下的所有文件解除爱护(链接除外,需手动解决)
然而如果你的 macOS 正处于 SIP 爱护下,你是无权执行该命令的。
查看 SIP 爱护状态,只须要在 terminal 里输出
csrutil status
如果显示System Integrity Protection status: enabled. 则意味着 SIP 作动中。

勾销 SIP 爱护

重启电脑,按 Command+ R 进入恢复模式,关上恢复模式下的终端。
在这里 cd /Volumes/ 你的硬盘名 /usr
再执行 chflags 就能够解除这些文件夹的 SIP 爱护了。
我并不倡议你应用 csrutil disable 来敞开 SIP 爱护,因为这可能使零碎陷入危险。

正文完
 0