问题描述

在执行 brew cleanup 的时候因为权限问题报错, Error: Permission denied @ apply2files,具体的文件是 ghostscript/9.27/Resource/CIDFSubst/paexg.ttf,查看了一下是一个软连接指向 /Library/TeX/Root/texmf-dist/fonts/truetype/public/ipaex/ipaexg.ttf。(另外,好像没有这个文件) 而那个软链的所有者是 wheel 用户组下的 root 用户,因此出现了权限问题?(用下文中的 cat /etc/group | grep wheel 命令看了下这个组中只有 root 一个用户)。

于是搜到了这个问题 https://github.com/Homebrew/homebrew-core/issues/45009

看到有人的解决方案非常粗暴:直接重装 brew 【注意:会卸载所有通过 brew 安装的软件,慎用!】

1
2
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

解决方案:修改 /usr/local 权限

另一个方案是修改整个文件夹的权限(不知道是否会带来后续隐患)

1
2
sudo chown -R $(whoami):admin /usr/local/* \
&& sudo chmod -R g+rwx /usr/local/*

然后在执行 brew cleanup 就没问题了。

后续:zsh 警告

果然,新开一个终端之后,zsh 警告

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
zsh compinit: insecure directories, run compaudit for list.
Ignore insecure directories and continue [y] or abort compinit [n]? ncompinit: initialization aborted
complete:13: command not found: compdef
[oh-my-zsh] Insecure completion-dependent directories detected:
drwxrwxr-x 3 frankshi admin 96 Oct 25 12:03 /usr/local/share/zsh

[oh-my-zsh] For safety, we will not load completions from these directories until
[oh-my-zsh] you fix their permissions and ownership and restart zsh.
[oh-my-zsh] See the above list for directories with group or other writability.

[oh-my-zsh] To fix your permissions you can do so by disabling
[oh-my-zsh] the write permission of "group" and "others" and making sure that the
[oh-my-zsh] owner of these directories is either root or your current user.
[oh-my-zsh] The following command may help:
[oh-my-zsh] compaudit | xargs chmod g-w,o-w

[oh-my-zsh] If the above didn't help or you want to skip the verification of
[oh-my-zsh] insecure directories you can set the variable ZSH_DISABLE_COMPFIX to
[oh-my-zsh] "true" before oh-my-zsh is sourced in your zshrc file.

里面写的很详细了!并且提到了 compaudit 来查看是那个目录有安全隐患。 (下面的那几条信息好像是我输入了 n 之后出现的?)

总之,就是要改某些文件夹的所有者和权限。 利用 compaudit 发现目录为 /usr/local/share/zsh。 我是直接将该目录的权限修改为 755,即 sudo chmod -R 755 zsh,再启动一个终端(或者 source ~/.zshrc )显示正常。

参见 https://github.com/zsh-users/zsh-completions/issues/433;对于 zsh 报的权限问题,通用思路是

  1. run compaudit and it will give you a list of directories it thinks are unsecure
  2. run sudo chown -R username:root target_directory
  3. run sudo chmod -R 755 target_directory

补充:关于 wheel 用户组

参见 Linux下Wheel用户组介绍

在Linux中为了更进一步加强系统的安全性,很有必要建立了一个管理员的组,只允许这个组的用户来执行“su -”命令登录为root用户,而让其他组的用户即使执行“su -”、输入了正确的root密码,也无法登录为root用户。在UNIX和Linux下,这个组的名称通常为“wheel”。而这个是在配置文件/etc/pam.d/su里面配置的。