Linux基本命令 chmod

2021-09-25 07:18:48 字數 3783 閱讀 2846

chmod命令用來變更檔案或目錄的許可權。在unix系統家族裡,檔案或目錄許可權的控制分別以讀取、寫入、執行3種一般許可權來區分,另有3種特殊許可權可供運用。使用者可以使用chmod指令去變更檔案與目錄的許可權,設定方式採用文字或數字代號皆可。符號連線的許可權無法變更,如果使用者對符號連線修改許可權,其改變會作用在被連線的原始檔案。

許可權範圍的表示法如下:

chmod (選項) (引數)

-c或——changes:效果類似「-v」引數,但僅回報更改的部分;

-f或--quiet或——silent:不顯示錯誤資訊;

-r或——recursive:遞迴處理,將指令目錄下的所有檔案及子目錄一併處理;

-v或——verbose:顯示指令執行過程;

《許可權範圍》+《許可權設定》:開啟許可權範圍的檔案或目錄的該選項許可權設定;

《許可權範圍》-《許可權設定》:關閉許可權範圍的檔案或目錄的該選項許可權設定;

《許可權範圍》=《許可權設定》:指定許可權範圍的檔案或目錄的該選項許可權設定;

許可權模式:指定檔案的許可權模式;

檔案:要改變許可權的檔案。

linux用 戶分為:擁有者、組群(group)、其他(other),linux系統中,預設的情況下,系統中所有的帳號與一般身份使用者,以及root的相關信 息, 都是記錄在/etc/passwd

檔案中。每個人的密碼則是記錄在/etc/shadow檔案下。 此外,所有的組群名稱記錄在/etc/group內!

linux檔案的使用者許可權的分析圖

r=可讀    //值為4

w=可寫    //值為2

x=可執行    //值為1

-=無許可權    值為0

[root@localhost ~]# touch

test.txt

[root@localhost ~]# ll

total

4-rw-------. 1 root root 1523 apr 4

18:27 anaconda-ks.cfg

-rw-r--r--. 1 root root 0 jun 13

16:22 test.txt

我們可以看到新建的test.txt文件具有的許可權:rw-r--r--,對應的值為644。

[root@localhost ~]# chmod u=rwx,g=rw,o=r test.txt 

[root@localhost ~]# ll

total

4-rw-------. 1 root root 1523 apr 4

18:27 anaconda-ks.cfg

-rwxrw-r--. 1 root root 0 jun 13

16:22 test.txt

現在的許可權為rwxrw-r--,許可權值為764.

[root@localhost ~]# chmod u-x,g+x-w,o+x test.txt 

[root@localhost ~]# ll

total

4-rw-------. 1 root root 1523 apr 4

18:27 anaconda-ks.cfg

-rw-r-xr-x. 1 root root 0 jun 13

16:22 test.txt

現在的許可權是rw-r-xr-x,許可權值為655.

[root@localhost ~]# chmod a=r test.txt 

[root@localhost ~]# ll test.txt

-r--r--r--. 1 root root 0 jun 13

16:22 test.txt

現在的許可權是r--r--r--,許可權值為444.

[root@localhost ~]# chmod a+x test.txt 

[root@localhost ~]# ll test.txt

-r-xr-xr-x. 1 root root 0 jun 13

16:22 test.txt

現在的許可權是r-xr-xr-x,許可權值為555.

[root@localhost ~]# chmod

440test.txt

[root@localhost ~]# ll test.txt

-r--r-----. 1 root root 0 jun 13

16:22 test.txt

通過值更改許可權後,同樣根據值的換算得到檔案許可權:r--r-----,值為440

[root@localhost ~]# chmod

777test.txt

[root@localhost ~]# ll test.txt

-rwxrwxrwx. 1 root root 0 jun 13

16:22 test.txt

上面是777許可權。rwxrwxrwx.

遞迴引數

[root@localhost ~]# ls -l

total

4-rw-------. 1 root root 1523 apr 4

18:27 anaconda-ks.cfg

drwxr-xr-x. 2 root root 45 jun 13

16:36

test

[root@localhost ~]# ls -l test

total

0-r--r--r--. 1 root root 0 jun 13

16:36

test1

--w--w--w-. 1 root root 0 jun 13

16:36

test2

---x--x--x. 1 root root 0 jun 13

16:36 test3

當前test目錄為731許可權,test目錄下的test1為444許可權,test2為222許可權,test3為111許可權。現在進行遞迴許可權管理:

[root@localhost ~]# chmod -r 777

test

[root@localhost ~]# ls -l

total

4-rw-------. 1 root root 1523 apr 4

18:27 anaconda-ks.cfg

drwxrwxrwx.

2 root root 45 jun 13

16:36

test

-rwxrwxrwx. 1 root root 0 jun 13

16:22

test.txt

[root@localhost ~]# ls -l test

total

0-rwxrwxrwx. 1 root root 0 jun 13

16:36

test1

-rwxrwxrwx. 1 root root 0 jun 13

16:36

test2

-rwxrwxrwx. 1 root root 0 jun 13

16:36 test3

通過執行chmod -r 777 test命令,test目錄的許可權以及其下的所有檔案全部遞迴調整許可權為777。

Linux基本命令31 chmod命令

linux unix 的檔案呼叫許可權分為 檔案擁有者 群組 其他。利用 chmod 可以藉以控制檔案如何被他人所呼叫。chmod的使用許可權 所有使用者。語法chmod cfvr help version mode file 引數說明 mode 許可權設定字串,格式如下 ugoa rwxx 其中 ...

linux基本命令

linux常用命令 基礎 eg man ls 就可以檢視ls相關的用法 注 按q鍵或者ctrl c退出,在linux下可以使用ctrl c終止當前程式執行。2.ls 檢視目錄或者檔案的屬 列舉出任一目錄下面的檔案 eg ls usr man ls l a.d表示目錄 directory 如果是乙個 ...

《linux基本命令》

博主部落格 斷橋殘雪 uname 顯示版本資訊 同win2k的 ver dir 顯示當前目錄檔案,ls al 顯示包括隱藏檔案 同win2k的 dir pwd 查詢當前所在的目錄位置 cd cd 回到上一層目錄,注意cd 與.之間有空格。cd 返回到根目錄。cat 檔名 檢視檔案內容 cat abc...