我编写了一个简单的 bash 脚本来备份主目录,e 将其放入 /var/backups。由于该目录受到保护,因此我以 root 身份编写了脚本,然后设置了 SUID。
armando@ubuntu:~/scripts/bash $ ll
-rwsr-xr-x 1 root root 2596 Jul 28 10:43 homebackup.sh*
即便如此,当脚本尝试写入 /var/backups 时,我会收到“权限被拒绝”错误。为什么?
In Linux and most other modern UNIX-family systems, setuid bits are only recognized for direct binary executables, not scripts.
This is by design, and for security reasons. You can work around it by building a compiled wrapper for your setuid scripts, or using an existing tool (such as sudo
with a configuration to avoid needing a password when calling the specific script as the desired user).