为什么 git 需要 sudo 权限才能克隆,如果需要,写入根目录?

Why does git needs sudo privileges to clone and if so, writes to root directory?

我正在尝试克隆存储库,但出现此错误:

$ sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
Cloning Oh My Zsh...
fatal: could not create work tree dir '/usr/share/oh-my-zsh': Permission denied
Error: git clone of oh-my-zsh repo failed

如果我在上一个命令之前使用 sudo,它会写入根目录,如您所见:

$ sudo sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
The $ZSH folder already exists (/root/.oh-my-zsh).
You'll need to remove it if you want to reinstall.

如何解决这个问题?我知道 git clone 应该写入当前目录,但事实并非如此!为什么?

这不是 Git,这是 the install script for oh-my-zsh

install.sh 正在尝试克隆存储库。它的安装位置由 ZSH 环境变量控制。默认情况下,它将安装到 ~/.oh-my-zsh。如果它进入 /usr/share/oh-my-zsh 你已经设置了 ZSH。您无权写入 /usr/share。这是正常的,普通用户不应该有 /usr/share 权限。

当 运行 具有 sudo(即具有 root 权限)时,它确实有权写入 /usr/share/oh-my-zsh,但 sudo 将清理您的环境变量,因此没有为该命令设置 ZSH。它 运行 及其默认值 ~/.oh-my-zsh。通常 sudo 应该保留你的主目录,但不知何故它并没有进入 root 的主目录。


如何“解决”这个取决于你想要什么。首先,使用 env | sort 检查您的环境中的内容并确保一切正确。

如果您只想自己安装软件,unset ZSH 和 运行 安装程序。它将进入 ~/oh-my-zsh.

如果你想给大家安装软件,下载脚本,使用sudomake setting ZSH part of the command

cd /tmp
curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh
sudo ZSH=/usr/share/oh-my-sh sh -c install.sh