Linux之文件与目录权限差别

Linux是一个多用户多任务系统。而为了让各个使用者具有较保密的文件数据,因此文件的权限管理就变的很重要了。Linux一般将文件可存取的身份分为三个类别,分别是owner(文件拥有者)/group(群组用户)/others(其他用户),且三种身份各有read/write/execute等权限。

[hml@VM_0_10_centos test]$ ls -l
总用量 8
drwxr-xr-x 2 root root 4096 4月  12 14:37 dir
-rw-r--r-- 1 root root    5 4月  12 14:47 file

以dir目录为例 drwxr-xr-x 2 root root 4096 4月 12 14:37 dir

  • 第一列(drwxr-xr-x):第一位为文件类型。当为[d]则是目录,当为[-]则是文件;若是[l]则表示为链接文件(linkfile);若是[b]则表示为设备文件里面的可供储存的周边设备(可随机存取设备);若是[c]则表示为设备文件里面的序列埠设备,例如键盘、鼠标(一次性读取设备)。
    其余每三位为一组(顺序固定就是以rwx为顺序,如果某位有该权限就显示对应的字母,没有则显示-),第一组为owner所属权限即rwx;第二组为group所属权限即r-x;第三组为other所属权限即r-x。
  • 第二列 (2)为该文件或目录的连接数
  • 第三列(root)为该文件或目录的拥有者
  • 第四列(root)为该文件或目录的所属群组
文件权限之rwx

文件的权限都是针对于文件的内容而言:

  • r(read):可读取此一文件的实际内容,如读取文本文件的文字内容等;
  • w(write):可以编辑、新增或者是修改该文件的内容(但不含删除该文件);
  • x(execute):该文件具有可以被系统执行的权限。
目录权限之rwx

目录的主要内容相当于记录文件名的清单。目录就相当于抽屉,而文件就相当于抽屉里的文件夹。

  • r(read contents in directory) : 表示具有读取该目录下的文件名数据。
  • w(modify contents of directory): 表示具有改动该目录下文件名的权限。即:
    • 创建新的文件与目录;
    • 删除已经存在的文件与目录(不论该文件的权限为何!)
    • 将已存在的文件或目录进行更名;
    • 搬移该目录内的文件、目录位置。总之,目录的w权限就与该目录下面的文件名异动有关就对了
  • x (access directory):表示能否进入该目录作为工作目录。
[hml@VM_0_10_centos test]$ ls -l
total 4
drwxr-xr-x 2 root root 4096 Apr 12 14:19 dir 
-rw-r--r-- 1 root root    0 Apr 11 16:00 file
[hml@VM_0_10_centos test]$ echo "haha" >> file
-bash: file: 权限不够      //file文件的other用户只具有r权限,故只能查看文件内容,而无法修改其内容
[hml@VM_0_10_centos test]$ rm file
rm: remove write-protected regular empty file ‘file’? y
rm: cannot remove ‘file’: Permission denied  //test文件夹的other只具有rx权限,故无法删除修改test目录下的文件
[hml@VM_0_10_centos test]$ cd dir/        //dir目录的other用户具有x权限,故可以进入该目录
[hml@VM_0_10_centos dir]$ touch file   
touch: cannot touch ‘file’: Permission denied   //dir目录的other用户没有具有w权限,因此不可修改该目录下的文件名结构,即新增删除等。

[hml@VM_0_10_centos test]$ sudo chmod o=r dir  //将dir文件夹的other用户权限改为r
[hml@VM_0_10_centos test]$ ls -l
total 4
drwxr-xr-- 2 root root 4096 Apr 12 14:37 dir
-rw-r--r-- 1 root root    0 Apr 11 16:00 file
cd dir/                       
bash: cd: dir/: Permission denied    //因为dir文件夹other用户不具有x权限故无法进入该目录
[hml@VM_0_10_centos test]$ ls -l dir
ls: cannot access dir/file2: Permission denied
total 0
-????????? ? ? ? ?            ? file2     //  //因为dir文件夹other用户只具有r权限而不具有x权限,只能读取该文件夹下的文件名而无法获取详细的文件信息
[hml@VM_0_10_centos test]$ ls dir
ls: cannot access dir/file2: Permission denied
file2

[hml@VM_0_10_centos test]$ sudo chown hml dir   //将目录的所属者改为hml用户
[hml@VM_0_10_centos test]$ sudo chown hml file  //将文件的所属者改为hml用户
[hml@VM_0_10_centos test]$ ls -l
total 4
drwxr-xr-x 2 hml root 4096 Apr 12 14:19 dir
-rw-r--r-- 1 hml root    0 Apr 11 16:00 file
[hml@VM_0_10_centos test]$ echo "haha" >> file     //file文件所属者具有w权限,故可修改其内容
[hml@VM_0_10_centos test]$ cat file
haha
[hml@VM_0_10_centos test]$ touch dir/file   //dir目录具有w权限,故可以新增删除等操作
[hml@VM_0_10_centos test]$ ls dir
file  file2
[hml@VM_0_10_centos test]$ rm dir/file 
[hml@VM_0_10_centos test]$ ls dir
file2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值