文章目錄
修改檔案的許可權
chmod [options] who operator permission file-list (符號模式)
chmod [options] mode file-list (絕對模式)
參數
file-list 是chmod需要修改許可權的檔案名稱或目錄的路徑名
說明
Linux中主要有兩種改變許可權的方法
第一種:使用符號模式,例如:chmod a+x file,此處的a代表所有使用者,+代表添加許可權,x代表執行許可權
第二種:使用絕對模式,例如:chmod 777 file,表示為所有使用者添加可讀可寫可執行許可權,三個數值分別對應三種使用者類型
符號模式
who 使用者類型
| who |
使用者類型 |
意義 |
| u |
User |
檔案的所有者 |
| g |
Other |
與檔案相關聯的組 |
| o |
Other |
所有其他使用者 |
| a |
All |
相當與ugo,所有使用者 |
operator 運算子
| operator |
意義 |
| + |
為指定的使用者類型添加許可權 |
| - |
為指定的使用者類型刪除許可權 |
| = |
設定或重設指定使用者類型的許可權 |
permission 模式
| permission |
意義 |
對檔案含義 |
對目錄含義 |
| r |
設定讀許可權 |
可以查看檔案內容 |
可以列出目錄中的內容 |
| w |
設定寫入權限 |
可以修改檔案內容 |
可以在目錄中建立、刪除檔案 |
| x |
設定執行許可權 |
可以執行檔案 |
可以進入目錄 |
從上面可以知道,為什麼在目錄的許可權中,r和x經常在一起設定
因為必須進入目錄才能讀取內容
絕對模式
絕對模式的典型範例
| 模 式 |
意義 |
| 777 |
所有使用者都對檔案具有讀、寫和執行許可權 |
| 755 |
檔案所有者對檔案具有讀、寫和執行許可權;組使用者和其他使用者對檔案需有讀和執行許可權 |
| 711 |
檔案所有者對檔案具有讀、寫和執行許可權;組使用者和其他使用者對檔案具有執行許可權 |
| 644 |
檔案所有者可以讀、寫檔案;組使用者和其他使用者可以讀檔案 |
| 640 |
檔案所有者可以讀、寫檔案;組使用者可以讀檔案;其他使用者不能訪問檔案 |
選項
-c 顯示修改過程資訊
-f 強制修改許可權
-R 對目錄遞迴修改許可權
-v 顯示修改過後的的資訊
樣本chmod u+x
$ ls -l temp -rw-r--r-- 1 siu siu 0 1月 10 13:50 temp$ chmod u+x temp $ ls -l temp -rwxr--r-- 1 siu siu 0 1月 10 13:50 temp
列出檔案詳細資料,可看到開頭有-rw-r--r--,排除第一位,後面的每三位代表一種使用者類型,-表示無設定
此處為檔案所有者添加執行許可權
chmod ug+x
$ ls -l temp -rwxr--r-- 1 siu siu 0 1月 10 13:50 temp$ chmod ug=rwx temp $ ls -l temp -rwxrwxr-- 1 siu siu 0 1月 10 13:50 temp
為檔案所有者和組使用者添加執行許可權
chmod g-x
$ ls -l temp -rwxrwxr-- 1 siu siu 0 1月 10 13:50 temp$ chmod g-x temp $ ls -l temp -rwxrw-r-- 1 siu siu 0 1月 10 13:50 temp
為組使用者減去執行許可權
chmod 777
$ ls -l temp -rwxrw-r-- 1 siu siu 0 1月 10 13:50 temp$ chmod 777 temp $ ls -l temp -rwxrwxrwx 1 siu siu 0 1月 10 13:50 temp
為所有使用者添加可讀可寫可執行許可權
chmod 755
$ ls -l temp -rwxrwxrwx 1 siu siu 0 1月 10 13:50 temp$ chmod 755 temp $ ls -l temp -rwxr-xr-x 1 siu siu 0 1月 10 13:50 temp
為所有者添加讀、寫和執行許可權,組使用者和其他使用者添加讀和執行許可權
chmod -Rv 755
$ ls -l總用量 4drwxr-xr-x 2 siu siu 4096 1月 10 13:57 dir$ chmod -Rv 755 dir"dir" 的許可權模式保留為0755 (rwxr-xr-x)
遞迴為檔案夾添加許可權,並顯示許可權添加資訊
Tips
1.檔案夾必須先要有執行許可權才可讀寫
2.除了以上基本的使用者權限外,還有setuid、setgid和粘滯位等設定,有點進階。俺這裡是簡明筆記