我可以通过 CLI 让“sudo”遵循我的路径吗?

我可以通过 CLI 让“sudo”遵循我的路径吗?

您可以编辑sudoers文件并设置secure_path以匹配您的路径。这记录在Unix 和 Linux

是否有一个命令行开关可以传递给它sudo,以便它使用我设置的路径?例如:

rick@alien:~$ sudo echo $PATH
/home/rick/bin:/home/rick/.local/bin:/mnt/e/bin:/mnt/e/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
───────────────────────────────────────────────────────────────────────────────
rick@alien:~$ echo $PATH
/home/rick/bin:/home/rick/.local/bin:/mnt/e/bin:/mnt/e/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
───────────────────────────────────────────────────────────────────────────────
rick@alien:~$ sudo which auto-brightness-config
/usr/local/bin/auto-brightness-config
───────────────────────────────────────────────────────────────────────────────
rick@alien:~$ which auto-brightness-config
/mnt/e/usr/local/bin/auto-brightness-config
───────────────────────────────────────────────────────────────────────────────
rick@alien:~$ sudo locate auto-brightness-config
/Desktop/Link to auto-brightness-config
/home/rick/Pictures/display-auto-brightness-config 1.png
/mnt/e/Desktop/Link to auto-brightness-config
/mnt/e/usr/local/bin/.auto-brightness-config
/mnt/e/usr/local/bin/Link to auto-brightness-config
/mnt/e/usr/local/bin/auto-brightness-config
/usr/local/bin/.auto-brightness-config
/usr/local/bin/Link to auto-brightness-config
/usr/local/bin/auto-brightness-config

当我运行使用sudo它时,找到了错误的脚本副本,而/usr/local/bin我真正想要的是版本/mnt/e/usr/local/bin

如果我不想维护sudoers文件中的路径,是否有一个开关可以传递sudo以使用我的路径来查找正确的命令?

答案1

Ubuntu 默认已经secure_path设置:

~ sudo -l
Matching Defaults entries for muru on muru-1604:
    env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin    
User muru may run the following commands on muru-1604:
    (ALL : ALL) ALL

没有可以覆盖secure_path设置的命令行选项sudoers

man sudoers

 secure_path   Path used for every command run from sudo.  If you don't
               trust the people running sudo to have a sane PATH
               environment variable you may want to use this.  Another use
               is if you want to have the “root path” be separate from the
               “user path”.  Users in the group specified by the
               exempt_group option are not affected by secure_path.  This
               option is not set by default.

因此,要么exempt_group加入sudoers该组,要么免除以下限制sudoers

Defaults:rick !secure_path

rick可能是您的用户名。)

相关内容