当我想要vimdiff
根文件时,我使用以下别名,按照这个建议。
alias sudovimdiff='SUDO_EDITOR=vimdiff sudoedit'
然后我可以使用以下命令。
$ sudovimdiff /root/a /root/b
但是,如果我的用户可写入其中一个文件,则该命令将失败。
$ sudovimdiff /root/a /tmp/b
sudoedit: /tmp/b: editing files in a writable directory is not permitted
sudoedit
有没有一种方法可以使用我的用户的环境设置(即)来 vimdiff 一个根文件和一个非根文件?
答案1
与 sudoedit 错误消息相关可能有用:
sudoedit: ...不允许编辑可写目录中的文件
请尝试使用 修改 sudoers 文件sudo visudo
,添加一行:
Defaults !sudoedit_checkdir
更多的这里。
答案2
从man sudo
,在描述部分-e
(又名sudoedit
):
To help prevent the editing of unauthorized files, the
following restrictions are enforced unless explicitly allowed
by the security policy:
· Symbolic links may not be edited (version 1.8.15 and
higher).
· Symbolic links along the path to be edited are not
followed when the parent directory is writable by the
invoking user unless that user is root (version 1.8.16
and higher).
· Files located in a directory that is writable by the
invoking user may not be edited unless that user is root
(version 1.8.16 and higher).
所以,要么:
- 我们
sudoedit
以 root 身份调用,这会达不到目的或 我们将用户的文件复制到用户不可编辑的新目录:
mkdir /tmp/foo cp /tmp/b /tmp/foo chmod a-w /tmp/foo sudoedit /root/a /tmp/foo/b
我们编辑根文件并在里面比较它:
sudoedit /root/a # in Vim :vert diffsplit /tmp/b
由于
sudoedit
处理所有非 sudo 参数文件名,因此您可以使用包装脚本:$ cat foo.sh #! /bin/sh exec vimdiff "$@" "$DIFF_FILE" $ SUDO_EDITOR ="$PWD/foo.sh" DIFF_FILE="$PWD/.zshrc" sudoedit /etc/zsh/zshrc [sudo] password for muru: 2 files to edit sudoedit: /etc/zsh/zshrc unchanged