使用 sudo 时找不到命令 [英] Command not found when using sudo
问题描述
我的主文件夹中有一个名为 foo.sh
的脚本.
当我导航到这个文件夹并输入 ./foo.sh
时,我得到 p>
-bash: ./foo.sh: 权限被拒绝
.
当我使用 sudo ./foo.sh
时,我得到 p>
sudo: foo.sh: command not found
.
为什么会发生这种情况,我该如何解决?
权限被拒绝
为了运行脚本,文件必须设置了可执行权限位.
为了全面了解 Linux 文件权限可以研究 chmod
命令的文档.chmod,更改模式的缩写,是用于更改文件权限设置的命令.
要阅读本地系统的 chmod 文档,请从命令行运行 man chmod
或 info chmod
.阅读并理解后,您应该能够理解运行 ...
ls -l foo.sh
... 它将列出文件所有者、组所有者以及不是文件所有者或文件所属组成员的其他所有人的 READ、WRITE 和 EXECUTE 权限(最后一个权限组是有时也称为世界"或其他")
这里总结了如何对您的案例中的权限被拒绝错误进行故障排除.
$ ls -l foo.sh # 检查foo的文件权限-rw-r--r-- 1 rkielty 用户 0 2012-10-21 14:47 foo.sh^^^^^^ |^^^ ^^^^^^^ ^^^^^|||||业主|世界||||的名字集团 |团体的名字所有者
所有者有读写权限 rw 但 - 表示缺少可执行权限
chmod
命令修复了这个问题.(组和其他只对文件设置了读取权限,他们不能写入或执行它)
$ chmod +x foo.sh # 所有者可以设置foo.sh上的可执行权限$ ls -l foo.sh # 现在我们在 rw 后面看到一个 x-rwxr-xr-x 1 rkielty 用户 0 2012-10-21 14:47 foo.sh^ ^ ^
foo.sh 现在就 Linux 而言是可执行的.
使用 sudo 导致找不到命令
当您使用 sudo 运行命令时,您实际上是以超级用户或根用户身份运行它.
root 用户找不到您的命令的原因可能是 root 的 PATH
环境变量不包含 foo.sh
所在的 目录位于.因此找不到该命令.
PATH 环境变量包含搜索命令的目录列表.每个用户根据自己的需要设置自己的 PATH 变量.查看它设置为运行什么
env |grep ^路径
这是首先作为普通用户然后作为 root 用户使用 sudo 运行上述 env
命令的一些示例输出
rkielty@rkielty-laptop:~$ env |grep ^路径PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/gamesrkielty@rkielty-laptop:~$ sudo env |grep ^路径[sudo] rkielty 的密码:PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin
请注意,虽然类似,但在这种情况下,非特权用户 (rkielty) 和超级用户包含在 PATH 中的目录不同.
foo.sh
所在的目录不存在于 root 用户的 PATH 变量中,因此 command not found 错误.
I have a script called foo.sh
in my home folder.
When I navigate to this folder, and enter ./foo.sh
, I get
-bash: ./foo.sh: Permission denied
.
When I use sudo ./foo.sh
, I get
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. chmod, an abbreviation of change mode, is the command that is used to change the permission settings of a file.
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
Owner has read and write access rw but the - indicates that the executable permission is missing
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 is now executable as far as Linux is concerned.
Using sudo results in Command not found
When you run a command using sudo you are effectively running it as the superuser or 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. Hence the command is not found.
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
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
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.
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屋!