在 EC2 (Ubuntu) 上运行 bash 脚本时出现奇怪的“权限被拒绝”异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20138711/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:42:29  来源:igfitidea点击:

Weird "Permission denied" exception when running bash script on EC2 (Ubuntu)

linuxbashubuntuamazon-ec2

提问by xiaowl

I've attached an EBS block and mounted it at "/data" on my EC2 instance, which runs Ubuntu 12.04. There is a backup script which runs okay but after I move the script to "/data/backup" folder, it cannot be executed, with error:

我附加了一个 EBS 块并将其安装在运行 Ubuntu 12.04 的 EC2 实例上的“/data”。有一个备份脚本可以正常运行,但是在我将脚本移动到“/data/backup”文件夹后,它无法执行,并出现错误:

-bash: ./db_backup.sh: Permission denied

The permission is:

权限是:

-rwxr-xr-x 1 ubuntu ubuntu 2.3K Nov 22 03:25 db_backup.sh

If I run it with "sudo", there is no error and no output, but there should be some thing echo to console.

如果我用“sudo”运行它,没有错误也没有输出,但应该有一些东西回显到控制台。

And I also try use "strace" to follow the excecution, got:

我也尝试使用“strace”来跟踪执行,得到:

execve("./db_backup.sh", ["./db_backup.sh"], [/* 19 vars */]) = -1 EACCES (Permission   denied)
dup(2)                                  = 3
fcntl(3, F_GETFL)                       = 0x8002 (flags O_RDWR|O_LARGEFILE)
fstat(3, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 1), ...}) = 0
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) =     0x7f354f010000
lseek(3, 0, SEEK_CUR)                   = -1 ESPIPE (Illegal seek)
write(3, "strace: exec: Permission denied\n", 32strace: exec: Permission denied
) = 32
close(3)                                = 0
munmap(0x7f354f010000, 4096)            = 0
exit_group(1)                           = ?

But if I put this script somewhhere, say "/tmp", it works no problem. To narrow down the root cause, I also created a test script:

但是如果我把这个脚本放在某个地方,比如“/tmp”,它就没有问题。为了缩小根本原因,我还创建了一个测试脚本:

#!/bin/bash

echo "hello"

Sadly, this doesn't work too if I put it in the /data folder.

可悲的是,如果我把它放在 /data 文件夹中,这也不起作用。

回答by janos

Most probably it is mounted with the noexecflag on. You can confirm with:

很可能它是挂着noexec旗帜的。您可以通过以下方式确认:

mount | grep /data

In the output you will probably see something like (rw,noexec)at the end of the line. The noexecflag is not a default, so this usually happens when it is configured explicitly to mount it that way.

在输出中,您可能会(rw,noexec)在行尾看到类似的内容。该noexec标志不是默认值,因此当它被显式配置为以这种方式安装时通常会发生这种情况。

I don't know how you mount it, but there must be a configuration somewhere for this. You can probably change it, but maybe it's not such a good idea, these defaults exist for a reason. A better option is to run the script explicitly in bash or sh, like this:

我不知道你是如何安装它的,但必须有一个配置。您可能可以更改它,但也许这不是一个好主意,这些默认值的存在是有原因的。更好的选择是在 bash 或 sh 中显式运行脚本,如下所示:

sh db_backup.sh

UPDATE

更新

In some cases @Doc's comment might help too:

在某些情况下,@Doc的评论也可能有帮助:

I was dealing with the same sort of behaviour. I noticed in my /etc/fstab, even though I had "exec" in there explicitly for my partition, I also had "user" after it.. As soon as I removed user and remounted the partition, my users could exec on the partition.

我正在处理同样的行为。我在我的 /etc/fstab 中注意到,即使我的分区中有明确的“exec”,在它之后我也有“user”。一旦我删除了用户并重新安装了分区,我的用户就可以在划分。