文件權限及chmod使用方法

文件權限
在linux在,由于安全控制需要,對于不同的文件有不現的權限,限制不同用戶的操作權限,總共有rwxXst這一些權限,我們經常使用到的是rwx,對于文件和文件夾而言,他們代表著不同的含義

  • 對于文件

r:用戶可以讀取該文件,如使用命令cat
w:用戶可以編輯該文件,如使用命令sed -i,vi
x:用戶可以運行該文件,如直接./get_key.sh

  • 對于文件夾

r:用戶可以讀取該文件夾下的文件名,如使用命令ls,x位也得開啟
w:用戶可以在該文件下進行文件的刪除,增加,改變文件信息,如用命令rm,x位也得開啟
x:用戶可以進入到此目錄中,如命令cd

所以:如果文件夾只有x位,可以進得去文件,只有wx位,可以刪除文件夾下的文件,只要刪除的文件名寫對也是可以刪除的,所以對于普通用戶,文件夾一般只開能rx位

舉個例子

[root@zejin240 tmp]# ll
total 4
drwx----wx. 2 root root 4096 Oct 24 13:18 testdir
[root@zejin240 tmp]# ll testdir/ #總共有兩個文件
total 8
-rw-r--r--. 1 root root 130 Oct 24 13:05 tfile
-rw-r--r--. 1 root root 99 Oct 24 13:18 tfile1
[root@zejin240 tmp]# su chenzejin
[chenzejin@zejin240 tmp]$ cd testdir/
[chenzejin@zejin240 testdir]$ ls #由于沒有r位,所以ls命令不被允許
ls: cannot open directory .: Permission denied
[chenzejin@zejin240 testdir]$ rm tfile -f #但是寫對文件名可以正常刪除
[chenzejin@zejin240 testdir]$ cd ..
[chenzejin@zejin240 tmp]$ exit
exit
[root@zejin240 tmp]# ll testdir/ #只剩一個文件了
total 4
-rw-r--r--. 1 root root 99 Oct 24 13:18 tfile1

所以能不能刪除一個文件就看它所有的文件夾的權限就可以了,看下面一個例子:

[root@zejin240 tmp]# tree testdir/
testdir/
└── secdir
    └── tfile

1 directory, 1 file
 
[root@zejin240 tmp]# ll -d testdir/
drwx---rwx. 3 root root 4096 Oct 25 18:54 testdir/
[root@zejin240 tmp]# ll -R testdir/
testdir/:
total 4
drwxr-xr-x. 2 root root 4096 Oct 25 18:54 secdir
 
testdir/secdir:
total 0
-rw-r--r--. 1 root root 0 Oct 25 18:54 tfile

對于testdir其它用戶擁有完全權限,對于secdir其它用戶只有進入查看權限,對于tfile只有讀的權限,我們現在用其它用戶進行登陸,并嘗試刪除secdir目錄

[root@zejin240 tmp]# su chenzejin
[chenzejin@zejin240 tmp]$ rm testdir/secdir/ -r
rm: descend into write-protected directory `testdir/secdir'? y
rm: remove write-protected regular empty file `testdir/secdir/tfile'? y
rm: cannot remove `testdir/secdir/tfile': Permission denied
[chenzejin@zejin240 tmp]$ rm testdir/secdir/ -r
rm: descend into write-protected directory `testdir/secdir'? y
rm: remove write-protected regular empty file `testdir/secdir/tfile'? n
rm: remove write-protected directory `testdir/secdir'? y
rm: cannot remove `testdir/secdir': Directory not empty

發現不管如何都刪除不了secdir,按照剛剛講的,我對文件夾testdir有rwx權限,應該可以刪除secdir才對,但這里為什么刪除不了呢?

這里其實不是刪除不了文件夾secdir,而我們沒有權限刪除tfile,因為對于tfile而言,要刪除它的話我們需要擁有對secdir的wx權限,而對于secdir我們只有r權限,并不具有x權限,所以我們這里刪除不了tfile,而tfile又在secdir里面,所以我們也就刪除不了secdir了。

所以如果沒有tfile,我們的普通用戶是可以刪除文件夾secdir的

[chenzejin@zejin240 tmp]$ exit
exit
[root@zejin240 tmp]# rm testdir/secdir/tfile -f
[root@zejin240 tmp]# su chenzejin
[chenzejin@zejin240 tmp]$ rm testdir/secdir/ -r
rm: remove write-protected directory `testdir/secdir'? y
[chenzejin@zejin240 tmp]$ ll testdir/
total 0

那么我們如何修改文件的權限:chmod命令

命令作用

修改文件或目錄的權限屬性

常用用法

chmod [option] MODE file
chmod [option] octalnum file

常用參數

-R:遞歸修改目錄及子目錄的所有文件

MODE

符合MODE的正則表達示為:[ugoa]*([-+=]([rwxXst]*|[ugo]))+
        u:表示文件擁有者,即user
        g:組擁有者,即group
        o:其它用戶擁有者,即other
        a:所有用戶,即相當于ugo
         :省略不寫代表a,即所有用戶

        -:去除相應權限位
        +:加上相應權限位
        =:設置相應權限位

常用使用范例

[chenzejin@zejin240 testdir]$ ll
total 4
-rw-rw-r--. 1 chenzejin chenzejin 17 Oct 25 19:17 tfile
1. 其它用戶可以編輯tfile文件內容
    [chenzejin@zejin240 testdir]$ chmod o+w tfile 
2. 其它用戶可以執行tfile文件
    [chenzejin@zejin240 testdir]$ chmod o+x tfile
3. 取消其它用戶對tfile的寫,執行權限
    [chenzejin@zejin240 testdir]$ chmod o-wx tfile
4. 為所有用戶只有只讀tfile的權限
    chmod =r tfile
    或者
    chmod a-wx,a+r tfile
    或者
    chmod 444 tfile
5. 只有用戶自身對文件有讀寫執行權限
   chmod 700 tfile
   或者
   chmod u=rwx,go-rwx tfile
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容