問題: 在/data/testdir里創建的新文件自動屬于g1組,組g2的成員如:alice能對這些新文件有讀寫權限,組g3的成員如:tom只能對新文件有讀權限,其它用戶(不屬于g1,g2,g3)不能訪問這個文件夾。
groupadd g1 #創建g1組 groupadd g2 #創建g2組 groupadd g3 #創建g3組 makdir /data/testdir #創建/data/testdir目錄 chmod g+s /data/testdir #給目錄添加sgid權限 chmod g=rwx o= /data/testdir #更改目錄屬組權限為讀寫執行,其他人無權限 chown :g1 /data/testdir #更改/data/testdir屬組為g1 setfacl -m u:alice:rwx /data/testdir #為目錄設置alice的讀寫執行的facl權限 setfacl -m g:g3:rx data/testdir #設置目錄的組針對g3的facl權限
三種權限rwx對文件和目錄的不同意義
對于文件 r:可獲取文件的數據 通常是對文本文件有意義,對于二進制文件無意義。 w:可修改文件的數據 有寫權限但是不能刪除文件,但是可以清空文件。 x可將此文件運行為進程,普通文件沒有執行權限,也不應該有執行權限。 執行權限通常針對于可執行文件和腳本文件 對于目錄: 對于目錄一般給的全顯示r-x或rwx或--- r:可使用ls命令獲取其下的所有文件列表,但是不能訪問文件,不能cd,也不能查看文件的元數據,不支持ls -l不能查看文件的元數據。 只有讀權限 [user2@localhost ~]$ ll /testdir/ ls: cannot access /testdir/file3: Permission denied ls: cannot access /testdir/file2: Permission denied ls: cannot access /testdir/file1: Permission denied ls: cannot access /testdir/file4: Permission denied total 0 -????????? ? ? ? ? ? file1 -????????? ? ? ? ? ? file2 -????????? ? ? ? ? ? file3 -????????? ? ? ? ? ? file4 w:可以獲取此目錄下的文件列表,即創建刪除文件 只有寫權限 可以獲取目錄的列表但是不能使用ls -l,不能創建和刪除文件。 有寫和執行的權限 可創建,刪除文件。但是不能獲取目錄的文件列表 [user1@localhost ~]$ chmod 703 /testdir/ [user2@localhost testdir]$ touch /testdir/user2-file [user2@localhost testdir]$ cd /testdir/ [user2@localhost testdir]$ ls ls: cannot open directory .: Permission denied [user2@localhost testdir]$ ll -d /testdir/ drwx----wx 2 user1 user1 4096 Jul 25 11:39 / x:可以cd至此目錄中,可使用ls -l 沒有執行權限即使用戶有寫和讀的權限依然不能操作目錄下的文件。不能刪除更改內容。x代表可以進入目錄中。 只有執行權限,僅是可以cd進目錄,不能查看目錄列表,不能使用ls -l,可以訪問目錄下的文件,可以追加內容(寫入),但是不能刪除文件。
umask和acl mask 的區別和聯系
設置了mask后,除所有者其他人以外的用戶和組的facl權限都不能大于mask,可以小于等于,但是更改group的權限后就會更改mask值。 與umask的區別。umask的值是用戶不能默認創建文件的權限,并且權限必須是用777或666減去umask的值,精確匹配。不能小于更不能有與umask一樣的權限,必須等于。 相同點:都是限制用戶權限 umask限制用戶新建文件的默認權限。 mask限制除所有者和其他人以外的人的權限。 不同點:嚴格程度不同。 umask嚴格限制用戶的權限精確到三位全部權限。 mask除所有者和其他人以外的用戶和組的權限可以小于等于mask但是不能多于mask
三種特殊權限的應用場景和作用
suid
功能: 用戶運行某程序時,如果此程序擁有SUID的權限,程序以其屬主身份運行 應用場景:需要使用所有者身份運行但是不希望,其他人或組成員將系統環境切換至所有者身份運行。系統只有少量特殊程序如passd具有該權限。這是一個比較危險的操作。不建議將某些程序設置suid特別屬主是root的。
sgid
功能: 1、作用于目錄 一般用戶創建目錄時應用此權限的目錄,其他用戶在此目錄下創建文件的屬組為SGID組。不是用戶的主組 2、作用在二進制文件 是以進程發起者的身份運行。只是該進程繼承了該程序屬組的權限。 該程序,訪問文件時,所有者不匹配,開始匹配所屬組。匹配的是進程的屬組,而不是進程發起者的基本組。 應用場景: 作用于目錄的應用,便于同一組內成員之間的文件更改使用??梢灾皇峭唤M內成員間的讀寫執行。其他用戶可以無權限。
sticky
功能: 對于屬組或全局可寫的目錄組內的所有用戶或系統上的所有用戶在此目錄中都能創建新文件或刪除文件;如果為此類目錄設置sticky權限,則每個用戶能創建文件,且只能刪除自己的文件。 應用場景: 防止同一組下成員誤刪除同組人員的文檔,盡管可以沒有同組成員之間沒有寫權限,但是可以刪除、覆蓋。
設置user1,使之新建文件權限為rw——-
umask 177 臨時生效,僅對當前shell有效。 更改~/.bashr或~/.bash_profile 寫入umask 177
設置/testdir/f1的權限,使user1用戶不可以讀寫執行,g1組可以讀寫 /testdir/dir的權限,使新建文件自動具有acl權限:user1:rw,g1:— 備份/testdir目錄中所有文件的ACL,清除/testdir的所有ACL權限,并利用備份還原
設置權限 [root@localhost ~]# getfacl /testdir/ getfacl: Removing leading '/' from absolute path names # file: testdir/ # owner: root # group: g1 # flags: -s- user::rwx group::rwx other::--- default:user::rwx default:user:user1:rw- default:group::rwx default:group:g1:--- default:mask::rwx default:other::--- [root@localhost ~]# getfacl /testdir/f1 getfacl: Removing leading '/' from absolute path names # file: testdir/f1 # owner: root # group: g1 user::rw- user:user1:--- group::r-- group:g1:rw- mask::rw- other::r-- 備份acl [root@localhost ~]# getfacl /testdir/* > /testdir/acl.txt getfacl: Removing leading '/' from absolute path names [root@localhost ~]# ll /testdir/ total 4 -rw-rw----+ 1 root g1 116 Jul 25 13:19 acl.txt -rw-rw-r--+ 1 root g1 0 Jul 25 13:13 f1 [root@localhost ~]# cat /testdir/acl.txt # file: testdir/f1 # owner: root # group: g1 user::rw- user:user1:--- group::r-- group:g1:rw- mask::rw- other::r-- 刪除acl [root@localhost ~]# setfacl -Rb /testdir/ [root@localhost ~]# getfacl /testdir/ getfacl: Removing leading '/' from absolute path names # file: testdir/ # owner: root # group: g1 # flags: -s- user::rwx group::rwx other::--- [root@localhost ~]# getfacl /testdir/f1 getfacl: Removing leading '/' from absolute path names # file: testdir/f1 # owner: root # group: g1 user::rw- group::r-- other::r-- 恢復權限 [root@localhost ~]# setfacl -M /testdir/acl.txt /testdir/
原創文章,作者:yyw,如若轉載,請注明出處:http://www.www58058.com/29295