1、 文件權(quán)限
- 更改文件的權(quán)限
[root@centos6 app]#touch f1
[root@centos6 app]#ll
total 0
-rw-r--r--. 1 root root 0 Jul 24 19:12 f1
[root@centos6 app]#chmod u=rw,g=r,o= f1 ---更改f1文件的權(quán)限為所有者rw,所屬組為r,其他人什么權(quán)限都沒有
[root@centos6 app]#ll
total 0
-rw-r-----. 1 root root 0 Jul 24 19:12 f1
[root@centos6 app]#chmod 755 f1 ---數(shù)字法更改文件的權(quán)限
[root@centos6 app]#ll
total 0
-rwxr-xr-x. 1 root root 0 Jul 24 19:12 f1
- 參考某個文件的屬性設(shè)置文件的權(quán)限
[root@centos6 app]#ll
total 0
-rwxr-xr-x. 1 root root 0 Jul 24 19:12 f1
[root@centos6 app]#chmod --reference=/etc/shadow f1 ---參考/etc/passwd 文件的屬性設(shè)置f1文件的屬性
[root@centos6 app]#ll
total 0
-r--------. 1 root root 0 Jul 24 19:12 f1
- X對目錄增加執(zhí)行權(quán)限,對原來沒有執(zhí)行權(quán)限的文件不增加執(zhí)行權(quán)限
[root@centos6 app]#ll
total 4
drw-r--r--. 4 root root 4096 Jul 24 19:25 dir
[root@centos6 app]#cd dir/
[root@centos6 dir]#ll
total 8
drw-r--r--. 2 root root 4096 Jul 24 19:25 dir1 ---無執(zhí)行權(quán)限
drw-r--r--. 2 root root 4096 Jul 24 19:25 dir2 ---無執(zhí)行權(quán)限
-rwxr--r--. 1 root root 0 Jul 24 19:25 f1 --- 所有者有執(zhí)行權(quán)限
-rw-r--r--. 1 root root 0 Jul 24 19:25 f2 --- 無執(zhí)行權(quán)限
[root@centos6 dir]#cd ..
[root@centos6 app]#chmod -R a+X dir --對dir這個目錄增加X執(zhí)行權(quán)限
[root@centos6 app]#ll
total 4
drwxr-xr-x. 4 root root 4096 Jul 24 19:25 dir
[root@centos6 app]#cd dir
[root@centos6 dir]#ll
total 8
drwxr-xr-x. 2 root root 4096 Jul 24 19:25 dir1
drwxr-xr-x. 2 root root 4096 Jul 24 19:25 dir2
-rwxr-xr-x. 1 root root 0 Jul 24 19:25 f1
-rw-r--r--. 1 root root 0 Jul 24 19:25 f2
總結(jié):對目錄增加X權(quán)限,則目錄內(nèi)的所有子目錄會增加執(zhí)行權(quán)限,目錄內(nèi)的文件不會增加執(zhí)行權(quán)限,除非文件原來就有執(zhí)行權(quán)限。
2、文件的默認(rèn)權(quán)限
對于文件=666-umask 根據(jù)結(jié)果,偶數(shù)不變,奇數(shù)加1
對于目錄=777-umask
- 設(shè)置默認(rèn)權(quán)限
[root@centos6 app]#umask 0042 ---設(shè)置umask值為偶數(shù)
[root@centos6 app]#touch fb
[root@centos6 app]#ll
total 4
drwxr-xr-x. 4 root root 4096 Jul 24 19:25 dir
-rw-r--r--. 1 root root 0 Jul 24 19:39 fa
-rw--w-r--. 1 root root 0 Jul 24 19:40 fb ---權(quán)限為666-042=624
[root@centos6 app]#umask 0135---umask設(shè)置為奇數(shù)
[root@centos6 app]#touch fc
[root@centos6 app]#ll
total 4
drwxr-xr-x. 4 root root 4096 Jul 24 19:25 dir
-rw-r--r--. 1 root root 0 Jul 24 19:39 fa
-rw--w-r--. 1 root root 0 Jul 24 19:40 fb
-rw-r---w-. 1 root root 0 Jul 24 19:41 fc ---權(quán)限為666-135=531 加1為642
[root@centos6 app]#mkdir dir4
[root@centos6 app]#ll
total 8
drwxr-xr-x. 4 root root 4096 Jul 24 19:25 dir
drw-r---w-. 2 root root 4096 Jul 24 19:42 dir4 ---權(quán)限為777-135=642
-rw-r--r--. 1 root root 0 Jul 24 19:39 fa
-rw--w-r--. 1 root root 0 Jul 24 19:40 fb
-rw-r---w-. 1 root root 0 Jul 24 19:41 fc
- umask直接設(shè)定默認(rèn)權(quán)限
[root@centos6 app]#umask u=rw,g=r,o=
[root@centos6 app]#touch f3
[root@centos6 app]#ll
total 8
drwxr-xr-x. 4 root root 4096 Jul 24 19:25 dir
drw-r---w-. 2 root root 4096 Jul 24 19:42 dir4
-rw-r-----. 1 root root 0 Jul 24 19:56 f3
-rw-r--r--. 1 root root 0 Jul 24 19:39 fa
-rw--w-r--. 1 root root 0 Jul 24 19:40 fb
-rw-r---w-. 1 root root 0 Jul 24 19:41 fc
- umask輸出可使用
[root@centos6 app]#umask -p
umask 0022
[root@centos6 app]#umask -p >> .bashrc或者/etc/bashrc
3、特殊權(quán)限
- suid權(quán)限
只能作用于可執(zhí)行的二進制程序上,繼承該程序所有者的權(quán)限
[root@centos6 app]#ll
total 0
-rw-r-----. 1 harry admins 0 Jul 24 20:03 f1
[root@centos6 app]#su - zhang
[zhang@centos6 ~]$id zhang
uid=500(zhang) gid=500(zhang) groups=500(zhang),0(root)
[zhang@centos6 ~]$cat /app/f1 ---對于zhang用戶來說屬于其他人,對f1文件沒有任何權(quán)限
cat: /app/f1: Permission denied
[zhang@centos6 ~]$exit
logout
[root@centos6 app]#chmod u+s /bin/cat
[root@centos6 app]#ll /bin/cat
-rwsr-xr-x. 1 root root 48568 Mar 23 02:52 /bin/cat ---對cat這個程序增加一個suid權(quán)限
[root@centos6 app]#su zhang
[root@centos6 app]#echo aaddcc >f1
[root@centos6 app]#su zhang
[zhang@centos6 app]$cat f1 ---繼承cat這個程序所有者即root的身份,root對f1文件具有讀寫權(quán)限
aaddcc
- sgid權(quán)限
①作用于可執(zhí)行的二進制程序上,繼承該程序所屬組的身份
②作用于目錄上,目錄內(nèi)新建文件的所屬組自動繼承目錄的所屬組
[zhang@centos6 app]$ll
total 4
-rw-r-----. 1 harry admins 7 Jul 24 20:16 f1 ---f1文件屬于admins組
[root@centos6 app]#chmod g+s /bin/cat ---加上sgid權(quán)限后繼承該程序所屬組的身份
[root@centos6 app]#ll /bin/cat
-rwxr-sr-x. 1 root admins 48568 Mar 23 02:52 /bin/cat ---cat程序?qū)儆赼dmins組
[root@centos6 app]#su zhang
[zhang@centos6 app]$cat f1
aaddcc
[root@centos6 app]#ll
total 4
drwxr-xr-x. 2 root zhang 4096 Jul 24 20:32 dir1
[root@centos6 app]#chmod g+s dir1/ ---增加sgid權(quán)限
[root@centos6 app]#ll
total 4
drwxr-sr-x. 2 root zhang 4096 Jul 24 20:32 dir1
[root@centos6 app]#cd dir1/
[root@centos6 dir1]#touch f{1,2}
[root@centos6 dir1]#ll
total 0
-rw-r--r--. 1 root zhang 0 Jul 24 20:33 f1 ---目錄內(nèi)新建文件的所屬組自動繼承目錄的所屬組
-rw-r--r--. 1 root zhang 0 Jul 24 20:33 f2
- sticky權(quán)限
作用于目錄上,目錄內(nèi)的文件只有目錄的所有者和root可以刪除其中的文件
[root@centos6 app]#chmod o+t dir1/ ---設(shè)置sticky權(quán)限
[root@centos6 app]#ll
total 4
drwxr-xrwt. 2 zhang zhang 4096 Jul 24 20:46 dir1
[root@centos6 app]#cd dir1/
[root@centos6 dir1]#ll
total 0
-rw-r--r--. 1 zhang zhang 0 Jul 24 20:33 f2
-rw-r--r--. 1 zhang root 0 Jul 24 20:46 f3
[root@centos6 dir1]#su zhang
[zhang@centos6 dir1]$rm -f f2 ---zhang屬于目錄的所屬組,可以刪f2文件
[zhang@centos6 dir1]$ll
total 0
-rw-r--r--. 1 zhang root 0 Jul 24 20:46 f3
[zhang@centos6 dir1]$exit
exit
[root@centos6 dir1]#su harry
[harry@centos6 dir1]$rm -f f3 ---harry不屬于目錄的所屬組,不能刪除f3文件
rm: cannot remove `f3': Operation not permitted
4、設(shè)置特定屬性
-chattr命令
[root@centos6 app]#chattr +i f1
[root@centos6 app]#lsattr f1
----i--------e- f1
[root@centos6 app]#rm -f f1
rm: cannot remove `f1': Operation not permitted
[root@centos6 app]#mv f1 /root
mv: cannot remove `f1': Operation not permitted
[root@centos6 app]#echo "abc" >f1
bash: f1: Permission denied
總結(jié):加上chattr+i后,即使root身份也不能對文件進行刪除,修改,重命名,可以避免誤操作。
[root@centos6 app]#chattr +a f1
[root@centos6 app]#lsattr
-----a-------e- ./f1
[root@centos6 app]#rm -f f1
rm: cannot remove `f1': Operation not permitted
[root@centos6 app]#mv f1 /root
mv: overwrite `/root/f1'? y
mv: cannot remove `f1': Operation not permitted
[root@centos6 app]#echo aaab>f1
bash: f1: Operation not permitted
[root@centos6 app]#echo aaddx>>f1
[root@centos6 app]#cat f1
ssaadc
aaddx
總結(jié):加上chattr+a后只能追加內(nèi)容,刪除,重命名和修改內(nèi)容都不可以。
5、ACL訪問控制列表
- 對用戶設(shè)置ACL權(quán)限
[root@centos6 app]#ll
total 0
-rw-r--r--. 1 root root 0 Jul 24 21:09 f1 ---張用戶只是其他人,沒有寫權(quán)限
[root@centos6 app]#setfacl -m u:zhang:rw f1 ---設(shè)置ACL 權(quán)限
[root@centos6 app]#su zhang
[zhang@centos6 app]$echo aaab>f1 ---可以寫了
[zhang@centos6 app]$cat f1
aaab
[zhang@centos6 app]$exit
exit
[root@centos6 app]#getfacl f1 ---查看ACL權(quán)限
# file: f1
# owner: root
# group: root
user::rw-
user:zhang:rw- ---zhang用戶有rw權(quán)限
group::r--
mask::rw-
other::r--
[root@centos6 app]#setfacl -x u:zhang f1 ---刪除ACL權(quán)限
[root@centos6 app]#getfacl f1
# file: f1
# owner: root
# group: root
user::rw-
group::r--
mask::r-- ---只刪除zhang用戶的ACL權(quán)限,mask沒有刪除,刪除不徹底
other::r--
[root@centos6 app]#setfacl -b f1 ---情況ACL 權(quán)限,mask也刪除
[root@centos6 app]#getfacl f1
# file: f1
# owner: root
# group: root
user::rw-
group::r--
other::r--
- 對組設(shè)置ACL權(quán)限
[root@centos6 app]#ll
total 4
-rw-r--r--. 1 root root 5 Jul 24 21:12 f1
[root@centos6 app]#id harry ---harry屬于admins組
uid=517(harry) gid=504(harry) groups=504(harry),515(admins)
[root@centos6 app]#setfacl -m g:admins:rw f1 ---對admins組設(shè)置ACL權(quán)限為rw
[root@centos6 app]#getfacl f1
# file: f1
# owner: root
# group: root
user::rw-
user:zhang:---
group::r--
group:admins:rw- ---設(shè)置成功
mask::rw-
other::r--
[root@centos6 app]#su harry
[harry@centos6 app]$echo 3344 >> f1 ---可以寫內(nèi)容
[harry@centos6 app]$cat f1
aaab
3344
- 設(shè)置mask權(quán)限-限高線
[root@centos6 app]#ll
total 8
-rw-rwxr--+ 1 root root 10 Jul 24 21:22 f1 ---所屬組的權(quán)限為rwx
[root@centos6 app]#setfacl -m mask::r f1 ---設(shè)置mask權(quán)限
[root@centos6 app]#getfacl f1
# file: f1
# owner: root
# group: root
user::rw-
user:zhang:---
user:harry:rwx #effective:r--
group::r--
group:admins:rw- #effective:r--
mask::r--
other::r--
[root@centos6 app]#ll
total 8
-rw-r--r--+ 1 root root 10 Jul 24 21:22 f1 ---設(shè)置mask后所屬組的權(quán)限為mask的權(quán)限
總結(jié):mask影響的范圍是除了所有者和其他人之外的人。
mask是個限高線,在這個范圍內(nèi)的人不允許超過這個線,設(shè)置mask后所屬組的權(quán)限就是mask的權(quán)限。
設(shè)置完mask后不要在更改ACL 權(quán)限,否則mask值會改變。