文件权限

在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
drwx----wx. root root Oct : testdir
[root@zejin240 tmp]# ll testdir/ #总共有两个文件
total
-rw-r--r--. root root Oct : tfile
-rw-r--r--. root root Oct : 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
-rw-r--r--. root root Oct : tfile1

所以能不能删除一个文件就看它所有的文件夹的权限就可以了,看下面一个例子:

[root@zejin240 tmp]# tree testdir/
testdir/
└── secdir
└── tfile directory, file [root@zejin240 tmp]# ll -d testdir/
drwx---rwx. root root Oct : testdir/
[root@zejin240 tmp]# ll -R testdir/
testdir/:
total
drwxr-xr-x. root root Oct : secdir testdir/secdir:
total
-rw-r--r--. root root Oct : 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

那么我们如何修改文件的权限: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
-rw-rw-r--. chenzejin chenzejin Oct : 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 tfile
只有用户自身对文件有读写执行权限
chmod tfile
或者
chmod u=rwx,go-rwx tfile

文件权限及chmod使用方法的更多相关文章

  1. Linux命令:修改文件权限命令chmod、chgrp、chown详解

    Linux系统中的每个文件和目录都有访问许可权限,用它来确定谁可以通过何种方式对文件和目录进行访问和操作. 文件或目录的访问权 限分为只读,只写和可执行三种.以文件为例,只读权限表示只允许读其内容,而 ...

  2. Linux 用户与组的基本操作及文件权限位的设置方法

    用户的基本操作 添加用户: useradd xxx 查看所有的用户: cat /etc/passwd 用户更改组: usermod -G groups loginname 将用户从组中删除: gpas ...

  3. linux 更改文件权限命令 chmod

    chmod -change file mode bits :更改文件权限 chmod是用来改变文件或者目录权限的命令,但只有文件的属主和超级用户(root)才有这种权限. 更改文件权限的2种方式: 一 ...

  4. 通俗易懂理解Linux文件权限修改chmod命令

    chmod g+w filename 给同组用户增加filename文件的写权限 chmod go+rw filename 给同组和组外用户增加写和读的权限 chmod g-w filename 给同 ...

  5. Jenkins_构建任务提示文件权限不足的处理方法

    问题现象 构建任务失败,查看日志提示读取文件权限不足. 问题分析 在linux上查看对应文件,发现这些文件只有root用户才有读的权限,jenkins默认是以jenkins用户在操作linux系统,因 ...

  6. Linux命令:修改文件权限命令chmod、chgrp、chown的区别

    chmod是更改文件的权限 chown是改改文件的属主与属组 chgrp只是更改文件的属组. (1)chmod是修改文件/目录的权限.可以有文字修改和数字修改. #chmod 777 /home/be ...

  7. 修改linux 文件权限命令 chmod

    [转载自:http://www.cnblogs.com/avril/archive/2010/03/23/1692809.html] Linux系统中的每个文件和目录都有访问许可权限,用它来确定谁可以 ...

  8. linux 修改文件权限指令chmod

    chmod 修改一下bin目录下的.sh权限就可以了. chmod u+x *.sh 这里的u 这里指文件所有者,+x 添加可执行权限,*.sh表示所有的sh文件.

  9. linux文件权限命令chmod学习

    Linux系统中的每个文件和目录都有访问许可权限,用它来确定谁可以通过何种方式对文件和目录进行访问和操作. 文件或目录的访问权限分为只读,只写和可执行三种.以文件为例,只读权限表示只允许读其内容,而禁 ...

随机推荐

  1. Ajax是如何运行的?

    1.我们需要知道什么是Ajax: AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). AJAX 不是新的编程语言,而是一种使用现 ...

  2. js判断地址转向

    <script type="text/javascript"> if (navigator.userAgent.search(/iphone|ipod|ipad|And ...

  3. Python黑帽编程2.2 数值类型

    Python黑帽编程2.2  数值类型 数值类型,说白了就是处理各种各样的数字,Python中的数值类型包括整型.长整型.布尔.双精度浮点.十进制浮点和复数,这些类型在很多方面与传统的C类型有很大的区 ...

  4. .NET单元测试的艺术-1.入门

    开篇:最近在看Roy Osherove的<单元测试的艺术>一书,颇有收获.因此,将其记录下来,并分为四个部分分享成文,与各位Share.本篇作为入门,介绍了单元测试的基础知识,例如:如何使 ...

  5. .Net开发笔记(十五) 基于“泵”的TCP通讯(接上篇)

    上一篇博客中说了基于“泵”的UDP通讯,附上了一个Demo,模拟飞鸽传书的功能,功能不太完善,主要是为了说明“泵”在编程中的应用.本篇文章我再附上一个关于TCP通讯的两个Demo,也都采用了“泵”模式 ...

  6. 实战JS正则表达式

    -正则表达式是一种文本模式的匹配工具. -文章导读: --1.正则对象的属性和方法 --2.字符串对象的方法 --3.使用正则表达式: ---3.1 给字符串加上千分符 ---3.2 字符串中出现次数 ...

  7. [译]Asp.net MVC 之 Contorllers(二)

    URL路由模块 取代URL重写 路由请求 URL路由模块的内部结构 应用程序路由 URL模式和路由 定义应用程序路由 处理路由 路由处理程序 处理物理文件请求 防止路由定义的URL 属性路由 书接上回 ...

  8. 《Entity Framework 6 Recipes》中文翻译系列 (30) ------ 第六章 继承与建模高级应用之多对多关联

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 第六章  继承与建模高级应用 现在,你应该对实体框架中基本的建模有了一定的了解,本章 ...

  9. C#并行编程系列-文章导航

    菜鸟初步学习,不对的地方请大神指教,参考<C#并行编程高级教程.pdf> 目录 C#并行编程-相关概念 C#并行编程-Parallel C#并行编程-Task C#并行编程-并发集合 C# ...

  10. java compiler level does not match the version of the installed java project facet 解决方案

    项目出现 java compiler level does not match the version of the installed java project facet 错误,一般是项目移植出现 ...