日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

文件权限及chmod使用方法

發布時間:2024/9/20 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 文件权限及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 └── tfile1 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 secdirtestdir/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其它用戶可以編輯tfile文件內容[chenzejin@zejin240 testdir]$ chmod o+w tfile其它用戶可以執行tfile文件[chenzejin@zejin240 testdir]$ chmod o+x tfile取消其它用戶對tfile的寫,執行權限[chenzejin@zejin240 testdir]$ chmod o-wx tfile為所有用戶只有只讀tfile的權限chmod =r tfile或者chmod a-wx,a+r tfile或者chmod 444 tfile只有用戶自身對文件有讀寫執行權限chmod 700 tfile或者chmod u=rwx,go-rwx tfile

來源:http://www.cnblogs.com/zejin2008/p/5999765.html

總結

以上是生活随笔為你收集整理的文件权限及chmod使用方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。