解释 linux 文件 s 权限.
s 权限的作用:表示对文件具用可执行权限的用户将使用文件拥有者的权限或文件拥有者所在组的权限在对文件进行执行.
s 权限的设置:4,用户拥有者的执行权限位, 6,用户组的执行权限位, 2, 两者都设置, 0, 两者都不设置.
假设有文件 a
-rw-rw---- 1 tony tony 4 Apr 10 21:27 a
tony 用户拥有此文件,文件权限如上行.
有程序如下 test.cpp:
#include < fstream > #include < iostream > using namespace std;
int main() {
fstream s("./a", ios: :in|ios: :out);
s << "123" << endl;
}
可执行文件为 test, 其权限为
-rwxrwx--x 1 tony tony 13712 Apr 10 21:11 test
tony@ubuntu:~/test$ su shw
Password:
shw@ubuntu:/home/tony/test$ ls
a test test.cpp
shw@ubuntu:/home/tony/test$ ls -l test
-rwxrwx--x 1 tony tony 13712 Apr 15 16:11 test
shw@ubuntu:/home/tony/test$
现切换到用户 shw, 此用户不在 tony 组,对于 test 具有可执行权限,对 a 没有任何权限.
此时用 shw 来执行 test 会发生什么呢?
执行后查看文件 a,发现 a 没有被写会任何内容.
也就是说 shw 虽然对 test 有可执行权限,但是 shw 对 a 没有写的权限,故执行失败.
这时有三个办法来解决:
1. 用超级用户来执行, 不安全
2. 使用 test 对 a 具有相应权限, 但如何 test 操作了 n 个文件 , 则需要对这 n 个文件都修改权限.
3. s 权限.
现在来设置 s 权限,使 shw 在执行 test 时使用 tony 的权限.
tony@ubuntu:~/test$ chmod 4771 test
tony@ubuntu:~/test$ ls -l
total 24
-rw-rw---- 1 tony tony 4 Apr 15 16:27 a
-rwsrwx--x 1 tony tony 13712 Apr 15 16:11 test
-rw-rw-r-- 1 tony tony 133 Apr 15 16:11 test.cpp
tony@ubuntu:~/test$
此时 test 的执行权限位为 s.
再使用 shw 来执行 test, 查看文件 a , 发现 123 已写入.
来源: