问题

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

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

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

chown: /usr/local: Operation not permittedchown: /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 0drwxrwxrwx    2 root  wheel  restricted   64 10  9 18:07 Caskroomdrwxrwxrwx   53 root  wheel  restricted   1696 10 10 17:13 Cellardrwxrwxrwx    4 root  wheel  restricted   128 11 19  2019 Frameworksdrwxrwxrwx   20 root  wheel  -            640 10 10 17:12 Homebrewdrwxrwxrwx  243 root  wheel  restricted   7776 10 10 17:13 bindrwxrwxrwx   26 root  wheel  -            832 10  9 18:20 etcdrwxrwxrwx   67 root  wheel  restricted   2144 10 10 17:13 includedrwxrwxrwx  196 root  wheel  restricted   6272 10 10 17:13 libdrwxrwxrwx    4 root  wheel  -            128 10 10 16:24 libexecdrwxrwxrwx   75 root  wheel  -            2400 10 10 17:13 optdrwxrwxrwx    9 root  wheel  restricted   288 10  9 18:16 sbindrwxrwxrwx   29 root  wheel  restricted   928 10 10 17:13 sharedrwxrwxrwx    3 mzy   wheel  -            96  4 16  2018 texlivedrwxrwxrwx    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爱护,因为这可能使零碎陷入危险。