[Linux] 使用 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。 

發佈留言