RH124-06 文件系統權限
第六章 文件系統權限
學習目標:
能夠解析Linux文件系統權限的工作機制
掌握實用命令工具修改文件權限和屬主
掌握特權位和粘貼位權限,用戶權限掩碼
6.1 了解基本的文件系統權限
r 讀權限: 對於文件,代表用戶可以讀取文件的內容
對於目錄,代表用戶可以對目錄裏的內容進行列表
w 寫權限: 對於文件,代表用戶可以修改文件的內容
對於目錄,代表用戶可以在目錄裏創建,刪除文件,修改文件名字等
x 執行權限: 對於文件,如果這個文件本身是可執行文件(腳本,命令,程序),代表用戶可以執行(運行)文件
對於目錄,代表用戶可以進入到該目錄
一個用戶要對目錄有基本的正常訪問操作,必須具備r和x權限
用戶對資源來說,有三種角色:
User(u): 主人(文件所有者)
Group(g): 所屬組的成員
Other(O): 其他人
首選判斷用戶是否為文件的所有者,如果是,就按所有者的權限進行訪問
如果不是文件的所有者,就繼續判斷是否為文件的所有組成員,如果是,就按組的權限去訪問
如果不是文件的所有組的成員,最終用戶就是文件的其他的人,按其他人的權限去訪問.
rwx, rwx, r-x
User Group Other
6.2 使用命令工具修改文件權限和屬主
考試必考
相關命令: chmod,chown,chgrp
使用數字描述權限
--- 000 0
--x 001 1
-w- 010 2
-wx 011
r-- 100 4
r-x 101 5
rw- 110 6
rwx 111 7
rwxr-xr-x ==> 755
rw-r--r-- ==> 644
r-------- ==> 400
rw-r--r-- ==> 644
rwxrwxrwx ==> 777
實驗: 在server上lab permissions setup
要求 ateam組的成員可以訪問/home/ateam-text目錄,並且可以在目錄下創建,刪除文件. 在目錄下,andy創建的文件可以讓alice修改
相關用戶的密碼為password
root # mkdir /home/ateam-text
root # chown :ateam /home/ateam-text
root # chmod g=rwx /home/ateam-text
andy $ touch /home/ateam-text/andy-file1
andy $ chmod o=rw /home/ateam-text/andy-file1
andy $ touch /home/ateam-text/andy-file2
andy $ chown :ateam /home/ateam-text/andy-file2
andy $ chmod g=rw /home/ateam-text/andy-file2
6.3 掌握特權位和粘貼位權限,用戶權限掩碼
考試必考
u+s,g+s,o+t
umask
setuid = u+s = 4
文件的執行有效身份為文件的擁有者,而不是執行者的身份
setgid = g+s = 2
文件的有效執行組身份為文件的擁有組,而不是執行者的組身份
目錄裏新建的文件的擁有組會自動繼承目錄的擁有組身份
sticky = o+t = 1
對目錄有寫權限的用戶僅僅可以刪除目錄裏屬於自己的文件,不能刪除其他用戶的文件 tom傳的文件mary不可刪除,管理員不受限制
umask
用於設置默認權限的.
默認新建文件,最大權限為666
默認新建目錄,最大權限是777
相關文件/etc/bashrc,/etc/profile,~/.bashrc,~/.bash_profile
[[email protected] ~]# umask
0022
練習:在server上lab permissions setup
系統會自動建立了三個用戶,分別為curly,larry,moe,這些用戶都是stooges組的成員,帳號密碼都為password
要求以上用戶和組可以在/home/stooges目錄裏訪問,創建,刪除文件. 其他用戶一律不允許訪問該目錄. 在該目錄下新建的文件會自動屬於stooges組擁有
所有普通用戶新建的文件或目錄不能被非組成員(其他人)訪問.
==================================================================================================================================================================
上課筆記:
6.1了解基本的文件系統權限
[[email protected] Desktop]$ file /usr/bin/touch
/usr/bin/touch: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=0xb8a26c6e357429bbb3ba545feaa789c2d36758fc, stripped
用戶對資源來說有三種 :
用戶(文件所有者
所屬於的組(group)
other(其他人)
user group other
首先判斷文件是否是所有者,如果不是則判斷是否所屬的組,如果不是,則判定為other
看權限 ls -ld /tmp/test
student用戶: sudo cho
修改權限的命令:chmod 777 /tmp/test
6.2 使用命令工具修改文件權限和屬主
[[email protected] ~]$ cp /etc/hosts ./
[[email protected] ~]$ ls
Desktop Downloads Music Public Videos
Documents hosts Pictures Templates
[[email protected] ~]$ ls -l
total 4
drwxr-xr-x. 2 student student 6 Jan 5 2015 Desktop
drwxr-xr-x. 2 student student 6 Jan 5 2015 Documents
drwxr-xr-x. 2 student student 6 Jan 5 2015 Downloads
-rw-r--r--. 1 student student 231 May 29 20:27 hosts
drwxr-xr-x. 2 student student 6 Jan 5 2015 Music
drwxr-xr-x. 2 student student 6 Jan 5 2015 Pictures
drwxr-xr-x. 2 student student 6 Jan 5 2015 Public
drwxr-xr-x. 2 student student 6 Jan 5 2015 Templates
drwxr-xr-x. 2 student student 6 Jan 5 2015 Videos
[[email protected] ~]$ chmod u+x,g+w,o-r ./hosts
[[email protected] ~]$ ls -l hosts
-rwxrw----. 1 student student 231 May 29 20:27 hosts
[[email protected] ~]$
[[email protected] ~]$ chmod go=rw ./hosts 不管以前是什麽 ,執行後g和o都是rw
[[email protected] ~]$ chmod u-x,go=r ./hosts
[[email protected] ~]$ ls -l hosts
-rw-r--r--. 1 student student 231 May 29 20:27 hosts
[email protected] ~]$
[[email protected] ~]$ mkdir test
[[email protected] ~]$ ls
Desktop Downloads Music Public test
Documents hosts Pictures Templates Videos
[[email protected] ~]$ cp /etc/passwd ./test/
[[email protected] ~]$ cd test
[[email protected] test]$ ls
passwd
[[email protected] test]$ ls -l
total 4
-rw-r--r--. 1 student student 2210 May 29 20:30 passwd
[[email protected] test]$ cd ..
[[email protected] ~]$ ls
Desktop Downloads Music Public test
Documents hosts Pictures Templates Videos
[[email protected] ~]$ ls ./test/
passwd
[[email protected] ~]$ ls ./test/ -l
total 4
-rw-r--r--. 1 student student 2210 May 29 20:30 passwd
[[email protected] ~]$ ls -ld ./test/
drwxrwxr-x. 2 student student 19 May 29 20:30 ./test/
[[email protected] ~]$ chmod g-w,o=---
chmod: missing operand after ‘g-w,o=---’
Try ‘chmod --help‘ for more information.
[[email protected] ~]$ chmod g-w,o=--- ./test
[[email protected] ~]$ ls -ld ./test
drwxr-x---. 2 student student 19 May 29 20:30 ./test
[[email protected] ~]$
[[email protected] ~]$ chmod -R o=--- ./test
[[email protected] ~]$ ls -l ./test/passwd
-rw-r-----. 1 student student 2210 May 29 20:30 ./test/passwd
[[email protected] ~]$ cd home
bash: cd: home: No such file or directory
[[email protected] ~]$ ls
Desktop Downloads Music Public test
Documents hosts Pictures Templates Videos
[[email protected] ~]$ mkdri test
bash: mkdri: command not found...
Similar command is: ‘mkdir‘
[[email protected] ~]$ ls
Desktop Downloads Music Public test
Documents hosts Pictures Templates Videos
[[email protected] ~]$ mkdir test
mkdir: cannot create directory ‘test’: File exists
[[email protected] ~]$ ls
Desktop Downloads Music Public test
Documents hosts Pictures Templates Videos
改用戶和組命令
[[email protected] student]# chown daemon:root /home/student/hosts 同時改擁有者和組
[[email protected] student]# ls -l
total 4
drwxr-xr-x. 2 student student 6 Jan 5 2015 Desktop
drwxr-xr-x. 2 student student 6 Jan 5 2015 Documents
drwxr-xr-x. 2 student student 6 Jan 5 2015 Downloads
-rw-r--r--. 1 daemon root 231 May 29 20:27 hosts
drwxr-xr-x. 2 student student 6 Jan 5 2015 Music
drwxr-xr-x. 2 student student 6 Jan 5 2015 Pictures
drwxr-xr-x. 2 student student 6 Jan 5 2015 Public
drwxr-xr-x. 2 student student 6 Jan 5 2015 Templates
drwxr-x---. 2 student student 19 May 29 20:30 test
drwxr-xr-x. 2 student student 6 Jan 5 2015 Videos
[[email protected] student]#
[[email protected] student]# chown bin /home/student/hosts 改擁有者
[[email protected] student]# ls -l /home/student/hosts
-rw-r--r--. 1 bin root 231 May 29 20:27 /home/student/hosts
[[email protected] student]# ls -lh /home/student/hosts
[[email protected] student]# chown :daemon /home/student/hosts 改組
[[email protected] student]# ls -lh /home/student/hosts
-rw-r--r--. 1 bin daemon 231 May 29 20:27 /home/student/hosts
[[email protected] student]# chgrp root /home/student/hosts 改組
數字表示權限
--- 000 0
--x 001 1
-w- 010 2
-wx 011 3
r-- 100 4
r-x 101 5
rw- 110 6
rwx 111 7
讀 寫 執 基礎數是 4 2 1 ,三者組合。
[[email protected] test]# chmod 755 /home/student/hosts
[[email protected] test]# ls -lh !$
ls -lh /home/student/hosts
-rwxr-xr-x. 1 bin root 231 May 29 20:27 /home/student/hosts
[[email protected] test]# chmod 600 /home/student/hosts
[[email protected] test]# ls -lh !$
ls -lh /home/student/hosts
-rw-------. 1 bin root 231 May 29 20:27 /home/student/hosts
[[email protected] test]# chmod 664 /home/student/hosts
[[email protected] test]# ls -lh /home/student/hosts
-rw-rw-r--. 1 bin root 231 May 29 20:27 /home/student/hosts
實驗: 在server上lab permissions setup
要求 ateam組的成員可以訪問/home/ateam-text目錄,並且可以在目錄下創建,刪除文件. 在目錄下,andy創建的文件可以讓alice修改
相關用戶的密碼為password
root # mkdir /home/ateam-text
root # chown :ateam /home/ateam-text
root # chmod g=rwx /home/ateam-text
andy $ touch /home/ateam-text/andy-file1
andy $ chmod o=rw /home/ateam-text/andy-file1 改這個文件的其他權限
andy $ touch /home/ateam-text/andy-file2
[[email protected] ateam-text]# chgrp student ./tom-file2 把這個改成其他的所屬組
如上 student用戶登陸可以修改 tom創建的兩個文件。
總結:如上,A用戶的目錄或文件,有兩種方式可以讓其他用戶獲取修改的權限,一種是修改o的權限,一種是修改文件所屬的組。
6.3 掌握特權位和粘貼位權限,用戶權限掩碼
[[email protected] /]$ ls -lh /usr/bin/passwd
-rwsr-xr-x. 1 root root 28K Jan 30 2014 /usr/bin/passwd
[[email protected] /]$ ls -lh /bin/touch
-rwxr-xr-x. 1 root root 61K Jan 25 2014 /bin/touch
[[email protected] /]$
如上,特權位s
[[email protected] ~]# chmod u-s /usr/bin/passwd 將其減掉後變成了w
設定特權位 chmod 4775 /usr/bin/passwd
好處:在執行命令的有效身份是文件的擁有者root,所以任何人在執行這個文件時的權限 不是任何人,而是擁有者的權限。
組位加特權位:
[[email protected] ~]# chmod g+s /usr/bin/cat 運行組身份
[[email protected] ~]# chmod u+s /usr/bin/cat 運行者身份
umask
用於設置默認權限的.
默認新建文件,最大權限為666 用它來減去umask的值,查看源文件可知,一般和管理員的默認umask是不同的。
默認新建目錄,最大權限是777 用它來減去umask的值
相關文件/etc/bashrc,/etc/profile,~/.bashrc,~/.bash_profile
[[email protected] ~]# umask
0022
[[email protected] ~]# umask
0022
[[email protected] ~]# ^C
[[email protected] ~]# mkdir 1
[[email protected] ~]# ls -l
total 12
drwxr-xr-x. 2 root root 6 May 29 22:55 1
-rw-------. 1 root root 8619 May 7 2014 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Dec 30 2014 Desktop
drwxr-xr-x. 2 root root 6 Dec 30 2014 Documents
drwxr-xr-x. 2 root root 6 Dec 30 2014 Downloads
drwxr-xr-x. 2 root root 6 Dec 30 2014 Music
drwxr-xr-x. 2 root root 6 Dec 30 2014 Pictures
drwxr-xr-x. 2 root root 6 Dec 30 2014 Public
drwxr-xr-x. 2 root root 6 Dec 30 2014 Templates
drwxr-xr-x. 2 root root 6 Dec 30 2014 Videos
[[email protected] ~]# umask 0777 改UMASK
[[email protected] ~]# mkdir 2
[[email protected] ~]# ls -l 2
total 0
[[email protected] ~]# ls -l
total 12
drwxr-xr-x. 2 root root 6 May 29 22:55 1
d---------. 2 root root 6 May 29 22:56 2
-rw-------. 1 root root 8619 May 7 2014 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Dec 30 2014 Desktop
drwxr-xr-x. 2 root root 6 Dec 30 2014 Documents
drwxr-xr-x. 2 root root 6 Dec 30 2014 Downloads
drwxr-xr-x. 2 root root 6 Dec 30 2014 Music
drwxr-xr-x. 2 root root 6 Dec 30 2014 Pictures
drwxr-xr-x. 2 root root 6 Dec 30 2014 Public
drwxr-xr-x. 2 root root 6 Dec 30 2014 Templates
drwxr-xr-x. 2 root root 6 Dec 30 2014 Videos
[[email protected] ~]# umsdk 0002
bash: umsdk: command not found...
[[email protected] ~]# umask 0002 改UMASK
[[email protected] ~]# mkdir 3
[[email protected] ~]# ls -l
total 12
drwxr-xr-x. 2 root root 6 May 29 22:55 1
d---------. 2 root root 6 May 29 22:56 2
drwxrwxr-x. 2 root root 6 May 29 22:56 3
-rw-------. 1 root root 8619 May 7 2014 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 Dec 30 2014 Desktop
drwxr-xr-x. 2 root root 6 Dec 30 2014 Documents
drwxr-xr-x. 2 root root 6 Dec 30 2014 Downloads
drwxr-xr-x. 2 root root 6 Dec 30 2014 Music
drwxr-xr-x. 2 root root 6 Dec 30 2014 Pictures
drwxr-xr-x. 2 root root 6 Dec 30 2014 Public
drwxr-xr-x. 2 root root 6 Dec 30 2014 Templates
drwxr-xr-x. 2 root root 6 Dec 30 2014 Videos
[[email protected] ~]#
用umask來幹預用戶創建的用戶目錄和文件,只對當前命令生效
普通是0002,管理員是0022,
系統會自動建立了三個用戶,分別為curly,larry,moe,這些用戶都是stooges組的成員,帳號密碼都為password
要求以上用戶和組可以在/home/stooges目錄裏訪問,創建,刪除文件. 其他用戶一律不允許訪問該目錄. 在該目錄下新建的文件會自動屬於stooges組擁有
所有普通用戶新建的文件或目錄不能被非組成員(其他人)訪問.
本文出自 “IT正能量” 博客,謝絕轉載!
RH124-06 文件系統權限