歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> 每天一個Linux命令(23)chmod命令

每天一個Linux命令(23)chmod命令

日期:2017/3/3 12:33:03   编辑:Linux技術

[b] chmod命令用來變更文件或目錄的權限。[/b] 在UNIX系統家族裡,文件或目錄權限的控制分別以讀取、寫入、執行3種一般權限來區分,另有3種特殊權限可供運用。用戶可以使用chmod指令去變更文件與目錄的權限,設置方式采用文字或數字代號皆可。符號連接的權限無法變更,如果用戶對符號連接修改權限,其改變會作用在被連接的原始文件。

[b] (1)用法:[/b] [b] 用法: chmod [選項] [mode] 模式 文件[/b]

或 chmod [-cfvR] [--help] [--version] mode file命令有兩種用法:一種是包含字母和操作符表達式的文字設定法;另一種是包含數字的數字設定法。

[b] (2)功能:[/b][b] 功能:用於改變文件或目錄的訪問權限,用它控制文件或目錄的訪問權限。[/b]

[b] (3)參數詳解:[/b] 1) -R ——recursive:              遞歸處理,將指令目錄下的所有文件及子目錄一並處理

2) -v ——verbose :               顯示指令執行過程 3) --reference=<參考文件或目錄>:         把指定文件或目錄的所屬群組全部設成和參考文件或目錄的所屬群組相同

4) <權限范圍>+<權限設置>:           開啟權限范圍的文件或目錄的該選項權限設置 <權限范圍> -<權限設置>: 關閉權限范圍的文件或目錄的該選項權限設置

<權限范圍>=<權限設置>: 指定權限范圍的文件或目錄的該選項權限設置[b] (4)權限范圍的表示法:

1) u User    即文件或目錄的擁有者[/b]

2) g Group   即文件或目錄的所屬群組 3) o Other 除了文件或目錄擁有者或所屬群組之外,其他用戶皆屬於這個范圍

4) a All      即全部的用戶,包含擁有者,所屬群組以及其他用戶 5) r 讀取權限,數字代號為“4”

6) w          寫入權限,數字代號為“2” 7) x 執行或切換權限,數字代號為“1”

8) - 不具任何權限,數字代號為“0” 9) s 特殊功能說明:變更文件或目錄的權限

[b] (5)實例:[/b] 1)[root@localhost Documents]# chmod a+x core.log          為所有用戶組添加可執行權限

[root@localhost Documents]# ll -al core.log         //ll -al 文件名與 ls -l 文件名貌似沒啥區別
-rw-r--r--. 1 root root 27 5月  19 04:21 core.log
[root@localhost Documents]# ls -l core.log
-rw-r--r--. 1 root root 27 5月  19 04:21 core.log
[root@localhost Documents]# chmod a+x core.log     
[root@localhost Documents]# ll -al core.log
-rwxr-xr-x. 1 root root 27 5月  19 04:21 core.log
2)[root@localhost Documents]# chmod ug+w,o-x core.log       同時為不同用戶添加或刪除權限

[root@localhost Documents]# ll -al core.log
-rwxr-xr-x. 1 root root 27 5月  19 04:21 core.log
[root@localhost Documents]# chmod ug+w,o-x core.log
[root@localhost Documents]# ll -l core.log
-rwxrwxr--. 1 root root 27 5月  19 04:21 core.log
3)[root@localhost Documents]# chmod g=x,o=rwx core.log       設置文件的u,g,o的三個權限。這裡是設置文件所有者權限為可執行,其他用戶可以讀寫執行,組用戶權限不變

[root@localhost Documents]# ll -l core.log
-rwxrwxr--. 1 root root 27 5月  19 04:21 core.log
[root@localhost Documents]# chmod g=x,o=rwx core.log
[root@localhost Documents]# ls -l core.log
-rwx--xrwx. 1 root root 27 5月  19 04:21 core.log
4)[root@localhost Documents]# chmod -R o=--- findDir      遞歸的設置findDir文件夾下的文件及文件夾的其他用戶權限為不具備任何權限

[root@localhost Documents]# chmod -R o=--- findDir
[root@localhost Documents]# find . -name "Dir" -print
./Dir
[root@localhost Documents]# find . -name "Dir" -exec ls -l {} \;
總用量 12
--w-------. 1 root root 664 5月   9 07:59 head_text
--w-------. 1 root root  45 5月   9 08:15 less1
--w-------. 1 root root  57 5月   9 08:16 less2
[root@localhost Documents]# ls -l Dir
總用量 12
--w-------. 1 root root 664 5月   9 07:59 head_text
--w-------. 1 root root  45 5月   9 08:15 less1
--w-------. 1 root root  57 5月   9 08:16 less2
5)[root@localhost Documents]# chmod -R u=r,g=r,o=r findDir 與 [root@localhost Documents]# chmod -R =rx Dir    等價地給ugo的用戶設置權限

[root@localhost Documents]# chmod -R u=r,g=r,o=r findDir
[root@localhost Documents]# ls -l findDir
總用量 0
-r--r--r--. 1 root root 0 5月  17 04:18 p1.pdf
-r--r--r--. 1 root root 0 5月  17 04:18 p2.pdf
-r--r--r--. 1 root root 0 5月  17 03:50 t1.txt
-r--r--r--. 1 root root 0 5月  17 04:02 T1.txt
-r--r--r--. 1 root root 0 5月  19 04:58 t2.txt
-r--r--r--. 1 root root 0 5月  17 04:02 T2.txt
[root@localhost Documents]# ls -l Dir
總用量 12
--w-------. 1 root root 664 5月   9 07:59 head_text
--w-------. 1 root root  45 5月   9 08:15 less1
--w-------. 1 root root  57 5月   9 08:16 less2
[root@localhost Documents]# chmod -R =rx Dir           //設置所有組用戶權限為rw之後,以前的所有者的w權限就沒了
[root@localhost Documents]# ls -l Dir
總用量 12
-r-xr-xr-x. 1 root root 664 5月   9 07:59 head_text
-r-xr-xr-x. 1 root root  45 5月   9 08:15 less1
-r-xr-xr-x. 1 root root  57 5月   9 08:16 less2
6)[root@localhost Documents]# chmod 444 find        用數字給文件設置權限

[root@localhost Documents]# chmod 444 find
[root@localhost Documents]# ls -l find
-r--r--r--. 1 root root 0 5月  19 04:16 find
[root@localhost Documents]# chmod =r newlocate                //兩種方法等價,但同時都會將原有的權限清除
[root@localhost Documents]# ls -l newlocate
-r--r--r--. 1 root root 0 5月  15 18:21 newlocate

[b] (6)其他:[/b] Linux用戶分為:擁有者、組群(Group)、其他(other)。

Linux系統中,預設的情況下,系統中所有的帳號與一般身份使用者,以及root的相關信息, 都是記錄在/etc/passwd文件中。每個人的密碼則是記錄在/etc/shadow文件下。 此外,所有的組群名稱記錄在/etc/group內!

Copyright © Linux教程網 All Rights Reserved