$ py-spy top --pid 11988
Permission Denied: Try running again with elevated permissions by going 'sudo env "PATH=$PATH" !!'
$ sudo py-spy top --pid 11988
sudo: py-spy: command not found
$ sudo cat /etc/sudoers
#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
...something...
Defaults secure_path="/home/urername/Softwares/texlive/2019/bin/x86_64-linux:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
当你使用sudo去执行一个程序时,处于安全的考虑,这个程序将在一个新的、最小化的环境中执行,也就是说,诸如PATH这样的环境变量,在sudo命令下已经被重置成默认状态了。所以当一个刚初始化的PATH变量中不包含你所要运行的程序所在的目录,用sudo去执行,你就会得到"command not found"的错误提示。
添加所需要的路径(如 /usr/local/bin)到secure_path下,在开篇所遇见的问题就将迎刃而解::Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin。这个修改会即刻生效。
要想改变PATH在sudo会话中的初始值,用文本编辑器打开/etc/sudoers文件,找到secure_path一行,当你执行sudo命令时,secure_path中包含的路径将被当做默认PATH变量使用。
造成原因
应该是因为使用sudo的时候,对应的环境变量和平时不同,所以需要特别处理下。
解决方案
个人最终使用如下形式搞定:
sudo env "PATH=$PATH" py-spy top --pid 11988
You can always do:
sudo env "PATH=$PATH" godi_console
As a security measure on Debian, /etc/sudoers has the secure_path option set to a safe value.
Note that:
sudo "PATH=$PATH" godi_console
Where sudo treats leading arguments containing = characters as environment variable assignments by itself, would also work at running godi_console with your $PATH (as opposed to the secure_path) in its environment, but would not affect sudo's search path for executable, so wouldn't help sudo in finding that godi_console.
How to make
sudo
preserve$PATH
?问题介绍
在使用
py-spy
的时候,提示:google了一下,找到了这个答案:https://unix.stackexchange.com/questions/83191/how-to-make-sudo-preserve-path/83194#83194?newreg=428c46e215aa4198b51ef2d9776e1366
在linux中国中找到一个解释性的文章:
造成原因
应该是因为使用
sudo
的时候,对应的环境变量和平时不同,所以需要特别处理下。解决方案
You can always do:
As a security measure on Debian,
/etc/sudoers
has thesecure_path
option set to a safe value.Note that:
Where
sudo
treats leading arguments containing=
characters as environment variable assignments by itself, would also work at runninggodi_console
with your$PATH
(as opposed to thesecure_path
) in its environment, but would not affect sudo's search path for executable, so wouldn't help sudo in finding thatgodi_console
.参考链接
sudo
配置的更多彩蛋~:https://linux.cn/article-8145-1.html