chmod命令(chmod函数)自动清除设置用户ID位和设置组ID位

《UNIX环境高级编程(第2版)》 4.9节

1.chmod自动清除“设置组ID位”
user3@SuSE11SP2-1:/home/user1/setgid> id

uid=1003(user3) gid=1003(group3) groups=1003(group3),16(dialout),33(video)

user3@SuSE11SP2-1:/home/user1/setgid>

user3@SuSE11SP2-1:/home/user1/setgid> ls -ld .

drwxr-srwx 2 user1 users 4096 Mar 20 12:40 .

user3@SuSE11SP2-1:/home/user1/setgid>

user3@SuSE11SP2-1:/home/user1/setgid> mkdir base_user3

user3@SuSE11SP2-1:/home/user1/setgid>

user3@SuSE11SP2-1:/home/user1/setgid> ls -ld base_user3/

drwxr-sr-x 2 user3 users 4096 Mar 20 12:40 base_user3/    # 设置组ID位生效,并且创建的目录继承了该位

user3@SuSE11SP2-1:/home/user1/setgid>

user3@SuSE11SP2-1:/home/user1/setgid> mkdir base_user3/sub_user3

user3@SuSE11SP2-1:/home/user1/setgid>

user3@SuSE11SP2-1:/home/user1/setgid> ls -ld base_user3/sub_user3/

drwxr-sr-x 2 user3 users 4096 Mar 20 12:41 base_user3/sub_user3/    # 继承的“设置组ID位”生效

user3@SuSE11SP2-1:/home/user1/setgid>

user3@SuSE11SP2-1:/home/user1/setgid> chmod o-x base_user3/

user3@SuSE11SP2-1:/home/user1/setgid>

user3@SuSE11SP2-1:/home/user1/setgid> ls -ld base_user3/

drwxr-xr-- 3 user3 users 4096 Mar 20 12:41 base_user3/    # 设置组ID位被自动关闭了

user3@SuSE11SP2-1:/home/user1/setgid>

user3@SuSE11SP2-1:/home/user1/setgid> mkdir base_user3/sub_user3.new

user3@SuSE11SP2-1:/home/user1/setgid>

user3@SuSE11SP2-1:/home/user1/setgid> ls -l base_user3/

total 8

drwxr-sr-x 2 user3 users  4096 Mar 20 12:41 sub_user3

drwxr-xr-x 2 user3 group3 4096 Mar 20 12:42 sub_user3.new

user3@SuSE11SP2-1:/home/user1/setgid>

2.写文件时,自动清除“设置用户ID位”
user3@SuSE11SP2-1:/home> ls -l setuid.txt

-rwSr--rw- 1 root root 0 Mar 20 13:05 setuid.txt

user3@SuSE11SP2-1:/home>

user3@SuSE11SP2-1:/home> echo "hello" >setuid.txt

user3@SuSE11SP2-1:/home>

user3@SuSE11SP2-1:/home> ls -l setuid.txt

-rw-r--rw- 1 root root 6 Mar 20 13:07 setuid.txt    # 设置用户ID位被清除

user3@SuSE11SP2-1:/home>

user3@SuSE11SP2-1:/home> ls -l setgid.txt

-rw-r-Srw- 1 root root 0 Mar 20 13:07 setgid.txt

user3@SuSE11SP2-1:/home>

user3@SuSE11SP2-1:/home> echo "hello" >setgid.txt

user3@SuSE11SP2-1:/home>

user3@SuSE11SP2-1:/home> ls -l setgid.txt

-rw-r-Srw- 1 root root 6 Mar 20 13:08 setgid.txt    # 设置ID位未被清除

user3@SuSE11SP2-1:/home>

user3@SuSE11SP2-1:/home> cat setgid.txt

hello

 

 

你可能感兴趣的:(chmod)