简体   繁体   English

使用 sudo 时找不到命令

[英]Command not found when using sudo

I have a script called foo.sh in my home folder.我的主文件夹中有一个名为foo.sh的脚本。

When I navigate to this folder, and enter ./foo.sh , I get当我导航到此文件夹并输入./foo.sh ,我得到

-bash: ./foo.sh: Permission denied . -bash: ./foo.sh: Permission denied

When I use sudo ./foo.sh , I get当我使用sudo ./foo.sh ,我得到

sudo: foo.sh: command not found . sudo: foo.sh: command not found

Why does this happen and how I can fix it?为什么会发生这种情况,我该如何解决?

Permission denied没有权限

In order to run a script the file must have an executable permission bit set .为了运行脚本,文件必须设置一个可执行权限位

In order to fully understand Linux file permissions you can study the documentation for the chmod command.为了全面了解 Linux 文件权限,您可以研究chmod命令的文档。 chmod , an abbreviation of change mode , is the command that is used to change the permission settings of a file. chmodchange mode的缩写,是用于更改文件权限设置的命令。

To read the chmod documentation for your local system , run man chmod or info chmod from the command line.要阅读本地系统的 chmod 文档,请从命令行运行man chmodinfo chmod Once read and understood you should be able to understand the output of running ...阅读并理解后,您应该能够理解运行...的输出

ls -l foo.sh

... which will list the READ, WRITE and EXECUTE permissions for the file owner, the group owner and everyone else who is not the file owner or a member of the group to which the file belongs (that last permission group is sometimes referred to as "world" or "other") ... 这将列出文件所有者、组所有者和不是文件所有者或文件所属组成员的其他所有人的 READ、WRITE 和 EXECUTE 权限(最后一个权限组有时称为作为“世界”或“其他”)

Here's a summary of how to troubleshoot the Permission Denied error in your case.以下是如何对您的案例中的Permission Denied 错误进行故障排除的摘要。

$ ls -l foo.sh                    # Check file permissions of foo
-rw-r--r-- 1 rkielty users 0 2012-10-21 14:47 foo.sh 
    ^^^ 
 ^^^ | ^^^   ^^^^^^^ ^^^^^
  |  |  |       |       | 
Owner| World    |       |
     |          |    Name of
   Group        |     Group
             Name of 
              Owner 

Owner has read and write access rw but the - indicates that the executable permission is missing所有者具有读写权限 rw 但 - 表示缺少可执行权限

The chmod command fixes that. chmod命令修复了这个问题。 (Group and other only have read permission set on the file, they cannot write to it or execute it) (组和其他只对文件设置了读取权限,他们不能写入或执行它)

$ chmod +x foo.sh               # The owner can set the executable permission on foo.sh
$ ls -l foo.sh                  # Now we see an x after the rw 
-rwxr-xr-x 1 rkielty users 0 2012-10-21 14:47 foo.sh
   ^  ^  ^

foo.sh is now executable as far as Linux is concerned.就 Linux 而言,foo.sh 现在是可执行的。

Using sudo results in Command not found在找不到命令中使用 sudo 结果

When you run a command using sudo you are effectively running it as the superuser or root.当您使用sudo运行命令时,您实际上是以超级用户或 root 用户身份运行它。

The reason that the root user is not finding your command is likely that the PATH environment variable for root does not include the directory where foo.sh is located . root 用户找不到你的命令的原因很可能是 root 的PATH环境变量不包括foo.sh所在目录 Hence the command is not found.因此找不到该命令。

The PATH environment variable contains a list of directories which are searched for commands. PATH 环境变量包含搜索命令的目录列表。 Each user sets their own PATH variable according to their needs.每个用户根据自己的需要设置自己的 PATH 变量。 To see what it is set to run查看它设置为运行的内容

env | grep ^PATH

Here's some sample output of running the above env command first as an ordinary user and then as the root user using sudo这是首先以普通用户身份然后以 root 用户身份使用 sudo 运行上述env命令的一些示例输出

rkielty@rkielty-laptop:~$ env | grep ^PATH
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

rkielty@rkielty-laptop:~$ sudo env | grep ^PATH
[sudo] password for rkielty: 
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin

Note that, although similar, in this case the directories contained in the PATH the non-privileged user (rkielty) and the super user are not the same .请注意,尽管类似,但在这种情况下,非特权用户 (rkielty) 和超级用户 PATH 中包含的目录并不相同

The directory where foo.sh resides is not present in the PATH variable of the root user, hence the command not found error. root 用户的 PATH 变量中不存在foo.sh所在的目录,因此找不到命令错误。

The other solutions I've seen here so far are based on some system definitions, but it's in fact possible to have sudo use the current PATH (with the env command) and/or the rest of the environment (with the -E option) just by invoking it right:到目前为止,我在这里看到的其他解决方案基于一些系统定义,但实际上可以让sudo使用当前的PATH (使用env命令)和/或环境的其余部分(使用-E选项)只需正确调用它:

sudo -E env "PATH=$PATH" <command> [arguments]

In fact, one can make an alias out of it:事实上,我们可以用它做一个别名:

alias mysudo='sudo -E env "PATH=$PATH"'

(It's also possible to name the alias itself sudo , replacing the original sudo .) (也可以将别名本身命名为sudo ,替换原来的sudo 。)

Check for secure_path on sudo检查 sudo 上的secure_path

[root@host ~]# sudo -V | grep 'Value to override'
Value to override user's $PATH with: /sbin:/bin:/usr/sbin:/usr/bin

If $PATH is being overridden use visudo and edit /etc/sudoers如果$PATH被覆盖,请使用visudo并编辑/etc/sudoers

Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
  1. Check that you have execute permission on the script.检查您是否对脚本具有执行权限。 ie chmod +x foo.shchmod +x foo.sh
  2. Check that the first line of that script is #!/bin/sh or some such.检查该脚本的第一行是否为#!/bin/sh或类似内容。
  3. For sudo you are in the wrong directory.对于 sudo,您位于错误的目录中。 check with sudo pwd检查sudo pwd

You can also create a soft link to your script in one of the directories ( /usr/local/bin for example) in the super user PATH.您还可以在超级用户 PATH 的目录之一(例如/usr/local/bin )中创建指向脚本的软链接。 It'll then be available to the sudo.然后它将可供 sudo 使用。

chmod +x foo.sh
sudo ln -s path-to-foo.sh /usr/local/bin/foo

Have a look at this answer to have an idea of which directory to put soft link in.看看这个答案,以了解将软链接放入哪个目录。

It seems that linux will say "command not found" even if you explicitly give the path to the file.即使您明确给出了文件的路径,Linux 似乎也会说“找不到命令”。

[veeam@jsandbox ~]$ sudo /tmp/uid.sh;echo $?
sudo: /tmp/uid.sh: command not found
1
[veeam@jsandbox ~]$ chmod +x /tmp/uid.sh
[veeam@jsandbox ~]$ sudo /tmp/uid.sh;echo $?
0

It's a somewhat misleading error, however it's probably technically correct.这是一个有点误导性的错误,但它在技术上可能是正确的。 A file is not a command until its executable, and so cannot be found.文件在可执行文件之前不是命令,因此无法找到。

Try chmod u+x foo.sh instead of chmod +x foo.sh if you have trouble with the guides above.如果您在上述指南中遇到问题,请尝试chmod u+x foo.sh而不是chmod +x foo.sh This worked for me when the other solutions did not.当其他解决方案没有时,这对我有用。

Ok this is my solution: in ~/.bash_aliases just add the following:好的,这是我的解决方案:在 ~/.bash_aliases 中添加以下内容:

# ADDS MY PATH WHEN SET AS ROOT
if [ $(id -u) = "0" ]; then
   export PATH=$PATH:/home/your_user/bin 
fi

Voila!瞧! Now you can execute your own scripts with sudo or set as ROOT without having to do an export PATH=$PATH:/home/your_user/bin everytime.现在您可以使用 sudo 执行您自己的脚本或设置为 ROOT ,而无需每次都执行 export PATH=$PATH:/home/your_user/bin 。

Notice that I need to be explicit when adding my PATH since HOME for superuser is /root请注意,在添加我的 PATH 时我需要明确,因为超级用户的 HOME 是 /root

There are excellent answers above. 上面有很好的答案。 If, after trying them, you still get command not found try again with the entire file path: 如果在尝试使用它们之后仍然command not found ,请使用整个文件路径再试一次:

sudo /home/user/path/to/foo.sh

Regarding "command not found" when using sudo far less hackier way would be to edit secure_path.关于使用 sudo 时“未找到命令”的问题,更简单的方法是编辑 secure_path。

It is perfectly described here: https://superuser.com/questions/927512/how-to-set-path-for-sudo-commands这里有完美的描述: https : //superuser.com/questions/927512/how-to-set-path-for-sudo-commands

In Ubuntu, right click on the executable file and choose Properties , go to the Permissions tab and highlight Allow executing file as program在 Ubuntu 中,右键单击可执行文件并选择Properties ,转到Permissions选项卡并突出显示Allow executing file as program 在此处输入图像描述

It seems sudo command not found 似乎没有找到sudo命令

to check whether the sudo package is installed on your system, type sudo , and press Enter .要检查您的系统上是否安装了 sudo 软件包,请键入sudo ,然后按 Enter 。 If you have sudo installed the system will display a short help message, otherwise you will see something like sudo: command not found如果您安装了sudo ,系统将显示一条简短的帮助消息,否则您将看到类似sudo: command not found

To install sudo, run one of the following commands using root account:要安装 sudo,请使用 root 帐户运行以下命令之一:

apt-get install sudo # If your system based on apt package manager apt-get install sudo # 如果你的系统基于 apt 包管理器

yum install sudo # If your system based on yum package manager yum install sudo # 如果你的系统基于 yum 包管理器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM