执行sudo命令时command not found的解决办法

问题的原因: 在编译sudo包的时候默认开启了- -with-secure-path选项。

  1. 方法1: 在/etc/sudoers文件内增加这么一行:Defaults secure_path=”/bin:/usr/bin:/usr/local/bin:…”, 把要用的命令path包括进去。
  2. 方法2: 用命令的绝对路径。
  3. 方法3: 使用sudo的env选项,像这样sudo env PATH=$PATH cmd.sh
  4. 方法4: 把脚本拷贝或链接到系统$PATH中。
  5. 方法5: 重新编译sudo,不带–with-secure-path选项了.(终极解决办法)。
    在非root下,可以执行很多命令。在root命令下,却提示 command not found。例如 使用adb
    现象: 当我们用sudo来执行cd、ls等命令时,会出现command not found的提示 在执行Linux命令时,如果在其前面加上sudo,就表示以root权限执行。但是这其实是有一个前提的,就是只有那些Linux内置系统命令才可以用如此的形式来执行,而对于Shell内置命令或其他用户自定义命令、别名等,是不能用sudo来使用root权限的。为什么呢?详细说一下sudo幕后隐藏的过程才能明白。
    因为当在Linux下用sudo执行某一命令时,是在原进程(parent process)的基础上fork出来一个子进程(child process),这个子进程是以root权限执行的。然后在子进程中,执行你在sudo后面跟的命令。
    在子进程中是无法调用涉及到父进程的状态的一些命令的,所以非系统内置命令会被拒绝。这就是为什么会出现command not found的提示。

解决:先用绝对路径执行吧

原因英文版

Path used for every command run from sudo(8). 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.” You will need to customize the path for your site. NOTE: this is not applied to users in the group specified by –with-exemptgroup. If you do not specify a path, “/bin:/usr/ucb:/usr/bin:/usr/sbin:/sbin:/usr/etc:/etc” is used.

作者:北夜人
链接:https://www.jianshu.com/p/049f13e55840
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

猜你喜欢

转载自blog.csdn.net/xiunai78/article/details/89466681