Pio*_*app 75 bash permissions mount shell-script
当我尝试跑步时./script.sh我得到了Permission denied但是当我跑步时bash script.sh一切都很好。
我做错了什么?
Ant*_*gan 73
这意味着您没有为 设置执行权限位script.sh。运行时bash script.sh,您只需要读取权限script.sh。请参阅运行“bash script.sh”和“./script.sh”有什么区别?了解更多信息。
您可以通过运行来验证这一点ls -l script.sh。
您甚至可能不需要启动一个新的 Bash 进程。在许多情况下,您可以简单地在当前交互式 shell 中运行source script.sh或. script.sh运行脚本命令。如果脚本更改当前目录或以其他方式修改当前进程的环境,您可能希望启动一个新的 Bash 进程。
如果 POSIX 权限位设置正确,则访问控制列表 (ACL) 可能已配置为阻止您或您的组执行该文件。例如,POSIX 权限将表明测试 shell 脚本是可执行的。
$ ls -l t.sh
-rwxrwxrwx+ 1 root root 22 May 14 15:30 t.sh
Run Code Online (Sandbox Code Playgroud)
但是,尝试执行该文件会导致:
$ ./t.sh
bash: ./t.sh: Permission denied
Run Code Online (Sandbox Code Playgroud)
该getfacl命令显示了原因:
$ getfacl t.sh
# file: t.sh
# owner: root
# group: root
user::rwx
group::r--
group:domain\040users:rw-
mask::rwx
other::rwx
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我的主要组是domain users通过使用sudo setfacl -m 'g:domain\040users:rw-' t.sh. 可以通过以下任一命令解除此限制:
sudo setfacl -m 'g:domain\040users:rwx' t.sh
sudo setfacl -b t.sh
Run Code Online (Sandbox Code Playgroud)
看:
Finally, the reason in this specific case for not being able to run the script is that the filesystem the script resides on was mounted with the noexec option. This option overrides POSIX permissions to prevent any file on that filesystem from being executed.
This can be checked by running mount to list all mounted filesystems; the mount options are listed in parentheses in the entry corresponding to the filesystem, e.g.
/dev/sda3 on /tmp type ext3 (rw,noexec)
Run Code Online (Sandbox Code Playgroud)
You can either move the script to another mounted filesystem or remount the filesystem allowing execution:
sudo mount -o remount,exec /dev/sda3 /tmp
Run Code Online (Sandbox Code Playgroud)
Note: I’ve used /tmp as an example here since there are good security reasons for keeping /tmp mounted with the noexec,nodev,nosuid set of options.
小智 59
尝试
chmod +rx script.sh
这将使文件可执行。那就试试吧
./script.sh
希望这会奏效。