Linux系统权限
第1章 Linux系統(tǒng)權限
1.1 簡介
Linux中的文件或目錄的權限和用戶及用戶組關聯(lián)很大,Linux中每個文件或目錄都有一組共9個基礎權限位,每三個字符被分為一組,他們分別是屬主權限位(占三個字符)、屬組權限位(占三個字符)、其他用戶權限位(占三個字符)。
比如rwxr-xr-x linux中正是這9個權限位來控制文件屬主(User)、屬組(Group)、其他用戶(Other)基礎權限。
1.2 三種角色
用戶對資源來說, 有三種角色
User(u): 屬主用戶(文件所有者)
Group(g): 屬組用戶(包含組成員)
Other(o): 匿名用戶(其他人)
1.3 環(huán)境
[root@oldboyedu ~]# ll -d dir drwxr-xr-x. 2 root root 18 8月 16 17:11 dir [root@oldboyedu ~]# ll -d dir/file -rw-r--r--. 1 root root 0 8月 16 17:11 dir/file1.3.1 權限描述
/root/dir的權限是所屬用戶root讀寫執(zhí)行,所屬組root讀執(zhí)行,其他用戶讀執(zhí)行
/root/dir/file的權限是所屬用戶root讀寫,所屬組root讀,其他用戶讀
1.4 修改文件所屬命令 chown
修改文件所屬
chown [user][.|:][group] [-R] filename
//修改屬主
[root@oldboyedu ~]# chown oldboy /root/dir
[root@oldboyedu ~]# ll -d /root/dir
//修改屬組
[```
root@oldboyedu ~]# chown .dba /root/dir
[root@oldboyedu ~]# ll -d /root/dir
d-wx--x--x. 2 oldboy dba 29 8月 16 17:24 /root/dir
1.5 修改權限命令chmod
第一種方式: chmod [ugoa] [+-=] [rwx] [-R] filename // a是全部
[root@oldboyedu ~]# chmod u=rx /root/dir
[root@oldboyedu ~]# ll -d dir
dr-xr-xr-x. 2 root root 18 8月 16 17:11 dir
[root@oldboyedu ~]# chmod u+w /root/dir
[root@oldboyedu ~]# ll -d dir
drwxr-xr-x. 2 root root 18 8月 16 17:11 dir
[root@oldboyedu ~]# chmod u-w /root/dir
[root@oldboyedu ~]# ll -d dir
dr-xr-xr-x. 2 root root 18 8月 16 17:11 dir
[root@oldboyedu ~]# chmod a=rwx /root/dir
[root@oldboyedu ~]# ll -d dir
drwxrwxrwx. 2 root root 18 8月 16 17:11 dir
[root@oldboyedu ~]# chmod a= /root/dir
[root@oldboyedu ~]# ll -d dir
d---------. 2 root root 18 8月 16 17:11 dir
[root@oldboyedu ~]# chmod =rx /root/dir
[root@oldboyedu ~]# ll -d dir
dr-xr-xr-x. 2 root root 29 8月 16 17:24 dir
第二種方式: chmod nnn [-R] filename //nnn 表示U G O
所屬用戶rw,屬組用戶只讀,其他用戶沒權限
4+2=6 4 0
[root@oldboyedu ~]# chmod 640 /root/dir/file
[root@oldboyedu ~]# ll /root/dir/file
-rw-r-----. 1 root root 0 8月 16 17:11 /root/dir/file
1.6 遞歸修改目錄權限(修改目錄及子目錄權限)
chmod -R 權限 filename [root@oldboyedu ~]# ll -d /root/dir drwxr-xr--. 2 root root 29 8月 16 17:24 /root/dir [root@oldboyedu ~]# ll -d /root/dir/file -rw-r-----. 1 root root 0 8月 16 17:11 /root/dir/file [root@oldboyedu ~]# chmod -R 755 /root/dir [root@oldboyedu ~]# ll -d /root/dir/file -rwxr-xr-x. 1 root root 0 8月 16 17:11 /root/dir/file第2章 文件權限實驗案例
2.1 默認文件其他用戶僅有讀權限
[root@oldboy ~]# echo "date" >/tmp/date.txt [root@oldboy ~]# ll /tmp/date.txt -rw-r--r--. 1 root root 5 Aug 16 06:37 /tmp/date.txt2.2 測試讀權限(無法執(zhí)行或刪除)
[root@oldboy ~]# su - oldboy [oldboy@oldboy ~]$ cat /tmp/date.txt date [oldboy@oldboy ~]$ echo "test" >/tmp/date.txt -bash: /tmp/date.txt: Permission denied [oldboy@oldboy ~]$ /tmp/date.txt -bash: /tmp/date.txt: Permission denied//增加x執(zhí)行權限
root@oldboy ~]# chmod o+x /tmp/date.txt
[root@oldboy ~]# ll /tmp/date.txt
-rw-r--r-x. 1 root root 5 Aug 16 06:37 /tmp/date.txt
[oldboy@oldboy ~]$ /tmp/date.txt
Thu Aug 16 06:40:56 CST 2018
[root@oldboy ~]# chmod o+w /tmp/date.txt
[root@oldboy ~]# ll /tmp/date.txt
-rw-r--rwx 1 root root 5 Aug 16 06:38 /tmp/date.txt
//測試寫權限
[oldboy@oldboy ~]$ echo "test" >/tmp/date.txt
或者使用vim編輯文件來測試
第3章 rwx對文件的影響
3.1 讀取權限(r)
文件只有r權限: 具有讀取\閱讀文件內(nèi)容權限
1.能使用查看類命令 cat、head、tail、less、more
2.不能復制、不能移動、不能編輯,不能刪除
3.2 寫入權限(w)
如果文件只有w權限: 具有新增、修改文件內(nèi)容的權限
1.使用vim編輯,會提示權限拒絕, 但可強制保存,會覆蓋之前文件內(nèi)容
2.使用echo命令重定向或追加重定向技術可以往文件內(nèi)寫入數(shù)據(jù)
3.使用cat命令讀取文件, 將讀取到的文件輸出交給僅有w權限文件的輸入
4.不能復制、不能移動、不能刪除,(刪除需要看上級目錄w的權限)
3.3 執(zhí)行權限(x)
文件只有x權限,具有執(zhí)行文件的權限。
//注意: 普通用戶需要有r權限,管理員不需要
1.不能執(zhí)行、查看、編輯、復制、移動、刪除
第4章 目錄權限實驗案例
4.1 實戰(zhàn)案例1: 對目錄沒有 w,對文件有 rwx
[root@oldboy ~]# mkdir /test [root@oldboy ~]# echo "test" > /test/test.txt [root@oldboy ~]# chmod 777 /test/test.txt [root@oldboy ~]# ll -d /test drwxr-xr-x. 2 root root 22 Aug 16 06:52 /test [root@oldboy ~]# ll /test/test.txt -rwxrwxrwx. 1 root root 5 Aug 16 06:52 /test/test.txt普通用戶驗證權限
[oldboy@oldboy ~]$ cat /test/test.txt test [oldboy@oldboy ~]$ rm -f /test/test.txt rm: cannot remove ‘/test/test.txt’: Permission denied4.2 實戰(zhàn)案例2: 對目錄有 w,對文件沒有任何權限
[root@oldboy ~]# chmod 777 /test/ [root@oldboy ~]# chmod 000 /test/test.txt [root@oldboy ~]# ll -d /test drwxrwxrwx. 2 root root 22 Aug 16 06:52 /test [root@oldboy ~]# ll -d /test/test.txt ----------. 1 root root 5 Aug 16 06:52 /test/test.txt//普通用戶驗證權限
[oldboy@oldboy ~]$ cat /test/test.txt
cat: /test/test.txt: Permission denied
[oldboy@oldboy ~]$ rm -f /test/test.txt
[oldboy@oldboy ~]$ touch /test/test1.txt
4.3 實戰(zhàn)案例3: 對目錄沒有 x,對文件有任何權限
[root@oldboy ~]# chmod 766 /test/ [root@oldboy ~]# chmod 777 /test/test.txt [root@oldboy ~]# ll -d /test/ drwxrw-rw-. 2 root root 22 Aug 16 06:58 /test/ [root@oldboy ~]# ll /test/test.txt -rwxrwxrwx. 1 root root 5 Aug 16 06:58 /test/test.txt//普通用戶驗證權限
[oldboy@oldboy ~]$ cd /test
-bash: cd: /test: Permission denied
[oldboy@oldboy ~]$ cat /test/test.txt
cat: /test/test.txt: Permission denied
[oldboy@oldboy ~]$ rm -f /test/test.txt
rm: cannot remove ‘/test/test.txt’: Permission denied
第5章 rwx對目錄的影響
5.1 目錄只有r權限: 具有瀏覽目錄及子目錄權限
1.能使用ls命令瀏覽目錄及子目錄, 同時會提示權限拒絕
2.能使用ls -l命令瀏覽目錄及子目錄, 會帶問號,同時只能看到文件名
總結: 目錄只有r權限,僅僅只能瀏覽內(nèi)的文件名,無其他操作權限
5.2 寫入權限(w)
如果目錄只有w權限:具有增加、刪除或修改目錄內(nèi)文件名權限(需要x配合)
//注意:如果目錄有w權限, 可以在目錄創(chuàng)建文件, 可以刪除目錄中的文件(跟文件權限無關)
不能進入目錄、不能復制目錄、不能刪除目錄、不能移動目錄
5.3 執(zhí)行權限(x)
目錄只有x權限
1.只能進入目錄
2.不能瀏覽、復制、移動、刪除
5.4 權限小結:
文件rw權限, 可以查看和編輯文件內(nèi)容
文件rx權限, 只能查看和執(zhí)行文件、不能編輯、復制、移動、刪除
目錄rx權限, 允許瀏覽目錄內(nèi)文件以及子目錄、并允許在目錄內(nèi)新建文件, 不允許創(chuàng)建、刪除文件和目錄
> 注意事項:
文件, x權限小心給予,建議賦予r或rw即可
目錄, w權限小心給予,建議無特殊需求賦予rx即可
轉載于:https://blog.51cto.com/13904281/2173705
總結
- 上一篇: 发文越多,影响力会越大吗?
- 下一篇: Zookeeper系列五:Master选