Linux 中檔案許可權的檢視與管理 acl 許可權
一.檔案許可權
1.檔案許可權存在的意義:
系統最底層安全設定方法之一
保證檔案可以被可用的使用者做相應操作
2.檔案許可權的檢視
ls -l file1 檢視檔案file 1的許可權
ls -ld xie 檢視目錄xie 的許可權
ls -l dir 檢視目錄xie底下檔案的許可權
ll file1 檢視檔案file1 的許可權
ll -d xie 檢視目錄xie的許可權
3.檔案許可權的讀取
每類佔用一個位元組
- |rw-rw-r–| 1 |kiosk | kiosk | 0 | Jul 21 09:18 | file
[1] [2] [3] [4] [5] [6] [7] [8]
[1]
檔案的型別
- 空檔案,或者文字
d 目錄
l 軟連結 (快捷方式)
s socket 套接字
b block 塊裝置
c 字元裝置
[2]
檔案的許可權
rw-|rw-|r–
1 2 3
1.[u] 檔案擁有者對檔案能做什麼操作
2.[g] 檔案所有組對檔案能做什麼操作
3.[o] 其他人對檔案能做什麼操作
[3]
對檔案:檔案硬連結個數(檔案內容被記錄的次數)
對目錄:目錄中子目錄的個數
[4]
檔案的所有人
[5]
檔案所有組
[6]
對檔案:檔案大小
對目錄:目錄中子檔案元資料(matedate可以理解為檔案的屬性)大小
[7]
檔案的內容被修改的時間
[8]
檔案的名稱
4.如何改變檔案的所有人和所有組
chown|chgrp
chown username file|dir 檔案file或者目錄dir的所有人為username
chown usergroup file|dir
chown -R user.group dir
chgrp group file|dir
chgrp -R group dir
5.如何改變檔案的許可權
1.對許可權的理解
r
對檔案:是否可以檢視檔案中的內容 —>cat file
對目錄:是否可以檢視目錄中有什麼子檔案或者子目錄 —> ls dir
w
對檔案:是否可以改變檔案裡面記錄的字元
對目錄:是否可以對目錄中子目錄或子檔案的元資料進行更改
x
對檔案:是否可以通過檔名稱呼叫檔案內記錄的程式
對目錄:是否可以進入目錄
2.更改方式
監測
touch /mnt/file{1…3}
mkdir /mnt/linux
touch /mnt/linux/westos{1…3}
ls -lR /mnt
watch -n 1 ls -lR /mnt
chmod <u|g|o><+|-|=><r|w|x> file|dir
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
r-x|r–|--x
5 4 1
#file3 所有人對檔案具有rwx 許可權
所有組對檔案具有r 許可權
其他人對檔案具有x許可權
7=rwx
6=rw-
5=r-x
4=r–
3=-wx
2=-w-
1=–x
0=—
#檔案所有者對檔案增加寫的許可權
6.umask
umask 系統建立檔案是預設保留的權力
umask 077 ##臨時設定系統預留許可權為077
永久更改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配置檔案
70 if [ $UID -gt 199 ] && [ “id -gn
” = “id -un
” ]; then
71 umask 002
72 else
73 umask 077
74 fi
source /etc/profile ##讓更改立即生效
source /etc/bashrc
檢視 umask
#原始檔案
#修改檔案
7.特殊許可權
1.sticky ##粘制位
作用:
只針對目錄生效,當一個目錄上有sticky許可權時
在這個目錄中的檔案只能被檔案的所有者刪除
設定方式:
chmod o+t dir
chmod 1xxx dir xxx表示原有許可權
2.sgid 強制位
作用
對檔案: 只針對與二進位制可執行檔案
當檔案上有sgid時任何人執行此檔案產成的程序都屬於檔案的的組
對目錄:
當目錄上有sgid許可權時任何人在此目錄中建立的檔案都屬於目錄的所有組
設定方式
chmod g+s file1 給檔案file1 加上s 特權
chmod 2xxx file1給檔案file1 加上s 特權 並且u具有rw 特權 g具有r特權,o具有r特權
**退出 chmod g-s /mnt/file1**
或者 chmod 0644 /mnt/file1
3.suid ##冒險位
只針對與2進位制可執行檔案
當檔案上有suid時任何人執行這個檔案中記的程式產生的程序都屬於檔案的所有人
設定方式
chmod u+s file1 檔案file1 開啟冒險位
**chmod 4xxx file1** 檔案file1 開啟冒險位
s和T共存 chmod 3777 /mnt/westos2
acl許可權列表
(acl 即訪問許可權列表)
1.作用
讓特定的使用者對特定的檔案擁有特定許可權
2.acl列表檢視
-rw-rwxr–+ 1 root root 0 Jul 21 15:45 file
“+” 說明acl 開啟
getfacl file 檢視acl開啟的檔案的許可權
# file: file 檔名稱
# owner: root 檔案擁有者
# group: root 檔案擁有組
user::rw- 檔案擁有人的許可權
user:kiosk:rwx ##指定使用者的許可權
group::r-- ##檔案擁有組的權力
mask::rwx ##能賦予使用者的最大許可權值
other::r-- ##其他人的許可權
3.acl列表的管理
getfacl file 檢視
watch -n 1 ‘ls -lR /mnt/ ;getfacl /mnt/westos(檔名)’ 監控
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列表
4.mask值
在許可權列表中mask標示能生效的權力值
當用chmod減小開啟acl的檔案許可權時mask值會發生改變
chmod g-w westos 將檔案westos 的擁有組去掉w即寫的許可權
如果要恢復mask值
setfacl -m m:rw westos
5.acl的預設許可權設定
acl預設許可權只針對目錄設定
“acl許可權只針對設定完成之後新建立的檔案或目錄生效,而已經存在的檔案是不會繼承預設許可權”
setfacl -m d:u:student:rwx /mnt/westos
去掉許可權
setfacl -K /mnt/westos
練習題:
1,新建使用者組 shengchan caiwu ,jishu
2,adminsc 的初始組為shengchan
admincw 的初始組為caiwu
adminjs 的初始組為jishu
tom 是shengchan的附加使用者
harry 是caiwu 組的附加使用者
leo 是jishu組 的附加使用者
(1)/cw 目錄所有人為admincw ,此目錄為財務部儲存目錄,只能對財務部人員寫入
(2)/sc目錄所有人為adminjs ,此目錄為生產部儲存目錄,只能對生產部人員寫入
(3):/js 目錄所有人為adminsc ,此目錄為技術部儲存目錄,只能對技術部人員寫入