Skip to content

sudo 执行指令时提示找不到命令 or "Command Not Found" Error When Using Sudo #121

Description

@lanlin

场景

当安装完某个程序,并且将 .bashrc 以及 .bash_profile 甚至是 /etc/bash_profile 的 PATH 都配置了一遍。
发现普通账户执行这个程序没有异常。但是 sudo 直接执行,和 sudo su root 后执行该程序,都提示找不到命令。

解决方法

  1. 使用 sudo visudo 指令打开 sudo 配置文件
  2. 搜索 Defaults secure_path =
  3. 在 2 中找到的这一行末尾加上你的程序bin路径,注意前面的冒号别掉了

以 golang 举例

# 添加前
Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin

# 添加后
Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/go/bin

原因

sudo 有自己单独的 $PATH,该路径由 sudo 配置文件中的的 Secure_Path 设置定义。
因此,往往手动安装的程序需要自己去添加上 bin 路径。

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions