1. 程式人生 > >linux檔案許可權,許可權的更改,特殊權力的賦予

linux檔案許可權,許可權的更改,特殊權力的賦予

一.檔案許可權存在的意義

系統最底層安全設定方法之一

保證檔案可以被可用的使用者作相應操作

二.檔案許可權的檢視

ls  -l    file

ls  -ld   dir

ll      file           ## ll = ‘ls -l' 的縮寫,每個版本的linux的縮寫不一定一樣,alias命令可查詢哪些命令能縮寫

ll  -d   file

三.檔案許可權的讀取

- rw-r--r-- 1 root root 0 Jan  3  11:52 file
[1] [2] [3] [4] [5] [6] [7] [8]

 

 

 

 

[1]    檔案的型別

-                   ##空檔案

d                  ##目錄

l                    ##軟連線:相當於快捷方式

s                   ##socket套接字:對外開放的介面,開啟程式的檔案

b                   ##block塊裝置:儲存裝置,例如u盤等

c                    ##字元裝置: 顯示字元,shell輸入後,回饋顯示的檔案

[2]   檔案的許可權

rw-|rw-|r--

 1     2   3

1.[u]=user   檔案擁有者能對檔案做什麼操作

2.[g]=group    檔案所有組對檔案能做什麼操作

3.[o]=other     其他人對檔案能做什麼操作

[3]

對檔案:檔案硬連結個數(檔案內容被記錄次數)

對目錄:目錄中子目錄個數

[4]

檔案的所有人

[5]

檔案所有組

[6]

對檔案:檔案大小

對目錄:目錄中子檔案源資料(matedate可以理解為檔案的屬性)大小

目錄大小:
取決於內容屬性的多少

[[email protected] Desktop]$ mkdir al
[[email protected] Desktop]$ ls -ld al
drwxrwxr-x 2 kiosk kiosk    6       Dec 31 13:23 al
                                       [目錄大小]
[[email protected] Desktop]$ ls -al al
total 0
drwxrwxr-x 2 kiosk kiosk   6 Dec 31 13:23 .
d rwxr-xr-x 3 kiosk kiosk 170 Dec 31 13:23 ..
0     1         1    1          1     1         1                 0  =6
新建目錄大小為6,因為其中包含‘。。’和‘。’,  ‘。’和本目錄大小無關,

在此目錄下新建檔案,
如:
[[email protected] Desktop]$ touch al/11
[[email protected] Desktop]$ ls -al al
total 0
drwxrwxr-x 2 kiosk kiosk  15 Dec 31 13:27 .
d rwxr-xr-x 3 kiosk kiosk 170   Dec 31 13:23  ..
0         1     1    1         1       1              1              0 =6
- rw-rw-r-- 1 kiosk kiosk   0     Dec 31 13:27   11  +
1       1      1    1         1       1                  1             2 =9
                                                                                    15
[[email protected] Desktop]$ ls -ld al
drwxrwxr-x 2 kiosk kiosk 15 Dec 31 13:27 al

所以目錄大小為15.

[7]

檔案的內容被修改的時間

[8]

檔案的名稱

四.改變檔案的所有人和所有組

chown|chgrp

chown                    username          file|dir

chown                     user:group        file|dir

chown      -R          user:group         dir           ## -R 是遞迴引數,即把該目錄下的所以檔案一併更改

chgrp                            group           file|dir

chgrp         -R             group             dir

五.改變檔案許可權

1.對許可權的理解

r                       對檔案:是否可以檢視檔案的內容      --->cat  file

                        對目錄:是否可以檢視目錄中有什麼字檔案或者子目錄    ------>ls   dir

w                      對檔案:是否可以改變檔案裡面記錄的字元

                        對目錄:是否可以對目錄中子目錄或者子檔案的源資料進行更改

x                      對檔案:是否可以通過檔名稱呼叫檔案內記錄的程式

                       對目錄:是否可以進入目錄

2.更改方式

chmod       <u|g|o><+|-|=><r|w|x>            file|dir

chmod    u+x          /mnt/file1

chmod    g-r            /mnt/file

chmod     ug-r         /mnt/file

chmod   u-r  ,g+x     /mnt/file

chmod   -r           /mnt/file

chmod  o=r-x        /mnt/file

引數可用另一種表所方法

r   w  x

2   1  0

r=4    w=2   x=1

r-x|r--|--x

5    4   1

chmod   541 /mnt/file1

六.umask

umask           系統建立檔案時預設保留的權力

root預設為 022
普通使用者預設為 002

建立目錄  777-022=755          #755即是新目錄的預設許可權
建立檔案  777-022-111=644  #644即是新檔案的預設許可權

umask   077         ##臨時設定系統預留許可權為077

永久更改umask

vim  /etc/profile             ##系統配置檔案

if  [$UID -gt 199] && ["'id   -gn' "="'id -un'"];   then

    umask     002                         ##普通使用者的umask

else

    umask     077                         ##超級使用者的umask

   fi

vim  /etc/bashrc            ##shell配置檔案

  if  [$UID -gt  199]  && ["'id  -gn'"="'id   -un'"];   then

     umask   002   

else

    umask   077

 fi

source   /etc/profile              #讓更改立即生效

source  /etc/bashrc  

七.特殊許可權

1.sticky                    ##粘制位

作用:

            只針對目錄生效,當一個目錄上有sticky許可權時

             在這個目錄中的檔案只能被檔案的所有者刪除

設定方式:

     chmod  o+t      dir

     chmod    1xxx   dir

2.sgid                 #強制位

作用:

                對檔案:只針對與二進位制可執行檔案

                                 當檔案上有sgid時任何人執行此檔案產生的程序都屬於檔案的組

                對目錄:當目錄上有sgid許可權時任何人在此目錄中建立的檔案都屬於目錄的所有組

 方式:

               chmod       g+s    file|dir

              chmod          2xxxx     file|dir

3.suid         #冒險位

            只針對二進位制可執行檔案

             當檔案上有suid時任何人執行這個檔案記的程式產生的程序都屬於檔案的所有人

設定方式

               chmod      u+s   file

               chmod      4xxxx  file

八.acl許可權列表

1.作用

  讓特定使用者對特定檔案擁有特定許可權

2.acl列表檢視

   -rw-rwxr-- + 1 root   root   0 Jan  3  11:52   file

                    ^

              檔案許可權後有'+',表示acl開啟

getfacl        file              #檢視acl開啟的檔案的許可權

 #file :   file                  #檔名稱

#owner: root                 #檔案擁有者

#group:root                    #檔案擁有組

user::rw-                      #檔案擁有人的許可權

user:wop:rwx              #指定使用者的許可權

 group::---                      ##檔案擁有組的權力

 mask::rwx                     ##能賦予使用者的最大權力伐值

other::---                         ##其他人的許可權

3.acl列表的管理

setfacl      -m u:username:rwx   file                          #設定username對file擁有rwx許可權

setfacl     -m   g:group:rwx    file                               #設定group組成員對file擁有rwx許可權

setfacl    -x    u:username  file                                 #從acl列表中刪除username

setfacl         -b    file                                                  #關閉file上的acl列表

4.mask值

在許可權列表中mask表示能生效的權力值

當用chmod減小開啟acl的檔案許可權時mask值會發生改變

chmod   g-w   file

如果要恢復mask值了

setfacl  -m  m :rwx file

5.acl的預設許可權設定

acl預設許可權只針對目錄設定

注:acl許可權只針對設定完成之後新建立的檔案或目錄生效,而已經存在的檔案是不會繼承預設許可權

setfacl  -m  d:u:student:rwx  dir             ##設定預設該目錄所有新建檔案讓username對file擁有rwx許可權

setfacl  -k   file                          ##刪除預設設定