1剑令、 文件權限
- 更改文件的權限
[root@localhost mnt]#touch t1
[root@localhost mnt]#ll
total 12
drwxr-xr-x. 2 root root 4096 Jan 9 06:17 hgfs
-rwxr--r--. 1 root root 1251 Feb 1 11:35 ip.sh
-rw-r--r--. 1 root root 2761 Feb 1 11:27 ks.cfg
-rw-r--r--. 1 root root 0 Feb 4 16:09 t1
[root@localhost mnt]#chmod u=rwx,g=rw,o= t1 ---更改t1文件的權限為所有者rwx舌镶,所屬組為rw柱彻,其他人什么權限都沒有
[root@localhost mnt]#ll
total 12
drwxr-xr-x. 2 root root 4096 Jan 9 06:17 hgfs
-rwxr--r--. 1 root root 1251 Feb 1 11:35 ip.sh
-rw-r--r--. 1 root root 2761 Feb 1 11:27 ks.cfg
-rwxrw----. 1 root root 0 Feb 4 16:09 t1
[root@localhost mnt]#chmod 755 t1 ---數(shù)字法更改文件的權限
[root@localhost mnt]#ll
total 12
drwxr-xr-x. 2 root root 4096 Jan 9 06:17 hgfs
-rwxr--r--. 1 root root 1251 Feb 1 11:35 ip.sh
-rw-r--r--. 1 root root 2761 Feb 1 11:27 ks.cfg
-rwxr-xr-x. 1 root root 0 Feb 4 16:09 t1
[root@localhost mnt]#chmod o-x t1 ---通過加減號更改文件的權限
[root@localhost mnt]#ll
total 12
drwxr-xr-x. 2 root root 4096 Jan 9 06:17 hgfs
-rwxr--r--. 1 root root 1251 Feb 1 11:35 ip.sh
-rw-r--r--. 1 root root 2761 Feb 1 11:27 ks.cfg
-rwxr-xr--. 1 root root 0 Feb 4 16:09 t1
- 參考某個文件的屬性設置文件的權限
[root@localhost mnt]#ll
total 12
drwxr-xr-x. 2 root root 4096 Jan 9 06:17 hgfs
-rwxr--r--. 1 root root 1251 Feb 1 11:35 ip.sh
-rw-r--r--. 1 root root 2761 Feb 1 11:27 ks.cfg
-rwxr-xr--. 1 root root 0 Feb 4 16:09 t1
[root@localhost mnt]#chmod --reference=/etc/passwd t1
[root@localhost mnt]#ll
total 12
drwxr-xr-x. 2 root root 4096 Jan 9 06:17 hgfs
-rwxr--r--. 1 root root 1251 Feb 1 11:35 ip.sh
-rw-r--r--. 1 root root 2761 Feb 1 11:27 ks.cfg
-rw-r--r--. 1 root root 0 Feb 4 16:09 t1 ---參考/etc/passwd 文件的屬性設置t1文件的屬性
- X對目錄增加執(zhí)行權限,對原來沒有執(zhí)行權限的文件不增加執(zhí)行權限
[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í)行權限
drw-r--r--. 2 root root 4096 Jul 24 19:25 dir2 ---無執(zhí)行權限
-rwxr--r--. 1 root root 0 Jul 24 19:25 f1 --- 所有者有執(zhí)行權限
-rw-r--r--. 1 root root 0 Jul 24 19:25 f2 --- 無執(zhí)行權限
[root@centos6 dir]#cd ..
[root@centos6 app]#chmod -R a+X dir --對dir這個目錄增加X執(zhí)行權限
[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
總結:對目錄增加X權限餐胀,則目錄內的所有子目錄會增加執(zhí)行權限哟楷,目錄內的文件不會增加執(zhí)行權限,除非文件原來就有執(zhí)行權限否灾。
2卖擅、文件的默認權限
對于文件=666-umask 根據(jù)結果,偶數(shù)不變墨技,奇數(shù)加1
對于目錄=777-umask
- 設置默認權限
[root@centos6 app]#umask 0042 ---設置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 ---權限為666-042=624
[root@centos6 app]#umask 0135---umask設置為奇數(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 ---權限為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 ---權限為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直接設定默認權限
[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惩阶、特殊權限
- suid權限
只能作用于可執(zhí)行的二進制程序上,繼承該程序所有者的權限
[root@localhost ~]#cd /tmp
[root@localhost tmp]#ll
total 16
-rw-r--r--. 1 root root 7 Feb 2 00:21 abc
-rw-r--r--. 1 root root 22 Feb 2 12:53 t1
-rw-r-----. 1 idc test 12 Feb 4 21:11 t3
drwx------. 2 root root 4096 Feb 4 21:10 vmware-root
[root@localhost tmp]#su - zheng
[zheng@localhost ~]$id zheng
uid=501(zheng) gid=501(zheng) groups=501(zheng)
[zheng@localhost ~]$cat /tmp/t3 ---對于t3來說扣汪,zheng用戶屬于其他人断楷,對t3文件沒有任何權限
cat: /tmp/t3: Permission denied
[zheng@localhost ~]$su -
Password:
[root@localhost ~]#chmod u+s /bin/cat
[root@localhost ~]#ll /bin/cat ---對cat這個程序增加一個suid權限
-rwsr-xr-x. 1 root root 48568 May 11 2016 /bin/cat
[root@localhost ~]#su - zheng
[zheng@localhost ~]$cat /tmp/t3 ---繼承cat這個程序所有者即root的身份,root對t3文件具有讀寫權限
hello world
- sgid權限
①作用于可執(zhí)行的二進制程序上崭别,繼承該程序所屬組的身份
②作用于目錄上脐嫂,目錄內新建文件的所屬組自動繼承目錄的所屬組
[root@localhost tmp]#ll
total 16
-rw-r--r--. 1 root root 7 Feb 2 00:21 abc
-rw-r--r--. 1 root root 22 Feb 2 12:53 t1
-rw-r-----. 1 idc test 12 Feb 4 21:11 t3 ---t3文件屬于test組
drwx------. 2 root root 4096 Feb 4 21:45 vmware-root
[root@localhost ~]#chmod g+s /bin/cat ---加上sgid權限后繼承該程序所屬組的身份
[root@localhost ~]#ll /bin/cat
-rwxr-sr-x. 1 root test 48568 May 11 2016 /bin/cat ---cat程序屬于test組
[root@localhost ~]#su - zheng
[zheng@localhost ~]$cat /tmp/t3
hello world
[root@localhost tmp]#ll
total 20
-rw-r--r--. 1 root root 7 Feb 2 00:21 abc
drwxr-xr-x. 2 root zheng 4096 Feb 4 22:09 dir1
-rw-r--r--. 1 root root 22 Feb 2 12:53 t1
-rw-r-----. 1 idc test 12 Feb 4 21:11 t3
drwx------. 2 root root 4096 Feb 4 22:05 vmware-root
[root@localhost tmp]#chmod g+s dir1/ ---增加sgid權限
[root@localhost tmp]#ll
total 20
-rw-r--r--. 1 root root 7 Feb 2 00:21 abc
drwxr-sr-x. 2 root zheng 4096 Feb 4 22:09 dir1
-rw-r--r--. 1 root root 22 Feb 2 12:53 t1
-rw-r-----. 1 idc test 12 Feb 4 21:11 t3
drwx------. 2 root root 4096 Feb 4 22:10 vmware-root
[root@localhost tmp]#cd dir1/
[root@localhost dir1]#touch {1,2}
[root@localhost dir1]#ll
total 0
-rw-r--r--. 1 root zheng 0 Feb 4 22:12 1 ---目錄內新建文件的所屬組自動繼承目錄的所屬組
-rw-r--r--. 1 root zheng 0 Feb 4 22:12 2
- sticky權限
作用于目錄上,目錄內的文件只有目錄的所有者和root可以刪除其中的文件
[root@localhost tmp]#chmod o+t dir1/ ---設置sticky權限
[root@localhost tmp]#ll
total 20
-rw-r--r--. 1 root root 7 Feb 2 00:21 abc
drwxr-sr-t. 2 zheng zheng 4096 Feb 4 22:12 dir1
-rw-r--r--. 1 root root 22 Feb 2 12:53 t1
-rw-r-----. 1 idc test 12 Feb 4 21:11 t3
drwx------. 2 root root 4096 Feb 5 00:00 vmware-root
[root@localhost tmp]#cd dir1/
[root@localhost dir1]#ll
total 0
-rw-r--r--. 1 zheng zheng 0 Feb 4 22:12 1
-rw-r--r--. 1 zheng root 0 Feb 4 22:12 2
[root@localhost dir1]#su - zheng
[zheng@localhost ~]$cd /tmp/dir1/
[zheng@localhost dir1]$rm -f 1 ---zheng屬于目錄的所有者紊遵,可以刪1文件
[zheng@localhost dir1]$ll
total 0
-rw-r--r--. 1 zheng root 0 Feb 4 22:12 2
[zheng@localhost dir1]$su - idc
Password:
[idc@localhost ~]$cd /tmp/dir1/
[idc@localhost dir1]$rm -f 2 ---idc不屬于目錄的所有者账千,不能刪除f3文件
rm: cannot remove `2': Permission denied
4、設置特定屬性
- chattr命令
[root@localhost tmp]#ll
total 20
-rw-r--r--. 1 root root 7 Feb 2 00:21 abc
drwxr-sr-t. 2 zheng zheng 4096 Feb 5 00:09 dir1
-rw-r--r--. 1 root root 22 Feb 2 12:53 t1
-rw-r-----. 1 idc test 12 Feb 4 21:11 t3
drwx------. 2 root root 4096 Feb 5 20:10 vmware-root
[root@localhost tmp]#chattr +i t1
[root@localhost tmp]#lsattr t1
----i--------e- t1
[root@localhost tmp]#rm -f t1
rm: cannot remove `t1': Operation not permitted
[root@localhost tmp]#mv t1 /mnt
mv: cannot move `t1' to `/mnt/t1': Operation not permitted
[root@localhost tmp]#echo "hello world" > t1
-bash: t1: Permission denied
總結:加上chattr+i后暗膜,即使root身份也不能對文件進行刪除匀奏,修改,重命名学搜,可以避免誤操作娃善。
[root@localhost tmp]#chattr +a t1
[root@localhost tmp]#lsattr t1
-----a-------e- t1
[root@localhost tmp]#rm -f t1
rm: cannot remove `t1': Operation not permitted
[root@localhost tmp]#mv t1 /mnt
mv: cannot move `t1' to `/mnt/t1': Operation not permitted
[root@localhost tmp]#echo "hello world" >> t1
[root@localhost tmp]#echo "hello world" > t1
-bash: t1: Operation not permitted
[root@localhost tmp]#cat t1
localhost.localdomain
hello world
總結:加上chattr+a后只能追加內容论衍,刪除,重命名和修改內容都不可以聚磺。
[root@localhost tmp]#lsattr t1
----ia-------e- t1
[root@localhost tmp]#chattr -a t1
[root@localhost tmp]#lsattr t1
----i--------e- t1
[root@localhost tmp]#chattr -i t1
[root@localhost tmp]#lsattr t1
-------------e- t1
總結:可以用chattr-a或-i來取消前面的+i或+a操作坯台。
5、ACL訪問控制列表
- 對用戶設置ACL權限
[root@localhost tmp]#ll
total 20
-rw-r--r--. 1 root root 7 Feb 2 00:21 abc ---zheng用戶只是其他人瘫寝,沒有寫權限
drwxr-sr-t. 2 zheng zheng 4096 Feb 5 00:09 dir1
-rw-r--r--. 1 root root 34 Feb 5 20:23 t1
-rw-r-----. 1 idc test 12 Feb 4 21:11 t3
drwx------. 2 root root 4096 Feb 5 21:45 vmware-root
You have new mail in /var/spool/mail/root
[root@localhost tmp]#setfacl -m u:zheng:rw abc ---設置ACL 權限
[root@localhost tmp]#su - zheng
[zheng@localhost ~]$cd /tmp
[zheng@localhost tmp]$echo aaa > abc ---可以寫入了
[zheng@localhost tmp]$cat abc
aaa
[zheng@localhost tmp]$su -
Password:
[root@localhost ~]#cd /tmp
[root@localhost tmp]#getfacl abc ---查看ACL權限
# file: abc
# owner: root
# group: root
user::rw-
user:zheng:rw- ---zheng用戶有rw權限
group::r--
mask::rw-
other::r--
[root@localhost tmp]#setfacl -x u:zheng abc ---刪除ACL權限
[root@localhost tmp]#getfacl abc
# file: abc
# owner: root
# group: root
user::rw-
group::r--
mask::r-- ---只刪除zheng用戶的ACL權限蜒蕾,mask沒有刪除,刪除不徹底
other::r--
[root@localhost tmp]#setfacl -b abc ---清空ACL 權限焕阿,mask也刪除
[root@localhost tmp]#getfacl abc
# file: abc
# owner: root
# group: root
user::rw-
group::r--
other::r--
- 對組設置ACL權限
[root@localhost tmp]#ll
total 20
-rw-r--r--. 1 root root 4 Feb 5 21:48 abc
drwxr-sr-t. 2 zheng zheng 4096 Feb 5 00:09 dir1
-rw-r--r--. 1 root root 34 Feb 5 20:23 t1
-rw-r-----. 1 idc test 12 Feb 4 21:11 t3
drwx------. 2 root root 4096 Feb 5 21:55 vmware-root
[root@localhost tmp]#id idc ---idc屬于test組
uid=500(idc) gid=500(idc) groups=500(idc),502(test),503(test1),504(test2),505(test3)
[root@localhost tmp]#setfacl -m g:test:rw abc ---對test組設置ACL權限為rw
[root@localhost tmp]#getfacl abc
# file: abc
# owner: root
# group: root
user::rw-
group::r--
group:test:rw- ---設置成功
mask::rw-
other::r--
[root@localhost tmp]#su idc
[idc@localhost tmp]$echo abcd >> abc ---可以寫內容
[idc@localhost tmp]$cat abc
aaa
abcd
- 設置mask權限-限高線
[root@localhost tmp]#ll
total 24
-rw-rw-r--+ 1 root root 9 Feb 5 21:57 abc ---所屬組的權限為rw
drwxr-sr-t. 2 zheng zheng 4096 Feb 5 00:09 dir1
-rw-r--r--. 1 root root 34 Feb 5 20:23 t1
-rw-r-----. 1 idc test 12 Feb 4 21:11 t3
drwx------. 2 root root 4096 Feb 5 22:53 vmware-root
[root@localhost tmp]#setfacl -m mask::r abc ---設置mask權限
[root@localhost tmp]#getfacl abc
# file: abc
# owner: root
# group: root
user::rw-
group::r--
group:test:rw- #effective:r--
mask::r--
other::r--
[root@localhost tmp]#ll
total 24
-rw-r--r--+ 1 root root 9 Feb 5 21:57 abc ---設置mask后所屬組的權限為mask的權限
drwxr-sr-t. 2 zheng zheng 4096 Feb 5 00:09 dir1
-rw-r--r--. 1 root root 34 Feb 5 20:23 t1
-rw-r-----. 1 idc test 12 Feb 4 21:11 t3
drwx------. 2 root root 4096 Feb 5 22:53 vmware-root
總結:mask影響的范圍是除了所有者和其他人之外的人咪啡。
mask是個限高線,在這個范圍內的人不允許超過這個線暮屡,設置mask后所屬組的權限就是mask的權限撤摸。
設置完mask后不要在更改ACL 權限,否則mask值會改變褒纲。