標籤:檔案夾 linux 使用者
chmod
作用:變更檔或檔案夾許可權
注釋:
a 所有使用者
u 所有者
g 所有組
o 其它人
rwx 對應許可權分別為4、2、1
常用選項:
-R 可遞迴遍曆子目錄,把修改應到目錄下所有檔案和子目錄
執行個體:
[[email protected] ~]# touch 123[[email protected] ~]# mkdir 321[[email protected] ~]# lltotal 4-rw-r--r-- 1 root root 0 Mar 31 12:39 123drwxr-xr-x 2 root root 4096 Mar 31 12:39 321[[email protected] ~]# chmod a+w 123[[email protected] ~]# lltotal 4-rw-rw-rw- 1 root root 0 Mar 31 12:39 123drwxr-xr-x 2 root root 4096 Mar 31 12:39 321[[email protected] ~]# chmod u=r-- 321[[email protected] ~]# lltotal 4-rw-rw-rw- 1 root root 0 Mar 31 12:39 123dr--r-xr-x 2 root root 4096 Mar 31 12:39 321[[email protected] ~]# chmod o-x 321[[email protected] ~]# lltotal 4-rw-rw-rw- 1 root root 0 Mar 31 12:39 123dr--r-xr-- 2 root root 4096 Mar 31 12:39 321[[email protected] ~]# chmod 777 123[[email protected] ~]# lltotal 4-rwxrwxrwx 1 root root 0 Mar 31 12:39 123dr--r-xr-- 2 root root 4096 Mar 31 12:39 321[[email protected] ~]# [[email protected] ~]# mkdir 123[[email protected] ~]# touch 123/321[[email protected] ~]# lltotal 4drwxr-xr-x 2 root root 4096 Mar 31 12:41 123[[email protected] ~]# ll 123/321 -rw-r--r-- 1 root root 0 Mar 31 12:41 123/321[[email protected] ~]# chmod 777 123[[email protected] ~]# ll 123total 0-rw-r--r-- 1 root root 0 Mar 31 12:41 321[[email protected] ~]# ll -d 123drwxrwxrwx 2 root root 4096 Mar 31 12:41 123[[email protected] ~]# chmod -R 777 123[[email protected] ~]# ll 123total 0-rwxrwxrwx 1 root root 0 Mar 31 12:41 321[[email protected] ~]#
chown
作用:改變使用者的所有者或所有組
常用選項:
-R 可遞迴遍曆子目錄,把修改應到目錄下所有檔案和子目錄
執行個體:
[[email protected] ~]# mkdir one[[email protected] ~]# mkdir two[[email protected] ~]# mkdir three[[email protected] ~]# lltotal 12drwxr-xr-x 2 root root 4096 Mar 31 12:47 onedrwxr-xr-x 2 root root 4096 Mar 31 12:47 threedrwxr-xr-x 2 root root 4096 Mar 31 12:47 two[[email protected] ~]# chown jacken one[[email protected] ~]# lltotal 12drwxr-xr-x 2 jacken root 4096 Mar 31 12:47 onedrwxr-xr-x 2 root root 4096 Mar 31 12:47 threedrwxr-xr-x 2 root root 4096 Mar 31 12:47 two[[email protected] ~]# chown jacken: two[[email protected] ~]# lltotal 12drwxr-xr-x 2 jacken root 4096 Mar 31 12:47 onedrwxr-xr-x 2 root root 4096 Mar 31 12:47 threedrwxr-xr-x 2 jacken jacken 4096 Mar 31 12:47 two[[email protected] ~]# chown jacken:user1 three[[email protected] ~]# lltotal 12drwxr-xr-x 2 jacken root 4096 Mar 31 12:47 onedrwxr-xr-x 2 jacken user1 4096 Mar 31 12:47 threedrwxr-xr-x 2 jacken jacken 4096 Mar 31 12:47 two[[email protected] ~]#[[email protected] ~]# mkdir 1[[email protected] ~]# touch 1/file_1[[email protected] ~]# mkdir 2[[email protected] ~]# touch 2/file_2[[email protected] ~]# lltotal 8drwxr-xr-x 2 root root 4096 Mar 31 12:49 1drwxr-xr-x 2 root root 4096 Mar 31 12:49 2[[email protected] ~]# ll 1 21:total 0-rw-r--r-- 1 root root 0 Mar 31 12:49 file_12:total 0-rw-r--r-- 1 root root 0 Mar 31 12:49 file_2[[email protected] ~]# chown jacken:user1 1[[email protected] ~]# chown -R jacken:user1 2[[email protected] ~]# lltotal 8drwxr-xr-x 2 jacken user1 4096 Mar 31 12:49 1drwxr-xr-x 2 jacken user1 4096 Mar 31 12:49 2[[email protected] ~]# ll 1 21:total 0-rw-r--r-- 1 root root 0 Mar 31 12:49 file_12:total 0-rw-r--r-- 1 jacken user1 0 Mar 31 12:49 file_2[[email protected] ~]#
chgrp
作用:改變檔案或檔案夾的組
常用選項:
-R 遍曆子目錄,把修改應到目錄下所有檔案和子目錄
執行個體:
[[email protected] ~]# mkdir hi[[email protected] ~]# touch hi/hi_hi[[email protected] ~]# mkdir hello[[email protected] ~]# touch hello/hello_hello[[email protected] ~]# lltotal 8drwxr-xr-x 2 root root 4096 Mar 31 12:52 hellodrwxr-xr-x 2 root root 4096 Mar 31 12:52 hi[[email protected] ~]# ll hi/ hello/hello/:total 0-rw-r--r-- 1 root root 0 Mar 31 12:52 hello_hellohi/:total 0-rw-r--r-- 1 root root 0 Mar 31 12:52 hi_hi[[email protected] ~]# chgrp jacken hello[[email protected] ~]# lltotal 8drwxr-xr-x 2 root jacken 4096 Mar 31 12:52 hellodrwxr-xr-x 2 root root 4096 Mar 31 12:52 hi[[email protected] ~]# ll hello/total 0-rw-r--r-- 1 root root 0 Mar 31 12:52 hello_hello[[email protected] ~]# chgrp -R jacken hi[[email protected] ~]# ll hi/total 0-rw-r--r-- 1 root jacken 0 Mar 31 12:52 hi_hi[[email protected] ~]#
lsattr
作用:查看檔案的隱藏許可權
常用選項:
-R:遞迴列示目錄及檔案屬性。
-V:顯示程式版本號碼。
-a:顯示所有檔案屬性,包括隱藏檔案(.)、當時目錄(./)及上層目錄(../)。
-d:僅列示目錄屬性。
-l:(此參數目前沒有任何作用)。
-v:顯示檔案或目錄版本。
執行個體:
[[email protected] ~]# lsattr -R-------------e- ./123-------------e- ./hi./hi:-------------e- ./hi/hi_hi-------------e- ./hello./hello:-------------e- ./hello/hello_hello[[email protected] ~]#
chattr
作用:修改檔案或目錄隱藏許可權
常用選項:
-R:遞迴處理,將指定目錄下的所有檔案及子目錄一併處理
+ :在原有參數設定基礎上,追加參數。
- :在原有參數設定基礎上,移除參數。
= :更新為指定參數設定
A:檔案或目錄的 atime (access time)不可被修改(modified), 可以有效預防例如手提電腦磁碟I/O錯誤的發生
a:即append,設定該參數後,只能向檔案中添加資料,而不能刪除,多用於伺服器記錄檔安全,只有root才能設定這個屬性
d:即no dump,設定檔案不能成為dump程式的備份目標
i:設定檔案不能被刪除、改名、設定連結關係,同時不能寫入或新增內容。i參數對於檔案 系統的安全設定有很大協助
執行個體:
[[email protected] ~]# touch 123[[email protected] ~]# chattr +i 123[[email protected] ~]# lsattr 123----i--------e- 123[[email protected] ~]# echo hi > 123-bash: 123: Permission denied[[email protected] ~]# echo hi >> 123-bash: 123: Permission denied[[email protected] ~]# vim 123[[email protected] ~]# rm -rf 123rm: cannot remove `123‘: Operation not permitted[[email protected] ~]#[[email protected] ~]# touch 456[[email protected] ~]# chattr +a 456[[email protected] ~]# lsattr 456 -----a-------e- 456[[email protected] ~]# vim 456[[email protected] ~]# cat 456[[email protected] ~]# echo new > 456-bash: 456: Operation not permitted[[email protected] ~]# echo new2 >> 456[[email protected] ~]# cat 456new2[[email protected] ~]# rm -rf 456rm: cannot remove `456‘: Operation not permitted[[email protected] ~]#
setfacl
作用:設定特殊許可權
常用選項:
-m 修改檔案或目錄的acl規則
-x 刪除檔案或目錄的acl規則
-d 設定預設的acl規則
執行個體:
[[email protected] ~]# cd /[[email protected] /]# touch 123[[email protected] /]# setfacl -m jacken:rw- 123[[email protected] /]# getfacl 123# file: 123# owner: root# group: rootuser::rw-user:jacken:rw-group::r--mask::rw-other::r--[[email protected] /]# su - user1[[email protected] ~]$ echo "hi" > /123-bash: /123: Permission denied[[email protected] ~]$ exitlogout[[email protected] /]# su - jacken[[email protected] ~]$ echo "hi" > /123[[email protected] ~]$ cat /123hi[[email protected] ~]$ [[email protected] /]# setfacl -x u:jacken /123[[email protected] /]# getfacl /123getfacl: Removing leading ‘/‘ from absolute path names# file: 123# owner: root# group: rootuser::rw-group::r--mask::r--other::r--[[email protected] /]#
本文出自 “一步步踏入Linux世界” 部落格,謝絕轉載!
Linux常用的基本命令11