Linux運維入門~6.檔案許可權管理
出於安全的考慮,多使用者作業系統需要具備保障個人隱私和系統安全的機制。在Linux中,無論是文件還是硬體裝置都以檔案的形式存在,
相應地,安全機制也主要表現為對檔案訪問許可權的控制。要保證相應的檔案被可用的使用者操作。
本節就是linux相應的檔案許可權管理。
1.檢視檔案許可權
ls -l 檔案
ls -ld 目錄
ll 檔案
ll -d 目錄
2.檔案許可權的讀取
1)檔案型別
- rw- r-- r-- 1 root root 0 Oct 8 07:30 westos
- 空檔案,或者文字
d 目錄
l 軟連結
s socket 套接字
b block 塊裝置
c 字元裝置
2)檔案許可權
rw- r-- r--
1 2 3
1.[u] 檔案擁有者對檔案能做什麼操作
2.[g] 檔案所有組對檔案能做什麼操作
3.[o] 其他人對檔案能做什麼操作
3)- rw- r-- r-- 1 root root 0 Oct 8 07:30 westos
對檔案:檔案硬連結個數(檔案內容被記錄的次數)
對目錄:目錄中子目錄的個數
4)- rw- r-- r-- 1 root root 0 Oct 8 07:30 westos
檔案的所有人
5)- rw- r-- r-- 1 root root 0 Oct 8 07:30 westos
檔案所有組
6)- rw- r-- r-- 1 root root 0 Oct 8 07:30 westos
對檔案:檔案大小
對目錄:目錄中子檔案元資料(matedate可以理解為檔案的屬性)大小
7)- rw- r-- r-- 1 root root 0 Oct 8 07:30 westos
檔案被修改的時間
8)- rw- r-- r-- 1 root root 0 Oct 8 07:30 westos
檔案的名稱
3.改變檔案的所有人和所有組
命令:chown|chgrp
chown username 檔案|目錄
chown user.group 檔案|目錄
chown -R user.group 目錄
chgrp group 檔案|目錄
chgrp -R group 目錄
注意:R 表示遞迴,即目錄下都改變
4.改變檔案的許可權
1)rwx三種許可權
r
對檔案:是否可以檢視檔案中的內容 --->cat file
對目錄:是否可以檢視目錄中有什麼子檔案或者子目錄 ---> ls dir
w
對檔案:是否可以改變檔案裡面記錄的字元
對目錄:是否可以對目錄中子目錄或子檔案的元資料進行更改
x
對檔案:是否可以通過檔名稱呼叫檔案內記錄的程式
對目錄:是否可以進入目錄
2)更改方式
命令:chmod <u|g|o><+|-|=><r|w|x> 檔案|目錄
例:
chmod u+x /mnt/file1
chmod g-r /mnt/file2
chmod ug-r /mnt/file3
chmod u-r,g+x /mnt/file4
chmod -r /mnt/file5
chmod o=r-x /mnt/file6
另:
rwx
210
r=4
w=2
x=1
rw-|r--|r-- =644
chmod 644 file
5.umask
系統建立檔案是預設保留的權力
永久更改umask
vim /etc/profile 系統配置檔案
59 if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
60 umask 002 普通使用者的umask
61 else
62 umask 077 超級使用者的umask
63 fi
vim /etc/bashrc ##shell配置檔案
source /etc/profile
source /etc/bashrc 重新讀取檔案,可以使改動立即生效
6.特殊許可權
1)sticky 粘制位
作用:只針對目錄生效,當一個目錄上有sticky許可權時,在這個目錄中的檔案智慧被檔案的所有者刪除
設定方式:
chmod o+t 目錄
chmod 1xxx 目錄
2)sgid 強制位
作用 對檔案:只針對與二進位制可執行檔案當檔案上有sgid時任何人執行此檔案產成的程序都屬於檔案的的組
對目錄:當目錄上有sgid許可權時任何人在此目錄中建立的檔案都屬於目錄的所有組
設定方式
chmod g+s 檔案|目錄
chmod 2xxx 檔案|目錄
3)suid 冒險位
作用:只針對與2進位制可執行檔案,當檔案上有suid時任何人執行這個檔案中記的程式產生的程序都屬於檔案的所有人
設定方式
chmod u+s 檔案
chmod 4xxx 檔案
7.acl許可權
讓特定的使用者對特定的檔案擁有特定的許可權
rw-rwxr--+........acl開啟
getfacl file 檢視acl開啟的檔案的許可權
user::rw- 檔案擁有人的許可權
user:kiosk:rwx 指定使用者的許可權
group::r-- 檔案擁有組的權力
mask::rwx 能賦予使用者的最大權力伐值
other::r-- 其他人的許可權
1)acl列表的管理
setfacl -m u:username:rwx file 設定username對file擁有rwx許可權
setfacl -m g:group:rwx file 設定group組成員對file擁有rwx許可權
setfacl -x u:username file 從acl列表中刪除username
setfacl -b file 關閉file上的acl列表
2)mask值
在許可權列表中mask標示能生效的權力值
當用chmod減小開啟acl的檔案許可權時mask值會發生改變
chmod g-w westos
如果要恢復mask值
setfacl -m m:rw westos
3)acl的預設許可權設定
acl預設許可權只針對目錄設定
注意:acl許可權只針對設定完成之後新建立的檔案或目錄生效,而已經存在的檔案是不會繼承預設許可權
setfacl -m d:u:student:rwx /mnt/westos
寫在最後,如果大家喜歡linux運維方面的知識,歡迎關注本博主,會每週更新與linux運維相關的一些小知識,幫助大家入門
願你與我一同成長!
往期:
ttps://blog.csdn.net/Stella_Pooter/article/details/82951345 Linux運維入門~1.虛擬機器使用
https://blog.csdn.net/Stella_Pooter/article/details/82952307 Linux運維入門~2.命令列使用技巧
https://blog.csdn.net/Stella_Pooter/article/details/82960344 Linux運維入門~3.檔案管理
https://blog.csdn.net/Stella_Pooter/article/details/82971516 Linux運維入門~4.輸入輸出管理與vim管理
https://blog.csdn.net/Stella_Pooter/article/details/82983752 Linux運維入門~5.使用者管理
Never say die