文件权限

在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使用方法相关推荐

  1. 在linux中 更改文件权限的命令是,linux 更改文件权限命令 chmod

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

  2. linux系统编程笔记02——Linux命令:修改文件权限命令chmod、chgrp、chown详解

    Linux命令:修改文件权限命令chmod.chgrp.chown详解 Linux系统中的每个文件和目录都有访问许可权限,用它来确定谁可以通过何种方式对文件和目录进行访问和操作. 文件或目录的访问权限 ...

  3. linux文件权限754表示,Linux命令chmod修改文件权限 777和754方法

    在linux操作系统下,使用shell命令来操作: 常用下面这条命令: chmod 777  文件或目录 示例:chmod  777 /etc/squid 运行命令后,squid文件夹(目录)的权限就 ...

  4. Linux修改文件权限(chmod函数)

    对于Linux中的修改文件权限,我们先看一张图片: 可以看到,对于一个文件而言,操作它的用户可以分为三类: 所有者,同组用户,其他用户 u: (user)表示文件属主的访问权限; g: (group) ...

  5. Linux文件权限管理 chmod 读、写、可执行

    使用 ls -l,查看文件权限 左侧第一列 -rw-r--r-- 这些就是文件的权限信息. 文件的权限分为三种:(三种权限可相互叠加,最高权限为7) 权限 字母 对应的二进制 对应的八进制 读 r 1 ...

  6. Ubuntu学习日记--Lesson7:文件权限管理chmod

    原文链接:http://blog.chinaunix.net/uid-21880738-id-1813031.html Ubuntu的许多操作是在终端中进行的,通过sudo命令管理的文件是由root持 ...

  7. linux用命令设置读权限,linux中的文件权限和chmod命令的使用

    1.linux中的文件权限 linux中用ls -al列出文件列表时,会看到第一列有rwx这样的字样,这个表示该文件的权限设置属性.在linux中,权限划分主要分为三种身份:所有者(owner或者us ...

  8. Linux文件权限设置(chmod命令)

    用户说明 linux每一个用户都属于一个组,不能独立于组外 1.文件所有者:一般为文件的创建者 2.文件所在组:当用户创建了一个文件后,这个文件的所在组就是该用户所在的用户组 3.其他组: 除开所在组 ...

  9. Linux:修改文件权限命令chmod

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

最新文章

  1. 天猫精灵可以当电脑音响吗_天猫精灵推出便携式投影仪,小到可以装进口袋,试试效果...
  2. spring 全局异常处理
  3. android定时循环,Android AlarmManager实现定时循环后台任务
  4. 记一次 React 组件无法更新状态值的问题分析与解决
  5. opencv-api arcLength
  6. 无法在驱动器0分区上安装windows 解决方法
  7. mysql 分表分库mycat_Mysql数据库之如何Mycat分表分库?
  8. 查询软件和硬件列表清单[将文章里代码另存为 list.vbs,双击运行就会出现一个html页面]...
  9. java jar 启动脚本
  10. Windows10最常用的软件推荐V1.7
  11. 双目测量空间中两点距离
  12. 十二属相配对与最佳配偶
  13. MBR+双硬盘(固态+机械)双系统(win+ubuntu21.04)装机教程
  14. 计算机信息学院运动会入场式,厦门大学信息学院第一届新生运动会成功举办
  15. Java实习生常规技术面试题每日十题Java基础(三)
  16. Linux 网络协议栈开发—— 二层桥实现原理
  17. Vue中使用高德地图,简单明了
  18. Web网页开发之问卷调查
  19. CDR 无法安装,解决办法
  20. 计算机应用文摘官网,计算机应用文摘

热门文章

  1. 30_visdom可视化、TensorboardX及其案例、安装visdom、使用visdom的案例
  2. Sqoop(三)将关系型数据库中的数据导入到HDFS(包括hive,hbase中)
  3. 4.windows环境下如何安装memcached教程(转载+自己整理)
  4. 2.cocos2dx 3.2中语法的不同之处,lambada表达式的使用和function和bind函数的使用
  5. Microsoft公司的匈牙利法命名规则
  6. iask(http://ishare.iask.sina.com.cn/download/explain.php?fileid=12207500)
  7. 检索数据_22_根据数据项的值排序
  8. 02-NVIDIA Jetson TX2 通过JetPack 3.1刷机完整版(踩坑版)
  9. 流程图绘制技巧及实战案例
  10. 2018,抢票大作战