文件权限及chmod使用方法

摘要:
文件夹通常只启用rx位,例如[root@zejin240tmp]#lltotal4drwx---wx.2根4096年10月2413:18测试目录[root@zejin240tmp]#lltestdir/#总共有两个文件,total8-rw-r-r--。1根2013年10月30日:

文件权限

在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

  其它用户可以编辑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

免责声明:文章转载自《文件权限及chmod使用方法》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇可变参数__VA_ARGS__ 、 va_start、va_arg、valist 简单使用Ubuntu 10.04 下安装codeblock下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Jmeter之计数器

如果需要引用的数据量较大,且要求不能重复或者需要自增,那么可以使用计数器来实现。 计数器(counter):允许用户创建一个在线程组之内都可以被引用的计数器。 计数器允许用户配置一个起点,一个最大值,增量数,循环到最大值,然后重新开始,继续这样,直到测试结束。计数器使用long存储的值,所取的范围是2^63——2^63-1. 【注意】ThreadGroup...

linux笔记1(创建用户、安装gcc、安装五笔输入法)

linux笔记1(创建用户、安装gcc、安装五笔输入法) linux简说 内核:在计算机启动时载入基本内存,管理一些基本的输入输出,管理一些进程的初始化以及进程之间的调 试。控制硬件的运行。   Shell:系统的命令解释器,用户进程与kernel的桥梁作用 终端模拟器(Terminal Emulator):交互操作系统,并得到提示与反馈。   x...

Maven系列(二)之安装和配置详解

检查JDK环境 在安装Maven之前,首先要确认你已经正确安装了JDK。Maven可以运行在JDK 1.4及以上的版本上。 打开cmd输入: java -version 下载Maven Maven官网:http://maven.apache.org/download.html 这里下载此时此刻的最新版本Maven 3.3.9版本 解压下载好的apach...

恢复grub启动

 重装windows7之后修复grub方法。     装了双系统之后(ubuntu linux和windows7),再继续装windows7,就会导致grub被覆盖,导致无法进入linux,这时候修复一下grub即可。下面是最简单的一个修复方法。     1、下载grub4dos(设置参见网上,需要在c盘根目录放置三个文件,grldr,grldr.mbr,...

CENTOS7静默安装ORACLE11G及数据泵迁移

2021年2月4日江苏淮安特钢 CENTOS7静默安装ORACLE11G及数据泵迁移 作者:查小广(北京红河谷时代信息技术有限公司) 检化验系统LIMS 数据库迁移 Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production lims数据库oracle:192.168.20....

iOS开发-应用崩溃日志揭秘(一)

作为一名应用开发者,你是否有过如下经历? 为确保你的应用正确无误,在将其提交到应用商店之前,你必定进行了大量的测试工作。它在你的设备上也运行得很好,但是,上了应用商店后,还是有用户抱怨会闪退 ! 如果你跟我一样是个完美主义者,你肯定想将应用做到尽善尽美。于是你打开代码准备修复闪退的问题……但是,从何处着手呢?这时iOS崩溃日志派上用场了。在大多数情况下...