linux文件權限

1.文件權限概述

linux文件權限

2.權限位說明

linux文件或目錄的權限位是由9個權限位來控制的,每三位一組,它們分別是文件屬主(Owner)的讀、寫、執行,用戶組(Group)的讀、寫、執行以及其餘用戶(Other)的讀、寫、執行。linux

r   read  可讀權限 對應數字4

w  write 可寫權限 對應數字2

x  (Execute,執行權限)對應數字1

`-` (沒有任何權限) 對應數字0

3.實戰案例

3.1 演示環境

用戶環境準備shell

[root@linzhongniao ~]# groupadd nishishei  一個家庭
[root@linzhongniao ~]# useradd linzhongniao -g nishishei 主人
[root@linzhongniao ~]# useradd lisi -g nishishei
useradd: user 'lisi' already exists

若是提示建立的用戶已經存在能夠用usermod修改用戶所在用戶組安全

[root@linzhongniao ~]# usermod lisi -g nishishei 家庭成員
[root@linzhongniao ~]# useradd test  外面的人
[root@linzhongniao ~]# id linzhongniao
uid=509(linzhongniao) gid=509(nishishei) groups=509(nishishei)
[root@linzhongniao ~]# id lisi
uid=504(lisi) gid=509(nishishei) groups=509(nishishei)
[root@linzhongniao ~]# id test
uid=510(test) gid=510(test) groups=510(test)

文件測試準備bash

[root@linzhongniao ~]# mkdir /linzhongniao -p
[root@linzhongniao ~]# echo "echo linux">/linzhongniao/test.sh
[root@linzhongniao ~]# chmod +x /linzhongniao/test.sh 
[root@linzhongniao ~]# cat /linzhongniao/test.sh 
echo linux
[root@linzhongniao ~]# ls -l /linzhongniao/test.sh 
 -rwxr-xr-x. 1 root root 11 Jul 26 23:05 /linzhongniao/test.sh

3.2修改文件的屬主和所屬組

修改文件的屬主和所屬組用chown命令ide

格式chown 用戶.(或者:)用戶組 文件/目錄測試

[root@linzhongniao ~]# chown linzhongniao.nishishei /linzhongniao/test.sh 
[root@linzhongniao ~]# ls -l /linzhongniao/test.sh
 -rwxr-xr-x. 1 linzhongniao nishishei 11 Jul 26 23:05 /linzhongniao/test.sh

linzhongniao用戶和nishishei用戶組的成員擁有對應屬主的權限,即讀,寫和執行的權限。
登陸lisi用戶能夠查看和執行test.sh,由於lisi屬於nishishei用戶組,屬於這個組的只有讀和執行的權限,沒有刪除和修改的權限。優化

4.普通文件權限及結論總結

(1)可讀r:表示具備讀取、閱讀文件內容的權限。ui

(2)可寫w:表示具備新增、修改刪除文件(刪除必須有執行權限)內容的權限,若是沒有r的配合,那麼vi編輯文件會提示沒法編輯(但可強制編輯,echo 能夠重定向或追加)。code

總結:有讀和寫(r和w)的權限配合,能夠用vi編輯文件,不配合也能vi編輯,保存要強制保存。
有寫和執行(w和x)權限的配合,能夠刪除文件。繼承

(3)特別提示:刪除文件(修改文件名)的權限是受父目錄的權限控制的,和文件自己權限無關。若是想刪除文件應該給上一級目錄的用戶或者用戶組可寫和執行的權限權限。

[linzhongniao@linzhongniao ~]$ ls -ld /linzhongniao/test.sh 
 -rwxr-xr-x. 1 linzhongniao nishishei 13 Jul 27 19:25 /linzhongniao/test.sh
[linzhongniao@linzhongniao ~]$ ls -ld /linzhongniao/
drwxr-xr-x. 2 root root 4096 Jul 27 19:25 /linzhongniao/
[linzhongniao@linzhongniao ~]$ rm -rf /linzhongniao/test.sh 
rm: cannot remove `/linzhongniao/test.sh': Permission denied
permitted

沒有權限刪除,因此要給文件上一級所在目錄的其餘用戶添加可寫權限

[root@linzhongniao ~]# chmod o+w /linzhongniao/
[root@linzhongniao ~]# ls -ld /linzhongniao/
drwxr-xrwx. 2 root root 4096 Jul 27 19:25 /linzhongniao/
[linzhongniao@linzhongniao ~]$ ls -ld /linzhongniao/
drwxr-xrwx. 2 root root 4096 Jul 27 19:25 /linzhongniao/
[linzhongniao@linzhongniao ~]$ rm -rf /linzhongniao/test.sh

(4)root只要有x的權限就能執行。

5.目錄的權限及總結

5.1可讀r

可讀的權限,須要和x執行權限配合使用才能查看目錄或文件的內容

(1)若是沒有x權限不能進到目錄裏,即沒法cd dir

(2)若是沒有x權限,ls查看列表時是能夠看到全部的文件名,可是會提示無權限訪問目錄下文件。

(3)若是ls –l 查看列表時全部屬性會帶有問號,也會提示沒法訪問目錄下的文件,可是能夠看到文件名。

[root@linzhongniao ~]# touch /linzhongniao/test/tmp.txt
[root@linzhongniao ~]# ls -ld /linzhongniao/test/
drwxrwxr-x. 2 root root 4096 Jul 27 20:16 /linzhongniao/test/

舉例:可讀權限r和執行權限x的使用

將其餘用戶只給可讀權限

[root@linzhongniao ~]# chmod o=r /linzhongniao/test/
[root@linzhongniao ~]# ls -ld /linzhongniao/test/   
drwxrwxr--. 2 root root 4096 Jul 27 20:16 /linzhongniao/test/

切換到test用戶測試,以下所示是不能查看和進入到目錄裏面去的

[test@linzhongniao ~]$ ls -ld /linzhongniao/test/tmp.txt
ls: cannot access /linzhongniao/test/tmp.txt: Permission denied
[test@linzhongniao ~]$ cd /linzhongniao/test/
 -bash: cd: /linzhongniao/test/: Permission denied
[test@linzhongniao ~]$ ls /linzhongniao/test/
ls: cannot access /linzhongniao/test/tmp.txt: Permission denied
tmp.txt

給上級目錄的其餘用戶可執行權限,就能夠查看文件內容了

[root@linzhongniao ~]# chmod o=rx /linzhongniao/test/ 
[root@linzhongniao ~]# ls -ld /linzhongniao/test/ 
drwxrwxr-x. 2 root root 4096 Jul 27 20:20 /linzhongniao/test/
[test@linzhongniao ~]$ ls /linzhongniao/test/
tmp.txt
[test@linzhongniao ~]$ cd /linzhongniao/test/
[test@linzhongniao test]$ pwd
/linzhongniao/test
[test@linzhongniao test]$ whoami
test

5.2可寫w

w表示具備建立、增長、刪除或修改目錄內文件內容的權限(須要x權限配合)。

[root@linzhongniao ~]# chmod o=x /linzhongniao/test/ 
[root@linzhongniao ~]# ls -ld /linzhongniao/test/
drwxrwx--x. 2 root root 4096 Jul 27 20:20 /linzhongniao/test/
[test@linzhongniao test]$ touch ddd.txt
touch: cannot touch `ddd.txt': Permission denied
[test@linzhongniao test]$ ls
ls: cannot open directory .: Permission denied

5.3可執行x權限

表示具備進入目錄和執行的權限,例如:cd dir。可是沒有讀取r權限沒法顯示列表文件和目錄,以及不能進入到目錄中。沒有可寫權限不能刪除修改編輯文件內容。有執行腳本的權限。

[root@linzhongniao ~]# chmod o=x /linzhongniao/test/ 
[root@linzhongniao ~]# ls -ld /linzhongniao/test/
drwxrwx--x. 2 root root 4096 Jul 27 20:20 /linzhongniao/test/
[test@linzhongniao ~]$ cd /linzhongniao/test/

提示:可讀權限r和可寫權限w都要和執行x權限配合纔可以查看和編輯修改目錄或文件的內容。

6.改變權限屬性命令chmod

chmod - change file mode bits
[root@linzhongniao ~]# which chmod
/bin/chmod

chmod是用來改變文件或目錄權限的命令,但只有文件的屬主和超級用戶纔有這種權限,經過chmod來改變文件或目錄的權限有兩種方法:一種是經過權限字母和操做符表達式的方法來設置權限,另一種是使用數字的方法來設置權限,數字方法是比較經常使用的。

若是咱們想改變的僅僅是第一個打開的目錄的權限時,使用chmod命令時不用加任何參數,若是想把目錄下的全部文件或子目錄也同時改變,須要使用 –R參數。

6.1 chmod數字權限表示法

[root@linzhongniao ~]# chmod 755 /linzhongniao/test/
[root@linzhongniao ~]# ls -ld /linzhongniao/test/ 
drwxr-xr-x. 2 root root 4096 Jul 27 20:20 /linzhongniao/test/

6.2 chmod字符式權限表示法

chmod 用戶類型 [+|-|=] 權限字符 文件名

用表格表示以下

linux文件權限

演示:

[root@linzhongniao ~]# ls -ld test.sh 
 -rw-r--r--. 1 root root 25 Jul 27 21:52 test.sh

屬主給執行權限

[root@linzhongniao ~]# chmod u+x test.sh 
[root@linzhongniao ~]# ls -ld test.sh
 -rwxr--r--. 1 root root 25 Jul 27 21:52 test.sh

屬組給執行權限

[root@linzhongniao ~]# chmod g+x test.sh  
[root@linzhongniao ~]# ls -ld test.sh
 -rwxr-xr--. 1 root root 25 Jul 27 21:52 test.sh

屬主取消可讀權限,屬組和其餘用戶給執行權限,中間能夠用「,」逗號分隔

[root@linzhongniao ~]# chmod u-r,g+x,o+x test.sh 
[root@linzhongniao ~]# ls -ld test.sh
 --wxr-xr-x. 1 root root 25 Jul 27 21:52 test.sh
[root@linzhongniao ~]# chmod ugo=r test.sh 
[root@linzhongniao ~]# ls -ld test.sh
 -r--r--r--. 1 root root 25 Jul 27 21:52 test.sh
[root@linzhongniao ~]# chmod a=xr test.sh
[root@linzhongniao ~]# ls -ld test.sh
 -r-xr-xr-x. 1 root root 25 Jul 27 21:52 test.sh

6.3 默認權限分配的命令umask(瞭解便可)

爲何linux的默認目錄權限是755,而文件的默認權限是644呢?

這些都是由umask值來控制的,它在建立文件的時候由一段腳原本控制作判斷。

[root@linzhongniao ~]# umask
0022
[root@linzhongniao ~]# sed -n '65,69p' /etc/bashrc 
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
   umask 002
else
   umask 022
fi

6.3.1 根據umask值來計算文件權限的方法

(1)簡單好用的加減法計算(並不是是數學裏面的加減法運算)

用666減去umask設置的035等於631,凡是獲得的數的個位爲奇數位,個位加1;同理目錄的權限計算方法也是同樣的用777減去umask值。

[root@linzhongniao ~]# umask 035
[root@linzhongniao ~]# touch fff
[root@linzhongniao ~]# ll fff
 -rw-r---w-. 1 root root 0 Jul 27 22:34 fff

umask設置的值都爲偶數直接相減

[root@linzhongniao ~]# umask 044
[root@linzhongniao ~]# touch hhh
[root@linzhongniao ~]# ll hhh
 -rw--w--w-. 1 root root 0 Jul 27 22:37 hhh

(2)永久修改umask值(瞭解)

[root@linzhongniao ~]# sed -n '65,69p' /etc/bashrc 
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
   umask 002
else
   umask 022
fi

6.4 setuid和setqid粘滯位

linux系統基本權限位爲9位權限,但還有額外3位權限位,共12位權限。粘滯位像tmp目錄同樣,是整個系統臨時文件的的存放地,一個目錄即便它的全部權限都開放rwxrwxrw,若是設置了粘滯位,除非目錄的屬主和root用戶有權刪除它除此以外的用戶不能刪除。能夠工做可是不能刪除。

suid   s(有x)   S(沒有x)   4  用戶對應的權限位(用戶對應的3位上)

sgid   s(有x)   S(沒有x)2  用戶組對應的權限位(用戶組對應的3位上)

sticky  t(有x)   T(沒有x)   1  其餘用戶對應的權限位(其餘用戶對應的3位上)

例如:

[root@linzhongniao ~]# find /usr/bin -type f -perm 4755 -exec ls -l {} \;
 -rwsr-xr-x. 1 root root 22544 Mar 17  2015 /usr/bin/pkexec
 -rwsr-xr-x. 1 root root 2438408 Jun 19 23:16 /usr/bin/Xorg
 -rwsr-xr-x. 1 root root 54464 Mar 22  2017 /usr/bin/at
 -rwsr-xr-x. 1 root root 30768 Nov 24  2015 /usr/bin/passwd
 -rwsr-xr-x. 1 root root 70480 May 11  2016 /usr/bin/chage
 -rwsr-xr-x. 1 root root 75640 May 11  2016 /usr/bin/gpasswd
 -rwsr-xr-x. 1 root root 40240 May 11  2016 /usr/bin/newgrp
 -rwsr-xr-x. 1 root root 51784 Aug 24  2016 /usr/bin/crontab

6.4.1 suid知識小結

suid是針對命令和二進制程序的

(1)用戶或屬主對應的前三位權限的x位上若是有s就表示suid權限。當x位上沒有小寫x執行權限的時候,suid的權限顯示的就是大S。

(2)suid做用是讓普通用戶能夠以root(或其餘)的用戶角色運行,只有root(或其餘)帳號才能運行的程序或命令以及程序命令對應原本沒有權限操做的文件等(注意和su及sudo的區別)。suid是爲某一個命令設置特殊權限(使用者爲全部人)。

(3)問題:但願linzhongniao用戶可以刪除原本無權刪除的文件。

解答:給rm命令的屬主設置suid權限

[root@linzhongniao ~]# ls -ld /linzhongniao/
drwxr-xrwx. 3 root root 4096 Jul 27 20:16 /linzhongniao/
[root@linzhongniao ~]# chmod 755 /linzhongniao/

設置權限位755,其餘用戶沒有刪除的權限

[root@linzhongniao ~]# ls -ld /linzhongniao/
drwxr-xr-x. 3 root root 4096 Jul 27 20:16 /linzhongniao/

切換到linzhongniao用戶,刪除/linzhongniao/test/下面的文件,會提示沒有權限

[linzhongniao@linzhongniao test]$ whoami
linzhongniao
[linzhongniao@linzhongniao test]$ ls
ddddd  tmp.txt
[linzhongniao@linzhongniao test]$ rm -f tmp.txt 
rm: cannot remove `tmp.txt': Permission denied

給rm命令設置suid權限,看linzhongniao用戶能不能刪除

[root@linzhongniao ~]# ls -l `which rm|tail -1`
 -rwxr-xr-x. 1 root root 57440 Jun 19 23:15 /bin/rm
[root@linzhongniao ~]# chmod u+s /bin/rm
[root@linzhongniao ~]# ls -l `which rm|tail -1`
 -rwsr-xr-x. 1 root root 57440 Jun 19 23:15 /bin/rm

切換到linzhongniao用戶,再刪除文件就能夠刪除了

[linzhongniao@linzhongniao test]$ whoami
linzhongniao
[linzhongniao@linzhongniao test]$ pwd
/linzhongniao/test
[linzhongniao@linzhongniao test]$ rm -f tmp.txt

(4)suid修改的是執行的命令passwd,而不是處理的目標文件/etc/shadow

[root@linzhongniao test]# touch test.sh
[root@linzhongniao test]# chmod u+s test.sh 
[root@linzhongniao test]# ls -l test.sh 
 -rwSr--r--. 1 root root 0 Jul 28 09:25 test.sh

(5)僅對二進制命令程序有效不能用在shell等相似腳本文件上(由於shell腳本僅僅是調用二進制命令程序而已,所以具體權限還須要看二進制命令自己)。

(6)二進制命令程序須要有可執行權限x配合。

(7)suid權限只在程序命令執行過程當中有效。

(8)執行suid命令的任意系統用戶均可以得到該命令程序在執行期間對應的擁有者的全部權限。

(9)suid是一把雙刃劍,是一個比較危險的功能,對系統安全有必定的威脅。系統suid的無用的功能取消suid權利(安全優化)。

6.4.2 sgid知識小結

sgid是針對用戶組的權限位

對於文件來講,sgid的功能以下:

(1)與suid不一樣的是,sgid既能夠針對文件也能夠針對目錄設置。

(2)二進制命令或程序須要有可執行權限x。

(3)執行命令的任意用戶能夠得到該命令程序執行期間所屬組的權限。

對於目錄來講,sgid的功能以下:

(1)linux裏默認狀況全部用戶建立文件,默認用戶和組都是自身。

[linzhongniao@linzhongniao ~]$ touch sss
[linzhongniao@linzhongniao ~]$ ls -l sss
 -rw-r--r--. 1 linzhongniao nishishei 0 Jul 28 11:02 sss

(2)能夠爲目錄設置sgid,讓超級用戶root建立屬於目錄屬主或者屬組的文件。

6.4.3 sgid基於locate命令文件的案例

讓用戶沒有使用locate查找命令路徑的權限

[root@linzhongniao ~]# ls -l `which locate`
 -rwx--s--x. 1 root slocate 38464 Mar 12  2015 /usr/bin/locate
[root@linzhongniao ~]# chmod g-s  `which locate`
[root@linzhongniao ~]# ls -l `which locate` 
 -rwx--x--x. 1 root slocate 38464 Mar 12  2015 /usr/bin/locate
[root@linzhongniao ~]# su - linzhongniao
[linzhongniao@linzhongniao ~]$ locate cat
locate: can not stat () `/var/lib/mlocate/mlocate.db': Permission denied

6.4.4 setgid基於目錄的案例

RHCE認證考試題建立共享目錄

建立共享目錄/home/admins:屬組爲adminuser,adminuser組成員對目錄有寫入讀寫和執行的權限,其餘全部用戶沒有任何權限(root除外);在/home/admins目錄中建立文件會自動繼承adminuser組,文件只有屬主能夠刪除。

[root@linzhongniao ~]# mkdir -p /home/admins
[root@linzhongniao ~]# groupadd adminuser
[root@linzhongniao ~]# ls -ld /home/admins
drwxr-xr-x. 2 root root 4096 Jul 28 11:19 /home/admins
[root@linzhongniao ~]# chown .adminuser /home/admins
[root@linzhongniao ~]# ls -ld /home/admins  
drwxr-xr-x. 2 root adminuser 4096 Jul 28 11:19 /home/admins
[root@linzhongniao ~]# touch /home/admins/test

雖然是在/home/admins/下建立的,可是root用戶建立的至關於root本身的孩子屬主和屬組仍是root;須要設置setgid,讓root建立屬於別的組的孩子。

[root@linzhongniao ~]# chmod g+s /home/admins/
[root@linzhongniao ~]# touch /home/admins/linzhongniao.txt
[root@linzhongniao ~]# ll /home/admins/linzhongniao.txt
 -rw-r--r--. 1 root adminuser 0 Jul 28 11:24 /home/admins/linzhongniao.txt

7.改變文件所屬關係命令chown

chown(change owner)

當咱們要改變一個文件的屬主或者屬組,咱們所使用的用戶必須是該文件的屬主並且同時是目標屬組的成員,或超級用戶。只有超級用戶才能改變文件的屬主。

chown語法:

chown [選項]…[全部者](:或.)[組] 文件

若是想把目錄下的全部內容修改屬主和屬組加-R參數。

改變屬組也能夠用chgrp命令,推薦使用chown

[root@linzhongniao ~]# chgrp kkk text.txt 
[root@linzhongniao ~]# ls -l text.txt
 -rw-r--r--. 1 root kkk 10 Aug  6 11:01 text.txt

8.改變文件屬性chattr

chattr改變文件屬性,lsattr顯示設置的文件屬性

(1) i參數鎖定目錄或文件,+i鎖定目錄,-i取消鎖定

鎖定passwd文件,鎖定以後就不能建立用戶了

[root@linzhongniao ~]# chattr +i /etc/passwd
[root@linzhongniao ~]# lsattr /etc/passwd     
 ----i--------e- /etc/passwd
[root@linzhongniao ~]# useradd dddd 
useradd: cannot open /etc/passwd
[root@linzhongniao ~]# chattr -i /etc/passwd 
[root@linzhongniao ~]# useradd dddd
[root@linzhongniao ~]# id dddd
uid=511(dddd) gid=512(dddd) groups=512(dddd)

(2)a參數只能追加不能刪除

只能向文件中追加內容,不能刪除和編輯。注意:儘管是root用戶也只能追加,不能刪除。

[root@linzhongniao ~]# chattr +a text.txt 
[root@linzhongniao ~]# lsattr  text.txt
 -----a-------e- text.txt
[root@linzhongniao ~]# rm -f text.txt 
rm: cannot remove `text.txt': Operation not permitted
[root@linzhongniao ~]# >text.txt
 -bash: text.txt: Operation not permitted
[root@linzhongniao ~]# echo "111">>text.txt 
[root@linzhongniao ~]# cat text.txt
ssdsd
111
相關文章
相關標籤/搜索