Linux 使用 sudo 时找不到命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12996397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Command not found when using sudo
提问by Neko
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?
为什么会发生这种情况,我该如何解决?
采纳答案by Rob Kielty
Permission denied
没有权限
In order to run a script the file must have an executable permission bit set.
为了运行脚本,文件必须设置一个可执行权限位。
In order to fully understand Linux file permissionsyou 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.
为了充分了解 Linux文件权限,您可以研究该chmod
命令的文档。chmod是change mode的缩写,是用于更改文件权限设置的命令。
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 ...
要阅读本地系统的 chmod 文档,请运行man chmod
或info chmod
从命令行。阅读并理解后,您应该能够理解运行...的输出。
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 errorin 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. (Group and other only have read permission set on the file, they cannot write to it or execute it)
该chmod
命令修复了这个问题。(组和其他只对文件设置了读取权限,他们不能写入或执行它)
$ 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 sudoyou 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. Hence the command is not found.
根用户没有找到自己指令的原因,很可能是PATH
根环境变量不包括地方目录foo.sh
所在。因此找不到该命令。
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
PATH 环境变量包含用于搜索命令的目录列表。每个用户根据自己的需要设置自己的 PATH 变量。查看它设置为运行的内容
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
这是env
首先以普通用户身份然后以 root 用户身份使用 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.
请注意,虽然类似,但在这种情况下,非特权用户 (rkielty) 和超级用户 PATH 中包含的目录并不相同。
The directory where foo.sh
resides is not present in the PATH variable of the root user, hence the command not founderror.
其中目录foo.sh
所在是不存在于根用户的PATH变量,因此命令没有发现错误。
回答by Ed Heal
- Check that you have execute permission on the script. i.e.
chmod +x foo.sh
- Check that the first line of that script is
#!/bin/sh
or some such. - For sudo you are in the wrong directory. check with
sudo pwd
- 检查您是否对脚本具有执行权限。IE
chmod +x foo.sh
- 检查该脚本的第一行是否是
#!/bin/sh
这样的。 - 对于 sudo,您位于错误的目录中。检查
sudo pwd
回答by codemonkee
Check for secure_pathon 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
回答by Tom
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
.)
回答by Jeff MacDonald
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.
这是一个有点误导性的错误,但它在技术上可能是正确的。文件在可执行文件之前不是命令,因此无法找到。
回答by Fernando D Jaime
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
回答by TrigonaMinima
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. It'll then be available to the sudo.
您还可以/usr/local/bin
在超级用户 PATH中的目录之一(例如)中创建指向脚本的软链接。然后它将可供 sudo 使用。
chmod +x foo.sh
sudo ln -s path-to-foo.sh /usr/local/bin/foo
Have a look at this answerto have an idea of which directory to put soft link in.
看看这个答案,了解将软链接放入哪个目录。
回答by What's in a Google Search
Try chmod u+x foo.sh
instead of chmod +x foo.sh
if you have trouble with the guides above. This worked for me when the other solutions did not.
尝试chmod u+x foo.sh
,而不是chmod +x foo.sh
如果你有以上的导游麻烦。当其他解决方案没有时,这对我有用。
回答by IggyM
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