1. 程式人生 > 實用技巧 >第十四章 Linux中rwx許可權管理

第十四章 Linux中rwx許可權管理

許可權管理

許可權的基本管理

什麼是許可權 許可權就是系統對我們使用者所做的操作的一種限制
為什麼要有許可權 給系統操作使用者的許可權做一個拓展和規定

許可權的分類
[root@lxy ~]# ll
-rw-r--r--. 1 root root 25 Dec 15 00:58 123.txt

有9位基礎許可權
屬主的許可權 owner 前三位
屬組的許可權 group 中間的三位
其他人的許可權 others 後三位

每個分類又分為幾個不同額許可權
可讀 readable
可寫 writable
可執行 executable

r 4
w 2
執行 x 1
許可權佔位符(沒有許可權) - 0


1.判斷該使用者是否是此檔案的屬主,如果是,就按照屬主的許可權操作
2.判斷該使用者是否是此檔案的屬組,如果是,就按照屬組的許可權操作
3.該使用者對於此檔案來說,是陌生人,就使用其他的許可權操作

案例
-rwxr-xr--. 1 root test 13 Dec 15 00:54 pass6.txt
1.root使用者對此檔案的可以操作什麼
root是這個的屬主 可讀 可寫 可執行

2.dev使用者對此檔案可以操作什麼,已知,dev屬於test組
dev使用者屬於test組,該檔案的屬組是test dev使用者擁有此檔案的屬組的許可權 可讀 可執行

3.oldboy使用者對此檔案有什麼操作許可權
oldboy使用者對於此檔案來說,是陌生人,擁有陌生人的許可權 可讀

許可權的設定

chmod      #設定許可權 
-R #遞迴設定許可權

第一種設定許可權

根據字母設定許可權
u 屬主的許可權
g 屬組的許可權
o 其它的人的許可權
a 所有人的許可權
+ 新增許可權
- 移除許可權
= 覆蓋許可權
r 讀的許可權
w 寫的許可權
x 執行的許可權
- 沒有許可權


[root@lxy ~]# touch test.txt
[root@lxy ~]#
[root@lxy ~]# ll
total 0
-rw-r--r--. 1 root root 0 Dec 17 17:21 test.txt
[root@lxy ~]# chmod u+x test.txt
[root@lxy ~]# ll
total 0
-rwxr--r--. 1 root root 0 Dec 17 17:21 test.txt
[root@lxy ~]# chmod g+w test.txt
[root@lxy ~]# ll
total 0
-rwxrw-r--. 1 root root 0 Dec 17 17:21 test.txt
[root@lxy ~]# chmod o+x test.txt
[root@lxy ~]# ll
total 0
-rwxrw-r-x. 1 root root 0 Dec 17 17:21 test.txt
[root@lxy ~]# chmod o-x test.txt
[root@lxy ~]# ll
total 0
-rwxrw-r--. 1 root root 0 Dec 17 17:21 test.txt
[root@lxy ~]# chmod a+x test.txt
[root@lxy ~]# ll
total 0
-rwxrwxr-x. 1 root root 0 Dec 17 17:21 test.txt
[root@lxy ~]# chmod -x test.txt
[root@lxy ~]# ll
total 0
-rw-rw-r--. 1 root root 0 Dec 17 17:21 test.txt
[root@lxy ~]# chmod a=r test.txt
[root@lxy ~]# ll
total 0
-r--r--r--. 1 root root 0 Dec 17 17:21 test.txt
[root@lxy ~]# chmod u=rwx test.txt
[root@lxy ~]# ll
total 0
-rwxr--r--. 1 root root 0 Dec 17 17:21 test.txt
[root@lxy ~]# chmod g=x test.txt
[root@lxy ~]# ll
total 0
-rwx--xr--. 1 root root 0 Dec 17 17:21 test.txt
[root@lxy ~]# chmod o=- test.txt
[root@lxy ~]# ll
total 0
-rwx--x---. 1 root root 0 Dec 17 17:21 test.txt


第二種

根據數字進行設定許可權
[root@lxy ~]# ll
total 0
-rwx--x---. 1 root root 0 Dec 17 17:21 test.txt
[root@lxy ~]# chmod 644 test.txt
[root@lxy ~]# ll
total 0
-rw-r--r--. 1 root root 0 Dec 17 17:21 test.txt
[root@lxy ~]# chmod 000 test.txt
[root@lxy ~]# ll
total 0
----------. 1 root root 0 Dec 17 17:21 test.txt
[root@lxy ~]# chmod 777 test.txt
[root@lxy ~]# ll
total 0
-rwxrwxrwx. 1 root root 0 Dec 17 17:21 test.txt
[root@lxy ~]# chmod 700 test.txt
[root@lxy ~]# ll
total 0
-rwx------. 1 root root 0 Dec 17 17:21 test.txt


#遞迴設定許可權

[root@lxy ~]# mkdir test
[root@lxy ~]#
[root@lxy ~]# touch test/{1..5}.txt
[root@lxy ~]#
[root@lxy ~]#
[root@lxy ~]# ll test
total 0
-rw-r--r--. 1 root root 0 Dec 17 17:42 1.txt
-rw-r--r--. 1 root root 0 Dec 17 17:42 2.txt
-rw-r--r--. 1 root root 0 Dec 17 17:42 3.txt
-rw-r--r--. 1 root root 0 Dec 17 17:42 4.txt
-rw-r--r--. 1 root root 0 Dec 17 17:42 5.txt
[root@lxy ~]# ll -d test
drwxr-xr-x. 2 root root 71 Dec 17 17:42 test
[root@lxy ~]# chmod -R 700 test
[root@lxy ~]# ll -d test
drwx------. 2 root root 71 Dec 17 17:42 test
[root@lxy ~]# ll test
total 0
-rwx------. 1 root root 0 Dec 17 17:42 1.txt
-rwx------. 1 root root 0 Dec 17 17:42 2.txt
-rwx------. 1 root root 0 Dec 17 17:42 3.txt
-rwx------. 1 root root 0 Dec 17 17:42 4.txt
-rwx------. 1 root root 0 Dec 17 17:42 5.txt
[root@lxy ~]# touch test/test.log
[root@lxy ~]# ll test
total 0
-rwx------. 1 root root 0 Dec 17 17:42 1.txt
-rwx------. 1 root root 0 Dec 17 17:42 2.txt
-rwx------. 1 root root 0 Dec 17 17:42 3.txt
-rwx------. 1 root root 0 Dec 17 17:42 4.txt
-rwx------. 1 root root 0 Dec 17 17:42 5.txt
-rw-r--r--. 1 root root 0 Dec 17 17:43 test.log

許可權的作用


許可權對檔案的影響

r 可讀,是否能夠讀取檔案內容
w 可寫,是否可以編輯修改檔案內容
x 可執行,是否可以執行這個檔案

#檔案只有r許可權時,只能檢視檔案內容,可以拷貝。其它的操作是不允許的,刪除,移動要看目錄許可權
[test@lxy /opt]$ ll
total 4
-rw-r--r--. 1 root root 9 Dec 17 18:07 test.txt
[test@lxy /opt]$ cat test.txt
hostname
[test@lxy /opt]$ head test.txt
hostname
[test@lxy /opt]$ cp test.txt /tmp/
[test@lxy /opt]$ vim test.txt
[test@lxy /opt]$ echo 123 >> test.txt
-bash: test.txt: Permission denied
[test@lxy /opt]$ ./test.txt
-bash: ./test.txt: Permission denied

#當檔案只有w許可權時,不能檢視檔案內容,vim編輯檔案時,會提示許可權不足,如果強制儲存退出,會覆蓋原始檔內容,可以追加或者重定向檔案,不能執行該檔案,也不能複製該檔案
[test@lxy /opt]$ cat test.txt
cat: test.txt: Permission denied
[test@lxy /opt]$ ./test.txt
-bash: ./test.txt: Permission denied
[test@lxy /opt]$ vim test.txt
[test@lxy /opt]$ echo hostname >> test.txt
[test@lxy /opt]$ echo hostname > test.txt
[test@lxy /opt]$ cp test.txt /mnt/
cp: cannot open ‘test.txt’ for reading: Permission denied

[root@lxy ~]# cat /opt/test.txt
pwd
[root@lxy ~]# cat /opt/test.txt
pwd
hostname
[root@lxy ~]# cat /opt/test.txt
hostname

#當檔案只有x許可權時,什麼操作都不可以
[test@lxy /opt]$ ll
total 4
-rw-r----x. 1 root root 9 Dec 17 18:13 test.txt
[test@lxy /opt]$ cat test.txt
cat: test.txt: Permission denied
[test@lxy /opt]$ vim test.txt
[test@lxy /opt]$ cp test.txt /tmp/
cp: cannot open ‘test.txt’ for reading: Permission denied
[test@lxy /opt]$ echo 123 >> test.txt
-bash: test.txt: Permission denied
[test@lxy /opt]$ echo 123 > test.txt
-bash: test.txt: Permission denied
[test@lxy /opt]$ ./test.txt
bash: ./test.txt: Permission denied

#當檔案由wx許可權時,跟單獨擁有w許可權時一樣的
[test@lxy /opt]$ ll
total 4
-rw-r---wx. 1 root root 9 Dec 17 18:13 test.txt
[test@lxy /opt]$ ./test.txt
bash: ./test.txt: Permission denied
[test@lxy /opt]$ echo pwd >> test.txt
[test@lxy /opt]$ vim test.txt
[test@lxy /opt]$ cat test.txt
cat: test.txt: Permission denied
[test@lxy /opt]$ cp test.txt /tmp/
cp: cannot open ‘test.txt’ for reading: Permission denied

#當檔案擁有rw許可權時,可以正常讀取,編輯該檔案,不能執行該檔案
[test@lxy /opt]$ ll
total 4
-rw-r--rw-. 1 root root 9 Dec 17 18:21 test.txt
[test@lxy /opt]$ cat test.txt
hostname
[test@lxy /opt]$ cp test.txt /tmp/
[test@lxy /opt]$ ./test.txt
-bash: ./test.txt: Permission denied
[test@lxy /opt]$ echo pwd >> test.txt
[test@lxy /opt]$ vim test.txt

#當檔案擁有rx許可權時,可以正常的讀取檔案,可以執行該檔案,但是不能編輯該檔案
[test@lxy /opt]$ ll
total 4
cat: ll: No such file or directory
[test@lxy /opt]$ cat test.txt
hostname
[test@lxy /opt]$ echo 123 >> test.txt
-bash: test.txt: Permission denied
[test@lxy /opt]$ echo 123 > test.txt
-bash: test.txt: Permission denied
[test@lxy /opt]$ vim test.txt
[test@lxy /opt]$ ./test.txt
lxy

總結:許可權對於檔案的影響

r許可權可以正常讀取檔案內容,可以執行檢視檔案的命令,不能修改或者執行檔案,可以拷貝
只有w許可權,不能讀取檔案,可以追加或者重定向,使用vim命令時,會提示許可權不足,強制修改儲存時,會覆蓋原始檔的內容,不可以執行該檔案,w許可權需要r許可權的配合。
只有x許可權時,什麼操作都不可以,跟w許可權配合時,等同於只有w許可權的作用,x許可權需要跟r許可權配合使用。可以正常讀取檔案,執行檔案,但是不能修改檔案。


許可權對於目錄的影響

r 表示可以顯示目錄下面的檔案列表和屬性資訊
w 表示可以建立新檔案,刪除檔案,移動檔案的操作
x 是否可以進入該目錄 cd

#目錄只有r許可權時,可以顯示目錄的檔案列表,但是會提示許可權不足,檢視目錄下的檔案屬性資訊,會提示許可權不足,屬性資訊被?所顯示,無法進入目錄,無法建立新檔案,刪除檔案,移動檔案
[test@lxy /opt]$ ll
total 12
-rw-rw--w-. 1 yuwei yuwei 10 Dec 17 18:36 123
-rw-r---w-. 1 test test 9 Dec 17 18:41 passwd
drwxr-xr--. 2 root root 86 Dec 17 18:50 test
-rw-r---w-. 1 root root 9 Dec 17 18:33 test.txt
[test@lxy /opt]$ ls test
ls: cannot access test/old1.txt: Permission denied
ls: cannot access test/old2.txt: Permission denied
ls: cannot access test/old3.txt: Permission denied
ls: cannot access test/old4.txt: Permission denied
ls: cannot access test/old5.txt: Permission denied
old1.txt old2.txt old3.txt old4.txt old5.txt
[test@lxy /opt]$ ls -l test
ls: cannot access test/old1.txt: Permission denied
ls: cannot access test/old2.txt: Permission denied
ls: cannot access test/old3.txt: Permission denied
ls: cannot access test/old4.txt: Permission denied
ls: cannot access test/old5.txt: Permission denied
total 0
-????????? ? ? ? ? ? old1.txt
-????????? ? ? ? ? ? old2.txt
-????????? ? ? ? ? ? old3.txt
-????????? ? ? ? ? ? old4.txt
-????????? ? ? ? ? ? old5.txt
[test@lxy /opt]$ cd test
-bash: cd: test: Permission denied
[test@lxy /opt]$ rm -f test/old1.txt
rm: cannot remove ‘test/old1.txt’: Permission denied
[test@lxy /opt]$ touch test/new1.txt
touch: cannot touch ‘test/new1.txt’: Permission denied
[test@lxy /opt]$ mv test/old1.txt /tmp/
mv: cannot stat ‘test/old1.txt’: Permission denied

#只有w許可權時,什麼都幹不了
[test@lxy /opt]$ ll
total 12
-rw-rw--w-. 1 yuwei yuwei 10 Dec 17 18:36 123
-rw-r---w-. 1 test test 9 Dec 17 18:41 passwd
drwxr-x-w-. 2 root root 86 Dec 17 18:50 test
-rw-r---w-. 1 root root 9 Dec 17 18:33 test.txt
[test@lxy /opt]$ ls test
ls: cannot open directory test: Permission denied
[test@lxy /opt]$ cd test
-bash: cd: test: Permission denied
[test@lxy /opt]$ touch test/new1.txt
touch: cannot touch ‘test/new1.txt’: Permission denied
[test@lxy /opt]$ rm -f test/old1.txt
rm: cannot remove ‘test/old1.txt’: Permission denied
[test@lxy /opt]$ mv test/old1.txt /tmp/
mv: cannot stat ‘test/old1.txt’: Permission denied

#只有x許可權時,只能進入目錄當中,其它的操作都不能操作
[test@lxy /opt]$ ll
total 12
-rw-rw--w-. 1 yuwei yuwei 10 Dec 17 18:36 123
-rw-r---w-. 1 test test 9 Dec 17 18:41 passwd
drwxr-x--x. 2 root root 86 Dec 17 18:50 test
-rw-r---w-. 1 root root 9 Dec 17 18:33 test.txt
[test@lxy /opt]$ ls test
ls: cannot open directory test: Permission denied
[test@lxy /opt]$ touch test/new1.txt
touch: cannot touch ‘test/new1.txt’: Permission denied
[test@lxy /opt]$ rm -f test/old1.txt
rm: cannot remove ‘test/old1.txt’: Permission denied
[test@lxy /opt]$ mv test/old1.txt /tmp/
mv: cannot move ‘test/old1.txt’ to ‘/tmp/old1.txt’: Permission denied
[test@lxy /opt]$ cd test
[test@lxy /opt/test]$ ls
ls: cannot open directory .: Permission denied


#擁有rw許可權時,跟擁有單獨的r許可權是一個作用
[test@lxy /opt]$ ll
total 12
-rw-rw--w-. 1 yuwei yuwei 10 Dec 17 18:36 123
-rw-r---w-. 1 test test 9 Dec 17 18:41 passwd
drwxr-xrw-. 2 root root 86 Dec 17 18:50 test
-rw-r---w-. 1 root root 9 Dec 17 18:33 test.txt
[test@lxy /opt]$ ls test
ls: cannot access test/old1.txt: Permission denied
ls: cannot access test/old2.txt: Permission denied
ls: cannot access test/old3.txt: Permission denied
ls: cannot access test/old4.txt: Permission denied
ls: cannot access test/old5.txt: Permission denied
old1.txt old2.txt old3.txt old4.txt old5.txt
[test@lxy /opt]$ ll test
ls: cannot access test/old1.txt: Permission denied
ls: cannot access test/old2.txt: Permission denied
ls: cannot access test/old3.txt: Permission denied
ls: cannot access test/old4.txt: Permission denied
ls: cannot access test/old5.txt: Permission denied
total 0
-????????? ? ? ? ? ? old1.txt
-????????? ? ? ? ? ? old2.txt
-????????? ? ? ? ? ? old3.txt
-????????? ? ? ? ? ? old4.txt
-????????? ? ? ? ? ? old5.txt
[test@lxy /opt]$ touch test/new.txt
touch: cannot touch ‘test/new.txt’: Permission denied
[test@lxy /opt]$ rm -f test/old1.txt
rm: cannot remove ‘test/old1.txt’: Permission denied
[test@lxy /opt]$ mv test/old1.txt /tmp/
mv: cannot stat ‘test/old1.txt’: Permission denied

#擁有rx許可權時,可以正常的顯示目錄下的列表資訊,屬性資訊,可以進入目錄,不可以刪除,建立,移動目錄下的內容
[test@lxy /opt]$ ll
total 12
-rw-rw--w-. 1 yuwei yuwei 10 Dec 17 18:36 123
-rw-r---w-. 1 test test 9 Dec 17 18:41 passwd
drwxr-xr-x. 2 root root 86 Dec 17 18:50 test
-rw-r---w-. 1 root root 9 Dec 17 18:33 test.txt
[test@lxy /opt]$ ls test
old1.txt old2.txt old3.txt old4.txt old5.txt
[test@lxy /opt]$ ll test
total 0
-rw-r--r--. 1 root root 0 Dec 17 18:50 old1.txt
-rw-r--r--. 1 root root 0 Dec 17 18:50 old2.txt
-rw-r--r--. 1 root root 0 Dec 17 18:50 old3.txt
-rw-r--r--. 1 root root 0 Dec 17 18:50 old4.txt
-rw-r--r--. 1 root root 0 Dec 17 18:50 old5.txt
[test@lxy /opt]$ touch test/new.txt
touch: cannot touch ‘test/new.txt’: Permission denied
[test@lxy /opt]$ cd test
[test@lxy /opt/test]$ ls
old1.txt old2.txt old3.txt old4.txt old5.txt
[test@lxy /opt/test]$ ll
total 0
-rw-r--r--. 1 root root 0 Dec 17 18:50 old1.txt
-rw-r--r--. 1 root root 0 Dec 17 18:50 old2.txt
-rw-r--r--. 1 root root 0 Dec 17 18:50 old3.txt
-rw-r--r--. 1 root root 0 Dec 17 18:50 old4.txt
-rw-r--r--. 1 root root 0 Dec 17 18:50 old5.txt

#擁有wx許可權時,可以進入目錄,可以建立,刪除,移動檔案,但是檢視不了檔案列表,移動到其他目錄時,是需要看目標目錄的許可權,如果沒有wx,是移動不進去的。
[test@lxy /opt]$ ll
total 12
-rw-rw--w-. 1 yuwei yuwei 10 Dec 17 18:36 123
-rw-r---w-. 1 test test 9 Dec 17 18:41 passwd
drwxr-x-wx. 2 root root 86 Dec 17 18:50 test
-rw-r---w-. 1 root root 9 Dec 17 18:33 test.txt
[test@lxy /opt]$ ls test
ls: cannot open directory test: Permission denied
[test@lxy /opt]$ ll test
ls: cannot open directory test: Permission denied
[test@lxy /opt]$ touch test/new1.txt
[test@lxy /opt]$ ls test
ls: cannot open directory test: Permission denied
[test@lxy /opt]$ rm -f test/old1.txt
[test@lxy /opt]$ mv test/old2.txt /root
mv: cannot stat ‘/root/old2.txt’: Permission denied
[test@lxy /opt]$ mv test/old2.txt /tmp
[test@lxy /opt]$ rm -f test/*

總結:許可權對目錄的影響

只有r許可權時,只能檢視目錄下的檔案列表,但是會提示許可權不足,無法檢視檔案的屬性資訊,需要x許可權的配合才能正常的顯示檔案列表資訊。
只有w許可權的時候,什麼都操作不了。rw許可權跟單獨的r許可權作用是一樣的,w許可權需要x許可權的配合,可以正常建立檔案,刪除,移動檔案,移動檔案需要看目標目錄的許可權,但是不能顯示目錄下的檔案列表資訊。
只有x許可權時,只能進入目錄,但是什麼操作不了。