使用sudo时没有发现命令 [英] Command not found when using sudo

查看:148
本文介绍了使用sudo时没有发现命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

foo.sh 在我的主文件夹称为脚本。

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

-bash:./foo.sh:权限被拒绝

当我使用须藤./foo.sh ,我得到

须藤:foo.sh:找不到命令

为什么会出现这种情况,我该如何解决?

Why does this happen and how I can fix it?

推荐答案

权限被拒绝

为了运行一个脚本文件必须有可执行权限位设置

In order to run a script the file must have an executable permission bit set.

为了全面了解Linux的<一个href=\"http://www.gnu.org/software/coreutils/manual/html_node/File-permissions.html#File-permissions\">file权限你可以学习的搭配chmod 命令的文档。 CHMOD ,中的更改模式的缩写的,是用于改变的文件的权限设置命令

In order to fully understand Linux file permissions you can study the documentation for the chmod command. chmod, an abbreviation of change mode, is the command that is used to change the permission settings of a file.

要阅读您的本地系统,运行男人搭配chmod 信息CHMOD 在命令行中使用chmod文档。一旦阅读和理解,你应该能够理解运行的输出...

To read the chmod documentation for your local system , run man chmod or info chmod from the command line. 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")

下面是一个如何在你的情况排除在权限被拒绝错误摘要。

Here's a summary of how to troubleshoot the Permission Denied error in your case.

$ 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 

所有者具有读写访问RW,但 - 表示可执行权限丢失

Owner has read and write access rw but the - indicates that the executable permission is missing

搭配chmod 命令修复程序。 (集团等只拥有读取权限在文件上设置,都无法写入或执行它)

The chmod command fixes that. (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现在是可执行文件尽可能的Linux而言。

foo.sh is now executable as far as Linux is concerned.

命令中使用sudo结果没有找到

当您使用运行命令的须藤您正在运行的有效作为超级用户或根。

When you run a command using sudo you are effectively running it as the superuser or root.

这根用户没有找到自己命令的原因可能是root的 PATH 环境变量不包括的目录,其中 foo.sh 位于的。因此,命令不被发现。

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. Hence the command is not found.

PATH环境变量中包含有搜索的命令目录列表。每个用户根据自己的需要设置自己的PATH变量。
看看它设置为运行

The PATH environment variable contains a list of directories which are searched for commands. Each user sets their own PATH variable according to their needs. To see what it is set to run

env | grep ^PATH

下面是使用sudo首先作为一个普通用户,然后运行上面的 ENV 命令以root用户的​​一些示例输出

Here's some sample output of running the above env command first as an ordinary user and then as the root user using sudo

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

请注意,虽然类似的,在这种情况下包含在PATH非特权用户(rkielty)和超级用户的目录都的不一样的

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.

其中, foo.sh 所在的目录不在root用户的​​PATH变量present,因此命令没有找到的错误。

The directory where foo.sh resides is not present in the PATH variable of the root user, hence the command not found error.

这篇关于使用sudo时没有发现命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆