Linux文件權限

2.文件權限與目錄配置
本章同步視頻:https://edu.51cto.com/sd/e4874
2.1 三類用戶
0.root
1.文件擁有者
2.同組用戶
3.其餘用戶
本章同步視頻:https://edu.51cto.com/sd/e4874
2.2 文件權限
2.2.1 文件屬性
[root@localhost ~]# ls -l
total 548
-rw-------. 1 root root 1416 Oct 28 2018 anaconda-ks.cfg
-rw-r--r--. 1 root root 1467 Oct 27 2018 initial-setup-ks.cfg
-rw-r--r--. 1 root root 550255 Oct 11 14:15 練習平臺試題說明書.pdf
文件 鏈 屬 屬 文件 文件修改時間 文件名
權限 接 主 組 大小

2.2.2 chgrp - change group ownership
chgrp [OPTION]... GROUP FILE...
chgrp [OPTION]... --reference=RFILE FILE...
1.正常操做
[root@localhost tmp]# ll aaa #ls -l的別名
-rw-r--r--. 1 root root 0 Feb 26 19:58 aaa #屬組爲root
[root@localhost tmp]# chgrp calf aaa #將屬組修改成calf
[root@localhost tmp]# ll aaa
-rw-r--r--. 1 root calf 0 Feb 26 19:58 aaa #屬組爲calf
2.注意事項
(1)屬組必須存在
[root@localhost tmp]# chgrp group1 aaa
chgrp: invalid group: ‘group1’ #組group1不存在
(2)只有root能完成
[calflyok@localhost ~]$ ll aaa
-rw-rw-r--. 1 calflyok calflyok 0 Feb 26 20:02 aaa
[calflyok@localhost ~]$ chgrp calf aaa
chgrp: changing group of ‘aaa’: Operation not permitted
#文件擁有者亦無權執行該命令
2.2.3 chown - change file owner and group
chown [OPTION]... [OWNER][:[GROUP]] FILE...
1.正常修改擁有者
[root@localhost tmp]# ll aaa
-rw-r--r--. 1 root calf 0 Feb 26 19:58 aaa #擁有者爲root
[root@localhost tmp]# chown calf aaa
[root@localhost tmp]# ll aaa
-rw-r--r--. 1 calf calf 0 Feb 26 19:58 aaa #擁有者變爲calf
2.同時修改擁有者和屬組
[root@localhost tmp]# ll aaa
-rw-r--r--. 1 calf calf 0 Feb 26 19:58 aaa
[root@localhost tmp]# chown root:root aaa
[root@localhost tmp]# ll aaa
-rw-r--r--. 1 root root 0 Feb 26 19:58 aaa
3.注意事項
(1)用戶必須存在
[root@localhost tmp]# chown own aaa
chown: invalid user: ‘own’ #own用戶不存在
(2)只有root能執行
[calflyok@localhost ~]$ ll aaa
-rw-rw-r--. 1 calflyok calflyok 0 Feb 26 20:02 aaa
[calflyok@localhost ~]$ chown root aaa
chown: changing ownership of ‘aaa’: Operation not permitted
2.2.4 chmod - change file mode bits
chmod [OPTION]... MODE[,MODE]... FILE...
chmod [OPTION]... OCTAL-MODE FILE...
1.數字模式
r:4 w:2 x:1
(1)修改單個文件權限
[root@localhost tmp]# ll aaa
-rw-r--r--. 1 root root 0 Feb 26 19:58 aaa
[root@localhost tmp]# chmod 666 aaa
[root@localhost tmp]# ll aaa
-rw-rw-rw-. 1 root root 0 Feb 26 19:58 aaa
(2)修改整個目錄權限
[root@localhost tmp]# ls -al bbb
total 8
drwxr-xr-x. 2 root root 4096 Feb 26 20:24 .
drwxrwxrwt. 9 root root 4096 Feb 26 20:24 ..
#父目錄不在修改範圍內
-rw-r--r--. 1 root root 0 Feb 26 20:24 ccc
[root@localhost tmp]# chmod -R 666 bbb
[root@localhost tmp]# ls -al bbb
total 8
drw-rw-rw-. 2 root root 4096 Feb 26 20:24 .
drwxrwxrwt. 9 root root 4096 Feb 26 20:24 ..
#父目錄不在修改範圍內
-rw-rw-rw-. 1 root root 0 Feb 26 20:24 ccc
2.字符模式
chmod u
g
o
a +(加入)
-(除去)
=(設定) r
w
x 檔案或目錄php

(1)全部用戶增長執行權限
[root@localhost tmp]# ll ll aaa
-rw-rw-rw-. 1 root root 0 Feb 26 19:58 aaa
[root@localhost tmp]# chmod a+x aaa
[root@localhost tmp]# ll aaa
-rwxrwxrwx. 1 root root 0 Feb 26 19:58 aaa
(2)全部用戶去掉執行權限
[root@localhost tmp]# ll aaa
-rwxrwxrwx. 1 root root 0 Feb 26 19:58 aaa
[root@localhost tmp]# chmod a-x aaa
[root@localhost tmp]# ll aaa
-rw-rw-rw-. 1 root root 0 Feb 26 19:58 aaa
(3)全部用戶設定爲只讀
[root@localhost tmp]# ll aaa
-rw-rw-rw-. 1 root root 0 Feb 26 19:58 aaa
[root@localhost tmp]# chmod a=r aaa
[root@localhost tmp]# ll aaa
-r--r--r--. 1 root root 0 Feb 26 19:58 aaa
(4)擁有者增長寫和執行,其餘用戶設定爲可讀可寫
[root@localhost tmp]# ll aaa
-r--r--r--. 1 root root 0 Feb 26 19:58 aaa
[root@localhost tmp]# chmod u+wx,o=rw aaa
[root@localhost tmp]# ll aaa
-rwxr--rw-. 1 root root 0 Feb 26 19:58 aaa
(5)去掉其餘用戶的全部權限
[root@localhost tmp]# ll aaa
-rwxr--rw-. 1 root root 0 Feb 26 19:58 aaa
[root@localhost tmp]# chmod o= aaa
[root@localhost tmp]# ll aaa
-rwxr-----. 1 root root 0 Feb 26 19:58 aaa
3.注意事項
文件擁有者能夠無視權限,強行讀寫文件屬性和內容。
[calflyok@localhost ~]$ chmod a= aaa
[calflyok@localhost ~]$ ll aaa
----------. 1 calflyok calflyok 0 Feb 26 20:02 aaa
[calflyok@localhost ~]$ vim aaa
[calflyok@localhost ~]$ cat aaa
aaaaa
2.2.5 目錄與檔案之權限意義
1.權限對檔案的重要性
 r (read):可讀取此一檔案的實際內容,如讀取文本文件的文字內容等;
 w (write):能夠編輯、新增或者是修改該檔案的內容(但不含刪除該檔案);
 x (execute):該檔案具備能夠被系統執行的權限。
2.權限對目錄的重要性
 r (read contents in directory):表示具備讀取目錄結構列表的權限,能夠查詢該目錄下的文件名數據。 如使用ls命令!
 w (modify contents of directory):
 創建新的檔案與目錄;
 刪除已經存在的檔案與目錄(不論該檔案的權限爲什麼!)
 將已存在的檔案或目錄進行改名;
 搬移該目錄內的檔案、目錄位置。
 x (access directory):表明的是用戶可否進入該目錄成爲工做目錄! 如使用cd命令!
2.2.6 Linux檔案種類與擴展名
1.檔案種類:
 正規檔案(regular file ):第一個字符爲 [ - ] 。
 目錄(directory):第一個屬性爲 [ d ] 。
 連結檔(link):第一個屬性爲 [ l ] 。
 設備與裝置文件(device):一般都在/dev目錄下。
 區塊(block)設備檔 :第一個屬性爲[ b ]!
 字符(character)設備檔:第一個屬性爲 [ c ]。
 資料接口文件(sockets):第一個屬性爲 [ s ], 最常在/run或/tmp。
 數據輸送文件(FIFO, pipe):第一個屬性爲[p] 。
2.Linux檔案擴展名:
Linux的檔案是沒有所謂的『擴展名』的,一個Linux檔案能不能被執行,與他的第一欄的十個屬性有關, 與文件名根本一點關係也沒有。
.sh : 腳本或批處理文件 (scripts) ;
Z, .tar, .tar.gz, .zip, .tgz: 通過打包的壓縮文件。
.html, .php:網頁相關檔案,分別表明 HTML 語法與 PHP 語法的網頁檔案。
3.Linux檔案長度限制
單一檔案或目錄的最大允許文件名爲 255bytes,以一個 ASCII 英文佔用一個 bytes 來講,則大約可達 255 個字符長度。如果以每一箇中文字佔用 2bytes 來講, 最大檔名就是大約在 128 箇中文字!
4.Linux文件名的限制
因爲Linux在文字接口下的一些指令操做關係,通常來講,在設定Linux底下的文件名時, 最好能夠避免一些特殊字符比較好!html

相關文章
相關標籤/搜索