chmod -change file mode bits :更改文件權限
chmod是用來改變文件或者目錄權限的命令,但只有文件的屬主和超級用戶(root)才有這種權限。
更改文件權限的2種方式:
一、權限字母+操作符表達式
二、數字方法(常用)
hmod數字權限方法(推進)
命令格式:
chmod [數字組合] 文件名
chmod [數字組合] 目錄名 -R參數可遞歸生效(該目錄下所有文件或子目錄一起改變)
一、chmod的數字方法的說明:
r 4 w 2 x 1 - 0
例如:
rwxr-xr-x 755 目錄默認權限
rw-r--r-- 644文件默認權限
每個三位的權限代碼(屬主,用戶組,其他用戶)組合,有8種可能:
八進制 權限 0 --- 1 --x 2 -w- 3 -wx 4 r-- 5 r-x 6 rw- 7 rwx
舉例:
rw-rw-r-x 代表數字權限:665
--xr-x-wx 代表數字權限:163
-wx--x--x 代表數字權限:311
rwx--xr-x 代表數字權限:715
-----x-w- 代表數字權限:012
如果我們僅僅想改變目錄的權限,使用chmod不用加任何參數。如果想把目錄下的文件和子目錄也同時改變,需要使用
-R參數
chmod字符式權限表示法
命令格式:
Chmod [用戶類型] [+ | - | =] [權限字符] 文件名
表一 詳細說明表
chmod |
用戶類型 |
操作字符 |
權限字符 |
文件和目錄 |
U(user) |
+(增加) |
r |
||
G(group |
|
- |
||
O(others) |
-(減少) |
w |
||
A(all) |
=(設置) |
x |
說明:
+:添加某個權限 -:取消某個權限 =:取消其他所有權限賦予給定的權限
chmod u-x test.sh
[root@MongoDB ~]# chmod u-x test.sh [root@MongoDB ~]# ll total 4 -rw-------. 1 root root 1851 Mar 27 08:38 anaconda-ks.cfg -rw-r-xr-x 1 root root 0 Jun 5 06:43 test.sh
chmod g+w test.sh
[root@MongoDB ~]# chmod g+w test.sh [root@MongoDB ~]# ll total 4 -rw-------. 1 root root 1851 Mar 27 08:38 anaconda-ks.cfg -rw-rwxr-x 1 root root 0 Jun 5 06:43 test.sh
chmod g=w,o-x test.sh
[root@MongoDB ~]# chmod g=w,o-x test.sh [root@MongoDB ~]# ll total 4 -rw-------. 1 root root 1851 Mar 27 08:38 anaconda-ks.cfg -rw--w-r-- 1 root root 0 Jun 5 06:43 test.sh
chmod ugo=r test.sh
[root@MongoDB ~]# chmod ugo=r test.sh [root@MongoDB ~]# ll total 4 -rw-------. 1 root root 1851 Mar 27 08:38 anaconda-ks.cfg -r--r--r-- 1 root root 0 Jun 5 06:43 test.sh
chmod a=rw test.sh
a代表所有 相當於 chmod 777 test.sh
[root@MongoDB ~]# chmod a=rwx test.sh [root@MongoDB ~]# ll total 4 -rw-------. 1 root root 1851 Mar 27 08:38 anaconda-ks.cfg -rwxrwxrwx 1 root root 0 Jun 5 06:43 test.sh