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

 方法1: 在/etc/sudoers文件内增加这么一行:Defaults secure_path=”/bin:/usr/bin:/usr/local/bin:…”, 把要用的命令path包括进去。

 方法2: 用命令的绝对路径。

 方法3: 使用sudo的env选项,像这样sudo env PATH=$PATH cmd.sh。

 方法4: 把脚本拷贝或链接到系统$PATH中。

 方法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的提示。

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

==================================================================================================

各位在 Linux 下使用 sudo 時,是否會出現 "command not found" 的錯誤訊息?

這是因為預裝的 sudo 在編譯時,--with-secure-path 採用了預設值,而導致原來 User 的 PATH 環境設置項,在 sudo 時被暫時覆蓋。
--with-secure-path 的詳盡說明如下:
Sudo Installation Notes
--with-secure-path[=PATH]

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. 有個簡單的方法可以解決這個問題,只要做個 alias 便可以了。   alias sudo=”sudo env PATH=$PATH” 這個 alias 可以增加 env PATH=$PATH 敘述,使得每次 sudo 時,可以套上原有的 PATH 環境變數。 當然,也可以透過 re-compile sudo package 而修改 secure path。 

=================================割割割割割割割割==============

在服务器上执行sudo xxx.sh(某个脚本…,使用非root帐户)时出错:”-bash: sudo: command not found”,
Google之,在http://blog.pcware.cn/it/linux/debian-sudo-command-not-found.html上看到几个解决方法.

  1. 在/etc/sudoers文件内增加这么一行:Defaults secure_path=”/bin:/usr/bin:/usr/local/bin:…”
  2. 使用脚本的完全路径,不是办法的办法.
  3. 使用sudo的env选项,像这样sudo env PATH=$PATH xxx.sh.
  4. 把脚本拷贝或链接到系统$PATH中.
  5. 重新编译sudo,不带–with-secure-path选项了.(终极解决办法)

至于出现这个问题的原因则是:在编译sudo包的时候默认开启了- -with-secure-path选项.

===========================================================

在使用ubuntu时发现一些奇怪的现象,明明定义了PATH,而且普通用户也可以调用,sudo却调用不了,报command not found的错误。

google之后知道是编译sudo的时候加入了–with-secure-path,这个选项。

–with-secure-path[=PATH]

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.


知道了问题就好解决了,要么重新编译sudo,要么在环境配置文件里加一个alias

alias  sudo = ' sudo  env  PATH = $PATH '
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐