Linux文件權限詳解


#前言:我們知道,無論什么東西,涉及到安全性的,比如文件、文件夾、磁盤(就如window系統的磁盤,我們就可以通過bitlocker技術將磁盤給加密鎖起來)、服務器,等都需要設置權限管理,以保證安全性,接下來讓我們來探討以下Linux的文件權限

1.概述

權限是操作系統用來限制對資源訪問的機制,權限一般分為讀、寫、執行。

系統中的每個文件都擁有特定的權限、所屬用戶及所屬組,通過這樣的機制來限制哪些用戶、哪些組可以對特定文件進行什么樣操作。

 

#Linux的權限是基於UGO模型進行控制

1.U代表User(用戶),G代表Group(組),O代表Other(其他)
2.每一個文件的權限基於UGO進行設置
3.權限三個一組(rwx),對應UGO分別設置

 

#查看權限

[root@ctos3 ~]# ls -ld test
drwxr-xr-- 2 root root 6 Mar  9 01:37 test

#講解:第一個d是文件類型,后面9位3個為一組

 

#文件權限說明

linux文件或目錄的權限位是由9個權限位來控制,每三位一組,
它們分別是文件屬主(Owner)的讀、寫、執行,用戶組(Group)的讀、寫、執行以及(Other)其它用戶的讀、寫、執行 其中 r(read)讀權限, 可以讀取文件內容,可以列出目錄內容 用數字表示為4 w(write)寫權限, 可以修改文件內容,可以在目錄中創建刪除文件 用數字表示為2 x(excute)執行權限,可以作為命令執行,可以訪問目錄內容 用數字表示為1
- 沒有權限, 用數字表示為0

 

2.修改文件所屬用戶、所屬組

#1.使用chown命令改變文件/目錄的所屬用戶

修改格式:chown 用戶 文件名/目錄名

#例子,將test.txt的所屬用戶從root更改為demo用戶

[root@ctos3 ~]# ls -l test.txt 
-rw-r--r-- 1 root root 0 Mar  9 01:36 test.txt
[root@ctos3
~]# chown demo test.txt #更改
[root@ctos3
~]# ls -l test.txt -rw-r--r-- 1 demo root 0 Mar 9 01:36 test.txt

#參數介紹

1.-R 參數遞歸的修改目錄下的所有文件的所屬用戶
 #例子:將/test目錄下的所有文件和用戶所屬用戶修改成demo
    [root@ctos3 ~]# chown -R demo /test/
    [root@ctos3 ~]# ls -l /test/
    drwxr-xr-x 3 demo root 16 Mar  9 01:55 aa

2.加個;也可以快速改回所屬用戶和所屬組

#2.使用chgrp改變文件/目錄的所屬組

命令格式:chgrp 用戶 文件/目錄名

#例子:

[root@ctos3 ~]# chgrp  demo /test/
[root@ctos3 ~]# ls -ld /test/
drwxr-xr-x 3 demo demo 16 Mar  9 01:55 /test/

#注意點:一般都是用chown修改用戶和組的了 格式chown -R 用戶.組 + 文件

#3.使用chmod命令修改文件/目錄的權限

 命令格式:chmod +模式 +文件

模式為如下格式:
1.u、g、o、分別代表用戶、組和其他
2.a可以代指ugo
3.+、-代表加入或刪除對應權限
4.r、w、x代表三種權限

#修改例子:

chmod u+rw test.txt  #在user用戶那里添加rw權限
chmod g-x test.txt    #在group組那里去掉x權限
chmod go+r test.txt  #在組和其他用戶添加r權限
chmod u=rx test.txt  #將用戶權限設置為rx權限

 

#提示:chmod命令也支持以數字方式修改

-r=4  (2的2次方)
-w=(2的1次方)
-x= (2的0次方)
使用數字表示權限時,每組權限分別對應數字之和:
rw=
rwx=
r-x=

#例子:
chmod 664 test.txt  == rw-rw-r
chmod 775 test.txt  == rwxrwxr-x

 

3.擴展權限

#3.1.默認權限

每一個終端都擁有一個umask屬性,來確定新建文件、文件夾的默認權限
umask使用數字權限方式表示,如:022
目錄的默認權限是777-umask 就是755 文件的默認權限是:666-umask 就是644
一般,普通用戶的默認umask是0222,root用戶的默認umask是0222
也就是說,對於普通用戶來講: 新建文件的權限是:
666002664 新建目錄的權限是:777002775

 

#例子:

#創建目錄,默認權限為755
[root@ctos3 ~]# ls -ld /test1/
drwxr-xr-x 2 root root 6 Mar  9 02:09 /test1/

#創建文件,默認權限為644
[root@ctos3 ~]# touch 2.txt
[root@ctos3 ~]# ls -l 2.txt 
-rw-r--r-- 1 root root 0 Mar  9 02:11 2.txt

#可以使用umask查看設置的umask值

[root@ctos3 ~]# umask
0022

 

#如果想要創建的文件權限多少,可以自己定義

[root@ctos3 ~]# umask 035  #設置默認umask為035,創建出來的文件默認權限為642
[root@ctos3 ~]# touch fil035
[root@ctos3 ~]# ls -l fil035 
-rw-r---w- 1 root root 0 Mar  9 02:25 fil035
#注意為什么是642,而不是631呢,因為是奇數的話就會加1,從之前默認權限6就會加到7,用7-3就是4,7-5就是2,0為偶數所以還是6,所以為642

[root@ctos3 ~]# umask 022  #設置022,創建文件的權限為644
[root@ctos3 ~]# touch fil022
[root@ctos3 ~]# ls -l fil022 
-rw-r--r-- 1 root root 0 Mar  9 02:25 fil022

#3.2特殊權限

linux系統基本權限位為9位權限,但還有額外3位權限位,共12位權限
  suid      用戶對應的權限位
  sgid      用戶組對應的權限位
  sticky    其他用戶對應的權限位(只能是刪除自己,不能刪除別人的。。但是有個例外,就是文件所有者就可以刪除)

與普通權限一樣,特殊權限也可以使用數字方式表示
-SUID=
-SGID-
-Sticky=

#設置特殊權限

#.設置suid針對用戶的)
 命令格式:chmod  4755 file 或者   chmod u+s file

#例子:
[root@ctos3 ~]# which passwd
/usr/bin/passwd
[root@ctos3 ~]# ls -l `which passwd`
-rwsr-xr-x. 1 root root 27832 Jun 10  2014 /usr/bin/passwd

[root@ctos3 ~]# chmod 4644 test.txt #設置test文件的s為權限
[root@ctos3 ~]# ls -l test.txt 
-rwSr--r-- 1 demo root 0 Mar  9 01:36 test.txt

#2.設置sgid(針對組的)
命令格式:chmod 2755 file  或者   chmod g+s file

#例子:
[root@ctos3 ~]# chmod 2755 /test
[root@ctos3 ~]# ls -ld /test
drwxr-sr-x 3 demo demo 16 Mar  9 01:55 /test

#3.設置sticky(可以將自己文件保護起來)
命令格式:chmod o+t file

#例子:
[root@ctos3 ~]# touch 3.txt
[root@ctos3 ~]# chmod o+t 3.txt 
[root@ctos3 ~]# ls -ld 3.txt 
-rw-r--r-T 1 root root 0 Mar  9 02:38 3.txt

#查找系統中設置了suid的文件

[root@ctos3 ~]# find /usr/bin/ -type f -perm 4755 -exec ls -l {} \;
-rwsr-xr-x. 1 root root 32008 Apr 11  2018 /usr/bin/fusermount
-rwsr-xr-x. 1 root root 64240 Nov  5  2016 /usr/bin/chage
-rwsr-xr-x. 1 root root 78216 Nov  5  2016 /usr/bin/gpasswd
-rwsr-xr-x. 1 root root 41776 Nov  5  2016 /usr/bin/newgrp
-rwsr-xr-x. 1 root root 44320 Apr 11  2018 /usr/bin/mount
-rwsr-xr-x. 1 root root 32184 Apr 11  2018 /usr/bin/su
-rwsr-xr-x. 1 root root 32048 Apr 11  2018 /usr/bin/umount
-rwsr-xr-x. 1 root root 27680 Apr 10  2018 /usr/bin/pkexec
-rwsr-xr-x. 1 root root 57576 Apr 10  2018 /usr/bin/crontab
-rwsr-xr-x. 1 root root 27832 Jun 10  2014 /usr/bin/passwd

 

#有關suid和sgid總結

1.suid是針對命令和二進制程序的
2.suid作用是讓普通用戶以root(或其他)的用戶角色運行只有root(或其他)賬號才能運行的程序或命令,或程序命令對應本來沒有權限操作的文件等
3.sgid與suid不同的是,sgid既可以針對文件也可以針對目錄設置
4.sgid是針對用戶組權限位的

 

4.查看和修改文件屬性命令lsattr,chattr

#使用lsattr命令顯示文件屬性,使用chattr命令修改文件屬性

#例子:

root@ctos3 ~]# mkdir attribute
[root@ctos3 ~]# cd attribute/
[root@ctos3 attribute]# echo "file attribution" > attribution.txt
[root@ctos3 attribute]# lsattr
---------------- ./attribution.txt
#根據上面操作。使用lsattr查看沒有賦予任何屬性,下面就使用chattr來為文件添加屬性

[root@ctos3 attribute]# chattr +i attribution.txt 
[root@ctos3 attribute]# lsattr 
----i----------- ./attribution.txt

#提示:添加i屬性到文件之后,即使是root用戶也不能修改、刪除文件,可以加a權限,但是添加了也不能刪除文件,知道將這兩個權限刪除,才能刪除修改文件
[root@ctos3 attribute]# chmod 655 attribution.txt 
chmod: changing permissions of ‘attribution.txt’: Operation not permitted
[root@ctos3 attribute]# rm attribution.txt 
rm: remove regular file ‘attribution.txt’? y
rm: cannot remove ‘attribution.txt’: Operation not permitted

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM