Linux chmod 命令 詳解

2021-06-03 08:30:02 字數 3838 閱讀 5152

注:本文**自:

chmod 命令用的很多,但是對這個命令卻不是很了解。 在網上搜了一下。 整理如下。

命令格式:

chmod [-cfvr] [--help] [--version] mode file...

[root@qs-wg-db2 ~]# chmod --help

usage: chmod [option]... mode[,mode]... file...

or:  chmod [option]... octal-mode file...

or:  chmod [option]... --reference=rfile file...

change the mode of each file to mode.

-c, --changes           like verbose but report only when a change is made

--no-preserve-root  do not treat `/' specially (the default)

--preserve-root     fail to operate recursively on `/'

-f, --silent, --quiet   suppress most error messages

-v, --verbose           output a diagnostic for every file processed

--reference=rfile   use rfile's mode instead of mode values

-r, --recursive         change files and directories recursively

--help     display this help and exit

--version  output version information and exit

each mode is of the form `[ugoa]*([-+=]([rwxxst]*|[ugo]))+'.

report bugs to .

[root@qs-wg-db2 ~]#

引數說明:

1. -cfvr 部分

-c : 若該檔案許可權確實已經更改,才顯示其更改動作

-f : 若該檔案許可權無法被更改也不要顯示錯誤訊息

-v : 顯示許可權變更的詳細資料

-r : 對目前目錄下的所有檔案與子目錄進行相同的許可權變更(即以遞迴的方式逐個變更), 這個-r 用的還是很多的。

2.  mode 部分

這部分可以分成如下3塊: [who] operator [permission]

`[ugoa]*([-+=]([rwxxst]*|[ugo]))+'.

who的含義是:

u 檔案屬主許可權

g 同組使用者許可權

o 其它使用者許可權

a 所有使用者(包括以上三種)

operator的含義:

+ 增加許可權

- 取消許可權

=  唯一設定許可權

permission的含義:

r 讀許可權

w 寫許可權

x 執行許可權

x 表示只有當該檔案是個子目錄或者該檔案已經被設定過為可執行。

s 檔案屬主和組id

l 給檔案加鎖,使其它使用者無法訪問

如: chmod a+x

3. 示例:

(1)將檔案 file1.txt 設為所有人皆可讀取 :

chmod ugo+r file1.txt 

(2)將檔案 file1.txt 設為所有人皆可讀取 :

chmod a+r file1.txt 

(3)將檔案 file1.txt 與 file2.txt 設為該檔案擁有者,與其所屬同乙個群體者可寫入,但其他以外的人則不可寫入 :

chmod ug+w,o-w file1.txt file2.txt 

(4)將 ex1.py 設定為只有該檔案擁有者可以執行 :

chmod u+x ex1.py 

(5)將目前目錄下的所有檔案與子目錄皆設為任何人可讀取 :

chmod -r a+r * 

(6)收回所有使用者的對file1的執行許可權

chmod a-x file1

4. 使用數字來賦權

4.1 先看一下檔案的許可權格式

[root@qs-wg-db2 scripts]# ll

total 20

-rw-r--r-- 1 oracle oinstall    0 feb 24 00:00 alertlogbyday.log

-rwxr-xr-x 1 oracle oinstall  430 feb 20 01:10 alertlogbyday.sh

-rwxr-xr-x 1 oracle oinstall    7 feb 24 05:00 del_st_arch.log

-rwxr-xr-x 1 oracle oinstall  648 feb 19 00:51 del_st_archive.sh

-rwxr-xr-x 1 oracle oinstall    9 feb 24 05:00 max_sn.log

drwxr-xr-x 3 root   root     4096 feb 23 23:40 pymonitor

ll的結果返回七列,分別表示如下含義:

第一欄  [檔案屬性]

第二欄  [檔案數]

第三欄  [擁有者]

第四欄  [所有者群組]

第五欄  [大小]

第六欄  [建檔日期]

第七欄  [檔名]

我們設定檔案的許可權就是這是第一欄裡的檔案屬性。

檔案屬性這塊共有十個字段,如:drwxr-xr-x

我們把這10個列分成4塊:[d] [rwx] [r-x] [r-x]

第一塊:也就是第一列,用來表示這個檔案的型別,有如下值:

(1)[ d ]則是目錄,我這裡的是d,表示的是乙個目錄

(2)[ - ]則是檔案;

(3)[ l ]則表示為鏈結檔(link file);

(4)[ b ]則表示為裝置檔案裡面的可供儲存的介面裝置;

(5)[ c ]則表示為裝置檔案裡面的串列埠裝置,例如鍵盤、滑鼠。

第二塊:第 2到4 列,表示檔案擁有者的許可權。

第三塊:第5到7 列,表示擁有者同組人的許可權。

第四塊:第8到10列,表示是非擁有者組人的許可權。

這些許可權均有[rwx] 三個引數表示,而且分別對應不同的位置。每塊由3個列組成,每列對應乙個值。 [ r ]代表可讀、[ w ]代表可寫、[ x ]代表可執行。

4.2 使用數字賦權

在4.1 中了解準備知識之後,就可以使用數字賦權了。 每塊使用者有3個許可權[rwx]. 他們對應數字:

r -->4

w-->2

x-->1

使用數字賦權的命令格式如下:

chmod abc filename

其中的abc 分別代表3個數字,並且分別對應問個不同的屬組。 即:

數字a 對應 第2到4位,表示擁有者的許可權。

數字b 對應 第5到7位,表示同組使用者的許可權。

數字c 對應第8到10位,表示其他組的許可權。

rwx 對應4,2,1. 那麼唯讀的許可權用4表示[r--],讀寫用6(4+2)表示[rw-],寫加執行用7(4+2+1)表示[rwx]。 唯讀加執行用5(4+1)表示[r-x]。

示例:chmod 755 file1

chmod 777 file1 <==> chmod a=rwx file 

chmod 771 file  <==> chmod ug=rwx,o=x file 

Linux chmod命令詳解

chmod 改變乙個或多個檔案的訪問模式 mode chmod options mode files 只能檔案屬主或特權使用者才能使用該功能來改變檔案訪問模式。mode可以是數字形式或以who opcode permission形式表示。who是可選的,預設是a 所有使用者 只能選擇乙個opcode...

Linux chmod命令詳解

chmod 改變乙個或多個檔案的訪問模式 mode chmod options mode files 只能檔案屬主或特權使用者才能使用該功能來改變檔案訪問模式。mode可以是數字形式或以who opcode permission形式表示。who是可選的,預設是a 所有使用者 只能選擇乙個opcode...

Linux chmod命令詳解

chmod 改變乙個或多個檔案的訪問模式 mode chmod options mode files 只能檔案屬主或特權使用者才能使用該功能來改變檔案訪問模式。mode可以是數字形式或以who opcode permission形式表示。who是可選的,預設是a 所有使用者 只能選擇乙個opcode...