2018-02-04 文件權限

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值會改變褒纲。

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末准夷,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子莺掠,更是在濱河造成了極大的恐慌冕象,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,607評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件汁蝶,死亡現(xiàn)場離奇詭異渐扮,居然都是意外死亡,警方通過查閱死者的電腦和手機掖棉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評論 3 395
  • 文/潘曉璐 我一進店門墓律,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人幔亥,你說我怎么就攤上這事耻讽。” “怎么了帕棉?”我有些...
    開封第一講書人閱讀 164,960評論 0 355
  • 文/不壞的土叔 我叫張陵针肥,是天一觀的道長。 經常有香客問我香伴,道長慰枕,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,750評論 1 294
  • 正文 為了忘掉前任即纲,我火速辦了婚禮具帮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己蜂厅,他們只是感情好匪凡,可當我...
    茶點故事閱讀 67,764評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著掘猿,像睡著了一般病游。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上稠通,一...
    開封第一講書人閱讀 51,604評論 1 305
  • 那天衬衬,我揣著相機與錄音,去河邊找鬼采记。 笑死佣耐,一個胖子當著我的面吹牛政勃,可吹牛的內容都是我干的唧龄。 我是一名探鬼主播,決...
    沈念sama閱讀 40,347評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼奸远,長吁一口氣:“原來是場噩夢啊……” “哼既棺!你這毒婦竟也來了?” 一聲冷哼從身側響起懒叛,我...
    開封第一講書人閱讀 39,253評論 0 276
  • 序言:老撾萬榮一對情侶失蹤丸冕,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后薛窥,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體胖烛,經...
    沈念sama閱讀 45,702評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,893評論 3 336
  • 正文 我和宋清朗相戀三年诅迷,在試婚紗的時候發(fā)現(xiàn)自己被綠了佩番。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,015評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡罢杉,死狀恐怖趟畏,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情滩租,我是刑警寧澤赋秀,帶...
    沈念sama閱讀 35,734評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站律想,受9級特大地震影響猎莲,放射性物質發(fā)生泄漏。R本人自食惡果不足惜技即,卻給世界環(huán)境...
    茶點故事閱讀 41,352評論 3 330
  • 文/蒙蒙 一益眉、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦郭脂、人聲如沸年碘。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,934評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽屿衅。三九已至,卻和暖如春莹弊,著一層夾襖步出監(jiān)牢的瞬間涤久,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,052評論 1 270
  • 我被黑心中介騙來泰國打工忍弛, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留响迂,地道東北人。 一個月前我還...
    沈念sama閱讀 48,216評論 3 371
  • 正文 我出身青樓细疚,卻偏偏與公主長得像蔗彤,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子疯兼,可洞房花燭夜當晚...
    茶點故事閱讀 44,969評論 2 355

推薦閱讀更多精彩內容

  • 一.文件權限屬性 (1) [root@pc root]# ls -l install.log-rw-r--r--...
    楠人幫閱讀 988評論 0 0
  • 一然遏、USERS UID為0的用戶為管理員用戶,任何一個UID為0的用戶吧彪,都可為管理員用戶若系統(tǒng)沒有了UID=0的管...
    JevonWei閱讀 2,219評論 0 2
  • 1.安全AAA(3A) Authentication:認證Authorization:授權Accouting|Au...
    尛尛大尹閱讀 1,441評論 0 2
  • 1待侵、 文件權限 更改文件的權限 參考某個文件的屬性設置文件的權限 X對目錄增加執(zhí)行權限,對原來沒有執(zhí)行權限的文件不...
    張大志的博客閱讀 299評論 0 0
  • “愿你走過半生姨裸,別回來了……” 年輕的時候秧倾,最討厭被人說: “他好幼稚……” “還像小孩一樣……” “一點也不成熟...
    談希言閱讀 562評論 3 11