1、三種權限rwx對文件和目錄的不同意義 對文件 r:read 可讀取此文件的實際內容,如讀取文本文件的文字內容等 w: write 可以編輯,新增或修改該文件的內容 x:execute 該文件具有可以被系統執行的權限==>>針對二進制文件或腳本 X:針對目錄會加上x權限,如果是文件,本身沒有執行權限,那么也不會加上執行權限,對目錄不影響 d--------x 2 root test 14 Aug 4 14:15 1 d-------w- 2 root test 14 Aug 4 14:17 2 d------r-- 2 root test 14 Aug 4 14:17 3 -------r-- 1 root test 0 Aug 4 14:15 a --------w- 1 root root 0 Aug 4 14:52 b ---------x 1 root test 0 Aug 4 14:17 c [qiuwei@localhost qiuwei]$ cat a ==>可以訪問 [qiuwei@localhost qiuwei]$ cat b ==>無法訪問 cat: b: Permission denied [qiuwei@localhost qiuwei]$ cat c ==>無法訪問 cat: c: Permission denied [qiuwei@localhost qiuwei]$ echo 13 >a ==>無法修改 bash: a: Permission denied [qiuwei@localhost qiuwei]$ echo 13 >b ==>可以修改 [qiuwei@localhost qiuwei]$ echo 13 >c ==>無法修改 bash: c: Permission denied [qiuwei@localhost qiuwei]$ rm -rf a ==>可以刪除,因為上級目錄含wx權限 [qiuwei@localhost qiuwei]$ rm -rf b ==>可以刪除,因為上級目錄含wx權限 [qiuwei@localhost qiuwei]$ rm -rf c ==>可以刪除,因為上級目錄含wx權限 [qiuwei@localhost qiuwei]$ ll total 0 d--------x 2 root test 14 Aug 4 14:15 1 d-------w- 2 root test 14 Aug 4 14:17 2 d------r-- 2 root test 14 Aug 4 14:17 3
對目錄: x: execute 可以進入目錄,可以訪問目錄中的文件,但是看不到里面有什么文件 r:read 進不去目錄,可以看文件列表。不能訪問目錄內文件,文件的元數據不能查看 w: write 可以創建或刪除目錄中的文件,但是要匹配x權限 X:和x等價 假如對目錄有寫和執行,就可以刪除里面的任何文件,里面文件u=,也一樣
total 0 d--------x 2 root test 14 Aug 4 14:15 1 d-------w- 2 root test 14 Aug 4 14:17 2 d------r-- 2 root test 14 Aug 4 14:17 3 [qiuwei@localhost qiuwei]$ ll 1 ls: cannot open directory 1: Permission denied ==>x權限無法顯示文件列表 [qiuwei@localhost qiuwei]$ ll 2 ls: cannot open directory 2: Permission denied ==>w權限無法顯示文件列表 [qiuwei@localhost qiuwei]$ ll 3 ls: cannot access 3/a: Permission denied total 0 ?????????? ? ? ? ? ? a ==>r權限可以顯示文件列表,但是看不到元數據 [qiuwei@localhost qiuwei]$ cd 1 ==>x權限可以進入到目錄中 [qiuwei@localhost 1]$ cd .. [qiuwei@localhost qiuwei]$ cd 2 ==>w權限無法進入到目錄中 bash: cd: 2: Permission denied [qiuwei@localhost qiuwei]$ cd 3 ==>r權限無法進入到目錄中 bash: cd: 3: Permission denied [qiuwei@localhost qiuwei]$ cat 1/c ==>x權限可以訪問目錄下文件 [qiuwei@localhost qiuwei]$ cat 2/a ==>w權限無法訪問目錄下文件 cat: 2/a: Permission denied [qiuwei@localhost qiuwei]$ cat 3/a ==>r權限無法訪問目錄下文件 cat: 3/a: Permission denied [qiuwei@localhost qiuwei]$ rm -rf 1/c rm: cannot remove ‘1/c’: Permission denied [qiuwei@localhost qiuwei]$ rm -rf 2/a rm: cannot remove ‘2/a’: Permission denied [qiuwei@localhost qiuwei]$ rm -rf 3/a rm: cannot remove ‘3/a’: Permission denied 無法刪除創建文件 [qiuwei@localhost qiuwei]$ touch 1/b touch: cannot touch ‘1/b’: Permission denied [qiuwei@localhost qiuwei]$ touch 2/b touch: cannot touch ‘2/b’: Permission denied [qiuwei@localhost qiuwei]$ touch 3/b touch: cannot touch ‘3/b’: Permission denied
2.umask和acl,mask的區別和聯系
umask是針對屬主屬組other用戶的,用來設置新建文件權限的掩碼
umask的使用設定是掩碼,即在文件或目錄全部權限的基礎上,除去umask的權限,得到文件或目錄的權限,一般系統上使用unmask來設置新建文件的權限
ACL則是針對特殊用戶和組的,不是屬主屬組和other用戶的
ACL則是直接設置文件或目錄的權限,不會影響原文件上的權限,ACL用戶的權限獨立,只受ACL設置權限限制
mask則是針對除去屬主和other用戶之外的其他用戶
屬組和ACL特殊權限用戶的權限受到mask的限制,只能比mask低,如果某用戶權限比mask高,也會被屏蔽,不會擁有此權限,有效權限只能和mask權限一樣活著比mask低
3.三種特殊權限的應用場景和作用
SUID:s ===>只針對二進制文件可執行文件
在我們linux上,有很多重要文件權限設置的很低,不讓一般用戶隨意訪問,造成不必要的問題,例如/etc/shadow,權限為———,這個文件里面存放的是用戶的密碼等信息,及其重要,肯定不能讓普通用戶隨意訪問,但是我們普通用戶也要為自己修改密碼啊,所以SUID這個特殊權限就出現了,通俗講s權限就是當一個文件的屬主擁有s權限時,其他用戶執行此文件后,進程屬主將獲得此文件屬主的權限
在linux上我們通過執行passwd命令,來修改密碼的,那么過程究竟是怎樣的呢
passwd這個文件是有s權限的,使用普通用戶執行passwd命令后,此普通用戶暫時獲得passwd屬主(root)的權限,有了root用戶的權限,修改shadow文件當然是沒問題了
以上只是些文字描述,具體實際操作在上一篇博客中有,這里就不再做了
SGID:s ===>只針對二進制文件可執行文件
如果上面SUID明白了,這個SGID就好理解了,和上面原理一樣,
總結為:當我們執行一個屬組擁有s權限的文件時,用戶會暫時獲得此文件屬組的權限,并且以此權限去訪問一些其他的文件
SGID作用在目錄上時:
對目錄加上g+s,任何人不管是不是目錄所在組的成員,在里面創建文件時,該目錄里面的文件屬組繼承目錄的屬組
即作用在目錄上,將使在該目錄中新建目錄或文件將自動繼承該目錄所屬組
Skity :t ==>針對于目錄
當用戶對一個目錄擁有rw權限時,是可以刪除目錄里面的任何文件,無論該文件的權限或擁有者,為了避免這種情況,我們設置Skity,增加了此權限,只有文件的所有者或者root可以刪除該文件
4.設置user1,使之新建文件權限為rw——–
操作如下
[root@localhost qiuwei]# su qiuwei ==>切換到普通用戶qiuwe [qiuwei@localhost qiuwei]$ umask 166 ==>設置umask為166 [qiuwei@localhost qiuwei]$ umask 0166 [qiuwei@localhost qiuwei]$ touch test ==>創建文件test [qiuwei@localhost qiuwei]$ ll total 0 d--------x 2 root test 14 Aug 4 14:15 1 d------r-- 2 root test 14 Aug 4 14:17 3 -rw------- 1 qiuwei qiuwei 0 Aug 4 16:29 test ==>文件權限為rw-------
小練習:
? 1. 當用戶xiaoming 對/testdir 目錄無執行權限時,意味著無法
做哪些操作?
[root@localhost qiuwei]# su qiuwei [qiuwei@localhost qiuwei]$ ll total 0 drw-rw-rw- 2 root root 14 Aug 4 16:45 test [qiuwei@localhost qiuwei]$ ll total 0 drw-rw-rw- 2 root root 14 Aug 4 16:45 test [qiuwei@localhost qiuwei]$ ll test/ ls: cannot access test/a: Permission denied total 0 ?????????? ? ? ? ? ? a ==>能看到目錄里面的文件名,無法看到文件元數據 [qiuwei@localhost qiuwei]$ rm -rf test/a rm: cannot remove ‘test/a’: Permission denied ==>無法刪除目錄里面文件 [qiuwei@localhost qiuwei]$ cd test ==>無法進入到此目錄 bash: cd: test: Permission denied
2. 當用戶xiaoqiang 對/testdir 目錄無讀權限時,意味著無法做
哪些操作?
[qiuwei@localhost qiuwei]$ ll total 0 d-wx-wx-wx 2 root root 14 Aug 4 16:45 test [qiuwei@localhost qiuwei]$ cd test ==>可以進入目錄 [qiuwei@localhost test]$ cd .. [qiuwei@localhost qiuwei]$ ll test/ ls: cannot open directory test/: Permission denied ==>無法看到目錄里面的文件
?3. 當用戶wangcai 對/testdir 目錄無寫權限時,該目錄下的只
讀文件file1 是否可修改和刪除?
[qiuwei@localhost qiuwei]$ ll total 0 dr-xr-xr-x 2 root root 14 Aug 4 16:45 test ==>無寫權限 [qiuwei@localhost qiuwei]$ cd test/ [qiuwei@localhost test]$ ll total 0 -r--r--r-- 1 root root 0 Aug 4 16:45 a ==>只讀文件 [qiuwei@localhost test]$ rm -rf a rm: cannot remove ‘a’: Permission denied ==>無法刪除
?4. 復制/etc/fstab 文件到/var/tmp 下,設置文件所有者為
wangcai 讀寫權限,所屬組為sysadmins 組有讀寫權限,其他
人無權限
[root@localhost qiuwei]# cp /etc/fstab /var/tmp/ [root@localhost qiuwei]# chmod 660 /var/tmp/fstab [root@localhost qiuwei]# chown wangcai:sysadmins /var/tmp/fstab [root@localhost qiuwei]# ll /var/tmp/fstab -rw-rw---- 1 wangcai sysadmins 595 Aug 4 17:04 /var/tmp/fstab
5, 誤刪除了用戶wangcai 的家目錄,請重建并恢復該用戶家目錄
及相應的權限屬性
[root@localhost home]# cp -r qiuwei wangcai ==>復制配置文件 [root@localhost home]# ll [root@localhost home]# chown -R wangcai:wangcai wangcai ==>遞歸更改屬主和屬組 [root@localhost home]# ll -a wangcai total 32 drwx------ 5 wangcai wangcai 4096 Aug 4 17:11 . drwxr-xr-x. 18 root root 4096 Aug 4 17:11 .. -rw-r--r-- 1 wangcai wangcai 0 Aug 4 17:11 3 -rw------- 1 wangcai wangcai 3390 Aug 4 17:11 .bash_history -rw-r--r-- 1 wangcai wangcai 18 Aug 4 17:11 .bash_logout -rw-r--r-- 1 wangcai wangcai 193 Aug 4 17:11 .bash_profile -rw-r--r-- 1 wangcai wangcai 231 Aug 4 17:11 .bashrc drwxr-xr-x 3 wangcai wangcai 17 Aug 4 17:11 .cache drwxr-xr-x 3 wangcai wangcai 17 Aug 4 17:11 .config -rwxr-xr-x 1 wangcai wangcai 0 Aug 4 17:11 g -rw------- 1 wangcai wangcai 40 Aug 4 17:11 .lesshst drwxr-xr-x 4 wangcai wangcai 37 Aug 4 17:11 .mozilla -rwxr-xr-x 1 wangcai wangcai 0 Aug 4 17:11 shabi -rw------- 1 wangcai wangcai 596 Aug 4 17:11 .viminfo
6.在/data/testdir 里創建的新文件自動屬于g1 組,組g2 的成
員如:alice 能對這些新文件有讀寫權限,組g3 的成員如
:tom 只能對新文件有讀權限,其它用戶(不屬于
g1,g2,g3 )不能訪問這個文件夾
drwxr-xr-x 2 root root 6 Aug 3 17:48 testdir [root@localhost data]# chmod g+s testdir ==>屬組增加s權限 [root@localhost data]# chgrp g1 testdir ==>屬組更改為g1 [root@localhost data]# ll total 0 drwxr-sr-x 2 root g1 6 Aug 3 17:48 testdir [root@localhost data]# cd testdir/ [root@localhost testdir]# touch f1 [root@localhost testdir]# chmod 640 f1 [root@localhost testdir]# ll total 0 -rw-r----- 1 root g1 0 Aug 4 17:24 f1 ==>新建文件屬組為g1 [root@localhost testdir]# setfacl -m g:g2:rw f1 [root@localhost testdir]# setfacl -m g:g3:r f1 [root@localhost testdir]# su alice [alice@localhost testdir]$ echo haha >f1 ==>alice用戶可寫 [alice@localhost testdir]$ cat f1 ==>alice用戶可讀 haha [root@localhost testdir]# su tom [tom@localhost testdir]$ ll total 4 -rw-rw----+ 1 root g1 5 Aug 4 17:28 f1 [tom@localhost testdir]$ cat f1 ==>ton用戶可讀 haha [tom@localhost testdir]$ echo 456 >f1 ==>tom用戶無法寫 bash: f1: Permission denied [root@localhost testdir]# su qiuwei [qiuwei@localhost testdir]$ cat f1 ==> other無法讀 cat: f1: Permission denied [qiuwei@localhost testdir]$ echo 1111111 >f1 ==>other無法寫 bash: f1: Permission denied [qiuwei@localhost testdir]$
原創文章,作者:qiuwei,如若轉載,請注明出處:http://www.www58058.com/28885