新聞中心
umask
linux操作系統對多用戶的管理,是非常繁瑣的,所以用組的概念來管理用戶就變得簡單,每個用戶可以在一個獨立的組,每個組也可以有零個用戶或者多個用戶。本文給大家介紹linux中權限管理命令詳解(Chmod/chown/chgrp/unmask),具體內容如下:

創(chuàng)新互聯是一家集網站建設、網站設計、網站頁面設計、網站優(yōu)化SEO優(yōu)化為一體的專業(yè)網站建設公司,已為成都等多地近百家企業(yè)提供網站建設服務。追求良好的瀏覽體驗,以探求精品塑造與理念升華,設計最適合用戶的網站頁面。 合作只是第一步,服務才是根本,我們始終堅持講誠信,負責任的原則,為您進行細心、貼心、認真的服務,與眾多客戶在蓬勃發(fā)展的市場環(huán)境中,互促共生。
chmod
解釋
命令名稱:chmod 命令英文原意:change the permissions mode of a file 命令所在路徑:/bin/chmod 執(zhí)行權限:所有用戶功能描述:改變文件或目錄權限
語法
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
權限特別注意
# 在/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 | 寫權限 | 可以修改文件內容 | 可以在目錄中創(chuàng)建和刪除文件 |
| x | 執(zhí)行權限 | 可以執(zhí)行文件 | 可以進入目錄 |
分析
對于文件有寫權限,僅僅代表可以修改文件的內容,而沒有刪除文件的權限
對于目錄有寫權限,可以在目錄中創(chuàng)建和刪除文件
因為上面的/tmp/test目錄的權限為777 所以普通用戶對于/tmp/test目錄也具有創(chuàng)建文件和刪除文件的權限所以,普通用戶也能刪除/tmp/test/test.txt文件但是普通用戶無法編輯/tmp/test/test.txt文件,使用vim編輯文件的時候,會提示Waring: Changing a readonly file
chown
解釋
命令名稱:chown 命令英文原意:change file ownership 命令所在路徑:/bin/chown 執(zhí)行權限:所有用戶功能描述:改變文件或目錄的所有者
語法
chown [用戶] [文件或目錄]
在linux中只有root能改變文件所有者,即便是創(chuàng)建者都不可以
示例
# 改變文件所有者(將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
執(zhí)行權限:所有用戶
功能描述:改變文件或目錄的所屬組
語法
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內置命令執(zhí)行權限:所有用戶功能描述:顯示/設置文件的缺省權限
語法
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值之后,缺省權限變?yōu)?br /> 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能改變文件所有者,即便是創(chuàng)建者都不可以文件的創(chuàng)建者為默認的所有者,此時默認的所屬組也是文件創(chuàng)建者 linux中文件夾的缺省權限時rwxr-xr-x,文件的缺省權限是rw-r–r–,新建文件不具備可執(zhí)行權限
到此這篇關于linux中權限管理命令詳解(chmod/chown/chgrp/unmask)的文章就介紹到這了,更多相關linux 權限管理命令內容請搜索以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持!
成都創(chuàng)新互聯科技有限公司,是一家專注于互聯網、IDC服務、應用軟件開發(fā)、網站建設推廣的公司,為客戶提供互聯網基礎服務!
創(chuàng)新互聯(www.cdcxhl.com)提供簡單好用,價格厚道的香港/美國云服務器和獨立服務器。創(chuàng)新互聯成都老牌IDC服務商,專注四川成都IDC機房服務器托管/機柜租用。為您精選優(yōu)質idc數據中心機房租用、服務器托管、機柜租賃、大帶寬租用,可選線路電信、移動、聯通等。
網站題目:linux中權限管理命令詳解(chmod/chown/chgrp/unmask)
標題來源:http://fisionsoft.com.cn/article/dhpdggs.html


咨詢
建站咨詢
