目錄 chmod chown chgrp umask chmod 解釋 語法 示例 許可權特別註意 root用戶進入的時候顯示 ,普通用戶顯示$ 只有管理員擁有rw讀寫許可權,所屬組和其他人只有讀許可權,但是此時普通用戶卻刪除了只有r讀許可權的文件,為什麼???? 文件目錄許可權總結 代表字元 | 許可權 | ...
目錄
- chmod
- chown
- chgrp
- umask
chmod
解釋
命令名稱:chmod
命令英文原意:change the permissions mode of a file
命令所在路徑:/bin/chmod
執行許可權:所有用戶
功能描述:改變文件或目錄許可權
語法
chmod [{ugoa}{+-=}{rwx}] [文件或目錄]
chmod [mode=421] [文件或目錄]
-R 遞歸修改
# 第一種修改方式 chmod [{ugoa}{+-=}{rwx}] [文件或目錄]
ugoa:
u:所有者
g:所屬組
o:其他人
a:所有人
+-=:
+:針對文件或目錄增加某個許可權
-:針對文件或目錄減少某個許可權
=:賦予文件或目錄全新的許可權,以此刻的許可權為準
# 第二種修改方式 chmod [mode=421] [文件或目錄]
rwx:
r:4
w:2
x:1
rwxrw-r--
許可權:764(4+2+1=7/4+2=6/4)
示例
# 第一種增加許可權
chmod g+x test.txt
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -l test.txt
-rw-r--r-- 1 root root 11 Nov 28 15:39 test.txt
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# chmod g+x test.txt
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -l test.txt
-rw-r-xr-- 1 root root 11 Nov 28 15:39 test.txt
# 第二種增加許可權
chmod 777 test.txt
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -l test.txt
-rw-r-xr-- 1 root root 11 Nov 28 15:39 test.txt
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# chmod 777 test.txt
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -l test.txt
-rwxrwxrwx 1 root root 11 Nov 28 15:39 test.txt
許可權特別註意
root用戶進入的時候顯示#,普通用戶顯示$
# 在/tmp下新建文件夾test
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# mkdir test
# 在/tmp/test文件夾下新建test.txt
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# touch test/test.txt
# 查看test文件下的文件
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -l test
total 0
-rw-r--r-- 1 root root 0 Nov 28 17:54 test.txt
# 查看/tmp/test文件夾的許可權
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -ld test
drwxr-xr-x 2 root root 4096 Nov 28 17:54 test
# 賦予/tmp/test文件夾全部的許可權
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# chmod 777 test
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -ld test
drwxrwxrwx 2 root root 4096 Nov 28 17:54 test
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# ls -l test/test.txt
-rw-r--r-- 1 root root 0 Nov 28 17:54 test/test.txt
# 新增加一個普通用戶並修改密碼
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# useradd eternity
[root@izm5e2q95pbpe1hh0kkwoiz tmp]# passwd eternity
# 使用eternity帳號,密碼123456,登錄伺服器
# 查看當前目錄
[eternity@izm5e2q95pbpe1hh0kkwoiz ~]$ pwd
/home/eternity
# 進入/tmp目錄
[eternity@izm5e2q95pbpe1hh0kkwoiz ~]$ cd /tmp
# 查看/tmp/test目錄的許可權,擁有全部許可權
[eternity@izm5e2q95pbpe1hh0kkwoiz tmp]$ ls -ld test
drwxrwxrwx 2 root root 4096 Nov 28 17:54 test
# /tmp/test目錄下存在test.txt,擁有讀許可權
[eternity@izm5e2q95pbpe1hh0kkwoiz tmp]$ ls -l test/test.txt
-rw-r--r-- 1 root root 0 Nov 28 17:54 test/test.txt
# 刪除/tmp/test下的test.txt文件
[eternity@izm5e2q95pbpe1hh0kkwoiz tmp]$ rm test/test.txt
rm: remove write-protected regular empty file ‘test/test.txt’? y
# 刪除成功,此時/tmp/test目錄下test.txt已經沒有了
[eternity@izm5e2q95pbpe1hh0kkwoiz tmp]$ ls -l test/test.txt
ls: cannot access test/test.txt: No such file or directory
只有管理員擁有rw讀寫許可權,所屬組和其他人只有讀許可權,但是此時普通用戶卻刪除了只有r讀許可權的文件,為什麼????
文件目錄許可權總結
代表字元 | 許可權 | 對文件的含義 | 對目錄的含義 |
---|---|---|---|
r | 讀許可權 | 可以查看文件內容 | 可以列出目錄中的內容 |
w | 寫許可權 | 可以修改文件內容 | 可以在目錄中創建和刪除文件 |
x | 執行許可權 | 可以執行文件 | 可以進入目錄 |
分析
對於文件有寫許可權,僅僅代表可以修改文件的內容,而沒有刪除文件的許可權
對於目錄有寫許可權,可以在目錄中創建和刪除文件
因為上面的/tmp/test目錄的許可權為777
所以普通用戶對於/tmp/test目錄也具有創建文件和刪除文件的許可權
所以,普通用戶也能刪除/tmp/test/test.txt文件
但是普通用戶無法編輯/tmp/test/test.txt文件,使用vim編輯文件的時候,會提示Waring: Changing a readonly file
chown
解釋
命令名稱:chown
命令英文原意:change file ownership
命令所在路徑:/bin/chown
執行許可權:所有用戶
功能描述:改變文件或目錄的所有者
語法
chown [用戶] [文件或目錄]
在linux中只有root能改變文件所有者,即便是創建者都不可以
示例
# 改變文件所有者(將test.txt的所有者由eternity更改為root)
chown root /tmp/test/test.txt
[root@izm5e2q95pbpe1hh0kkwoiz ~]# pwd
/root
[root@izm5e2q95pbpe1hh0kkwoiz ~]# ls -l /tmp/test/test.txt
-rw-r--r-- 1 eternity eternity 7 Nov 28 18:15 /tmp/test/test.txt
[root@izm5e2q95pbpe1hh0kkwoiz ~]# chown root /tmp/test/test.txt
[root@izm5e2q95pbpe1hh0kkwoiz ~]# ls -l /tmp/test/test.txt
-rw-r--r-- 1 root eternity 7 Nov 28 18:15 /tmp/test/test.txt
chgrp
解釋
命令名稱:chgrp
命令英文原意:change file group ownership
命令所在路徑:/bin/chgrp
執行許可權:所有用戶
功能描述:改變文件或目錄的所屬組
語法
chgrp [用戶組] [文件或目錄]
示例
# 改變文件所屬組(將test.txt的所屬組由eternity更改為eternityz)
chgrp eternityz /tmp/test/test.txt
# 當前目錄
[root@izm5e2q95pbpe1hh0kkwoiz ~]# pwd
/root
# 查看詳細信息
[root@izm5e2q95pbpe1hh0kkwoiz ~]# ls -l /tmp/test/test.txt
-rw-r--r-- 1 root eternity 7 Nov 28 18:15 /tmp/test/test.txt
# 增加eternityz組
[root@izm5e2q95pbpe1hh0kkwoiz ~]# groupadd eternityz
# 改變所屬組
[root@izm5e2q95pbpe1hh0kkwoiz ~]# chgrp eternityz /tmp/test/test.txt
[root@izm5e2q95pbpe1hh0kkwoiz ~]# ls -l /tmp/test/test.txt
-rw-r--r-- 1 root eternityz 7 Nov 28 18:15 /tmp/test/test.txt
umask
解釋
命令名稱:umask
命令英文原意the user file-creation mask
命令所在路徑:shell內置命令
執行許可權:所有用戶
功能描述:顯示/設置文件的預設許可權
語法
umask [-S]
-S 以rwx形式顯示新建文件預設許可權(大寫的S)
示例
# 查看文件的預設許可權
umask -S
# 查看umask
umask
[root@izm5e2q95pbpe1hh0kkwoiz ~]# umask
0022
0022中
0 特殊許可權
022 ----w--w-
# 通過所有許可權777和022許可權進行異或操作,得到預設許可權
777 rwx rwx rwx
022 --- -w- -w-
================
目錄 rwx r-x r-x
文件 rwx r-- r--
# 更改umask值,進而改變預設許可權
umask 077
# 更改umask值之後,預設許可權變為
777 rwx rwx rwx
077 --- rwx rwx
================
目錄 rwx --- ---
文件 rw- --- ---
# 以下實驗符合更改預設許可權的設置
[root@izm5e2q95pbpe1hh0kkwoiz ~]# umask 077
[root@izm5e2q95pbpe1hh0kkwoiz ~]# mkdir /tmp/lyf
[root@izm5e2q95pbpe1hh0kkwoiz ~]# ls -ld /tmp/lyf
drwx------ 2 root root 4096 Nov 29 10:55 /tmp/lyf
[root@izm5e2q95pbpe1hh0kkwoiz ~]# touch /tmp/lyf/lyf
[root@izm5e2q95pbpe1hh0kkwoiz ~]# ls -l /tmp/lyf/lyf
-rw------- 1 root root 0 Nov 29 10:56 /tmp/lyf/lyf