目錄
- 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
在linux中只有root能改變檔案所有者,即便是創建者都不可以
檔案的創建者為默認的所有者,此時默認的所屬組也是檔案創建者
linux中檔案夾的預設權限時rwxr-xr-x,檔案的預設權限是rw-r--r--,新建檔案不具備可執行權限
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/137656.html
標籤:Linux
上一篇:linux的磁區和檔案系統
下一篇:常用MySQL操作
