为什么未设置可执行位时root不能执行?

mus*_*usa 27 bash root permissions executable

root即使未设置权限,用户也可以写入文件write

root即使未设置权限,用户也可以读取文件read

root即使未设置权限,用户也可以 cd进入目录execute

root未设置权限时,用户无法执行文件execute

为什么?

user$ echo '#!'$(which bash) > file
user$ chmod 000 file
user$ ls -l file
---------- 1 user user 12 Jul 17 11:11 file
user$ cat file                      # Normal user cannot read
cat: file: Permission denied
user$ su
root$ echo 'echo hello' >> file     # root can write
root$ cat file                      # root can read
#!/bin/bash
echo hello
root$ ./file                        # root cannot execute
bash: ./file: Permission denied
Run Code Online (Sandbox Code Playgroud)

Sha*_*dur 26

简而言之,因为执行位被认为是特殊的;如果它没有设置在所有的,那么该文件被认为是不是一个可执行文件,因此不能执行。

但是,即使设置了一个执行位,root 也可以并且将执行它。

观察:

caleburn: ~/ >cat hello.sh
#!/bin/sh

echo "Hello!"

caleburn: ~/ >chmod 000 hello.sh
caleburn: ~/ >./hello.sh
-bash: ./hello.sh: Permission denied
caleburn: ~/ >sudo ./hello.sh 
sudo: ./hello.sh: command not found

caleburn: ~/ >chmod 100 hello.sh
caleburn: ~/ >./hello.sh
/bin/sh: ./hello.sh: Permission denied
caleburn: ~/ >sudo ./hello.sh 
Hello!
Run Code Online (Sandbox Code Playgroud)