文件权限及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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,298评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,701评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,078评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,687评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,018评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,410评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,729评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,412评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,124评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,379评论 2 242
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,903评论 1 257
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,268评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,894评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,014评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,770评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,435评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,312评论 2 260

推荐阅读更多精彩内容