除了基本的r,w,x之外,在linux传统的ext2、ext3、ext4文件系统下,还可以设置其他 的文件属性。如chattr,lsattr,而在CentOS7中默认利用xfs作为默认的文件系统,就不支持chattr的参数。

文件的特殊权限

SUID

  如果用户在运行某程序时,拥有该权限的话,就会以该程序的拥有者的身份运行该程序。

  •  chmod u+|-s FILE...

  • 位置:文件属主的执行位,有执行权限显示为s,否则S

以二进制程序passwd为例:

  1. [root@Der_Tencent ~]# info passwd  #passwd用来修改用户的密码
  2.  
  3. File: *manpages*, Node: passwd, Up: (dir)
  4.  
  5. PASSWD() User utilities PASSWD()
  6.  
  7. NAME
  8. passwd - update user's authentication tokens
  9.  
  10. SYNOPSIS
  11. passwd [-k] [-l] [-u [-f]] [-d] [-e] [-n mindays] [-x maxdays] [-w
  12. warndays] [-i inactivedays] [-S] [--stdin] [username]
  1. [root@Der_Tencent ~]# whereis passwd   #查看passwd程序的位置
  2. passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man5/passwd..gz /usr/share/man/man1/passwd..gz

[root@Der_Tencent ~]# ls /usr/bin/passwd -al   #查看passwd的详细信息
  -rwsr-xr-x. 1 root root 27832 Jun 10 2014 /usr/bin/passwd  #o,g,o都有文件的执行权

  [root@Der_Tencent ~]# ls -al /etc/shadow    #查看用户存放密码的文件
  ---------- 1 root root 1757 Sep 22 21:51 /etc/shadow    #只有root可强制修改文件

用户可以修改自己密码,但是修改的密码以密文存放在/ec/shadow中;如果用户没有/etc/shadow的写权限,又怎么能将修改的密码写入文件中呢?

仔细观察passwd程序的属性发现属主中的执行权限变成了‘ s ’,这个便是SUID权限。

当其他用户执行passwd时,便以root用户的身份执行该程序。用户便能正常修改自己密码了。可以其他程序文件验证,如cat,less等。

    • 权限在程序结束后归还;

SGID

  • chmod g+|-s FILE...
  • 位置:文件属组的执行位,有执行权限显示为s,否则S

对二进制程序文件:程序文件拥有SGID权限后,执行该程序的用户拥有程序属组的权限。  

  1. [root@Der_Tencent project]# cp /usr/bin/cat /tmp/project1    #将cat程序复制到工作目录,工作目录对用户需要有r,x,w视需要而给;
  2. [root@Der_Tencent project]# chown der:der /tmp/project1     #更改文件的属主和属组
  1. [root@Der_Tencent project]# su - der                #切换为der用户(提前创建der和bob用户,且他们不同组)
  2. [der@Der_Tencent project1]$ ls -al
  3. total
  4. drwxrwsr-x der der Sep : .
  5. drwxrwxrwt. root root Sep : ..
  6. -rwxr-xr-x der der Sep : cat            #程序的属主和属主均为der,且其他用户有执行权
  1. [der@Der_Tencent project1]$ touch test               #新建测试文件  
    [der@Der_Tencent project1]$ echo "This a test" > test      #写入测试数据
    [der@Der_Tencent project1]$ ls -al test
    -rw-rw-r-- 1 der der 12 Sep 23 15:19 test
    [der@Der_Tencent project1]$ chmod o-r test            #去除其他人对测试文件的r权限
  1. [root@Der_Tencent ~]# su - bob                   #切换用户,当前用户为root,否则需要密码
  2. [bob@Der_Tencent project1]$ ./cat test              #执行文件为当前目录下的,而不是默认的/usr/bin/cat
  3. ./cat: test: Permission denied
  1. [root@Der_Tencent project]# su - der               
  2. [der@Der_Tencent project1]$ chmod g+s cat            #在组的执行位上加上SGID权限
  3. [der@Der_Tencent project1]$ ls -l cat
  4. -rwxr-sr-x der der Sep : cat
  1. [root@Der_Tencent project]# su - bob
  2. [bob@Der_Tencent project1]$ ./cat test             #bob用户这时拥有der组的权利,可进行r操作
  3. This a test

对目录:目录拥有该权限,用户在该目录下创建的文件(目录)的属组均为该目录的属组。

  1. [root@Der_Tencent project]# chown :project /tmp/project1    #创一个工作组project****修改文件的权限只有root用户才有权利******
  2. [root@Der_Tencent project1]# su - bob
  3. [bob@Der_Tencent ~]$ touch /tmp/project1/test1          #此时在工作目录内创建的文件属组为文件的创建者
  4. [bob@Der_Tencent project1]$ ls -l test1
  5. -rw-rw-r-- bob bob Sep : test1
  1. [bob@Der_Tencent project1]$ exit
  2. [root@Der_Tencent ~]# chmod g+s /tmp/project1/        #给属组加上SGID权限
  3. [root@Der_Tencent ~]# ls -al /tmp/project1
  4. total
  5. drwxrwsrwx boob project Sep : .       #属组x权限位变为s   
  6. drwxrwxrwt. root root Sep : ..
  1. [root@Der_Tencent ~]# su - bob
  2. [bob@Der_Tencent ~]$ touch /tmp/project1/test2
  3. [bob@Der_Tencent ~]$ ls -al /tmp/project1/test2
  4. -rw-rw-r-- bob project Sep : /tmp/project1/test2  #新建的文件属组变成了project工作组

SBIT

 对拥有工作目录有wx执行权限的用户来说,拥有SBIT权限,则该用户只能在目录下创建新文件,或删除自己的文件只能删除自己的文件

  • chmod o+|-t FILE...
  • 位置:其他用户的执行位,有执行权限显示为t,否则T
  1. [der@Der_Tencent project1]$ ls -al /tmp
  2. total
  3. drwxrwxrwt. root root Sep : .
  1. [alice@Der_Tencent ~]$ rm -r /tmp/test
  2. rm: remove write-protected regular empty file ‘/tmp/test/file1.txt’? y
  3. rm: cannot remove ‘/tmp/test’: Operation not permitted
  1. [der@Der_Tencent project1]$ rm -r /tmp/test
  2. [der@Der_Tencent project1]$

chmod的数字表示法修改上述三种属性

  • SUID为4,SGID为2,SBIT为1
  1. [root@Der_Tencent project1]# ls -al test2
  2. -rw-r-xr-x bob project Sep : test2
  3. [root@Der_Tencent project1]# chmod test2
  4. [root@Der_Tencent project1]# ls -al test2
  5. -rwSr-sr-t bob project Sep : test2

文件隐藏属性:chattr

  为了增加系统的安全性,在原来九个基本属性上还增加了隐藏属性。chattr指令只能在ext2、ext3、ext4的Linux传统文件系统上完整生效,其他的文件系统可能就无法完整的支持这个指令。(摘自《鸟哥的Linux私房菜:基础学习篇》)

  chattr chattr [+-=][ai] 文件或目录名称

选项与参数:
+ :增加某一个特殊参数,其他原本存在参数则不动。
- :移除某一个特殊参数,其他原本存在参数则不动。
= :设置一定,且仅有后面接的参数

a:只能增加数据,不能删除和修改数据,只有root才能设置

i:不能增加数据,不能删除改名,设置连接,只有root才能设置

  1. [root@Der_Tencent ~]# mkdir test   #创建工作目录
  2. [root@Der_Tencent ~]# cd test
  3. [root@Der_Tencent test]# touch file1    #创建工作文件
  4. [root@Der_Tencent test]# chattr +a file1 #添加a属性
  5. [root@Der_Tencent test]# echo "Input data" > file1 #该符号会覆盖文件中的内容,而a属性中没有写权限
  6. -bash: file1: Operation not permitted
  7. [root@Der_Tencent test]# echo "Input data" >> file1 #在file1中添加数据
  8. [root@Der_Tencent test]# cat file1
  9. Input data
  1. [root@Der_Tencent test]# chattr -a file1    #去除a权限
  2. [root@Der_Tencent test]# chattr +i file1    #增加i权限
  3. [root@Der_Tencent test]# echo "Input data" >> file1 #不能添加
  4. -bash: file1: Permission denied
  5. [root@Der_Tencent test]# rm file1     #不能删除
  6. rm: remove regular file file1’? y
  7. rm: cannot remove file1’: Operation not permitted
  8. [root@Der_Tencent test]# chattr -i file1  
  9. [root@Der_Tencent test]# echo "Input data again" >> file1   #去除权限之后便能添加
  10. [root@Der_Tencent test]# cat file1
  11. Input data
  12. Input data again

文件默认权限:umask

文件权限的反向掩码,用于指定创建目录或文件时的默认值;即创建文件时减去的权限

  • umask:查看当前umask;

  • umask MASK:设置umask

文件默认权限:-rw-rw-rw-,即666;

目录默认权限:drwxrwxrwx,即777;

  1. [root@Der_Tencent test]# umask #查看当前掩码,为减去写权限,每一位对应的属性:特殊权限+属主+属组+其他用户,0表示不修改
  2.  
  3. [root@Der_Tencent test]# touch test1
  4. [root@Der_Tencent test]# ls -al test1
  5. -rw-r--r-- root root Sep : test1 #文件的默认权限减去w权限
  6.  
  7. [root@Der_Tencent test]# mkdir mydir
  8. [root@Der_Tencent test]# ls -al mydir/
  9. total
  10. drwxr-xr-x root root Sep : .  #目录的默认权限减去写权限
  11.  
  12. [root@Der_Tencent test]# umask 055      #修改掩码,减去r和x权限
  13. [root@Der_Tencent test]# touch test2
  14. [root@Der_Tencent test]# ls -al test2
  15. -rw--w--w- root root Sep : test2 #文件默认属性减去r,x(默认没有则不改变),此处勿用数字相减,否则容易出错。
  • 勿用数字相减,转换为对应的权限来计算;如上示例,文件的属性为:(-rw-rw-rw-)-(---r-xr-x)=-rw--w--w-  (注意第一个‘ - ’代表文件的类型)

文件访问控制列表(Access Control List,ACL)

提供rwx权限之外的权限,可针对单一使用者,单一文件或目录进行rwx的权限规范。——《鸟哥Linux私房菜:基础学习篇》

setfacl

设置某个文件/目录的ACL设置项目

setfacl [-bkdR] [{-m|-x} acl参数] file

-m:对文件设置acl参数

-x:取消文件的acl参数

-b:移除所有的acl

-k:移除默认的acl

-R:递归设置acl

-d:设置默认的acl,对目录进行acl权限设置,目录内的文件会继承权限

  1. [root@Der test]# ls -al file
  2. -rw-r--r-- root root Sep : file
  3. [root@Der test]# setfacl -m u:der:w file #der用户添加w权限
  4. [root@Der test]# ls -al file
  5. -rw-rw-r--+ root root Sep : file #显示+号
  6.  
  7. [root@Der test]# setfacl -m u::x file #省略用户名,默认对root用户操作******会把原来的属性覆盖*******
  8. [root@Der test]# ll file
  9. ---xrw-r--+ root root Sep : file

getfacl

获得某个目录/文件的ACL

  1. [root@Der test]# getfacl file
  2. # file: file
  3. # owner: root
  4. # group: root
  5. user::rw-    #root用户的权限
  6. user:der:-w- #der用户的权限
  7. group::r-- #默认root组的权限
  1. [root@Der test]# setfacl -m u:der:rwx file
  2. [root@Der test]# setfacl -m m:rw file #设置掩码
  3. [root@Der test]# getfacl file
  4. # file: file
  5. # owner: root
  6. # group: root
  7. user::rw-
  8. user:der:rwx #effective:rw- #仅与掩码相同的权限才拥有
    ..
    mask::rw-

Linux基础知识之文件的权限(二)的更多相关文章

  1. Linux基础知识之文件的权限(一)

    Linux基础知识之文件权限(一) Linux优点之一就是它拥有多用户多任务的环境,在提供文件共享的同时也能保证用户文件的安全性.所以,设置文件的权限管理变得尤为重要. 权限讲解 [der@Der ~ ...

  2. Linux基础知识之文件和目录的权限机制

    Linux中的用户 Linux中的用户有三类,分别是: 所有者(u) 同组用户(g) 其他人(o) 如下图所示,假设存在两个组:groupA和groupB,rachel和ross属于组groupA,m ...

  3. Linux基础知识第七讲,用户权限以及用户操作命令

    目录 Linux基础知识第七讲,用户权限以及用户操作命令 一丶简介linux用户,用户权限,组的概念. 1.1 基本概念 1.2 组 1.3 ls命令查看权限. 二丶用户权限修改命令 1.chmod ...

  4. Linux基础知识第三讲,拷贝文件跟移动文件命令

    目录 Linux基础知识第三讲,拷贝文件跟移动文件命令 一丶常用命令 1.tree命令常用选项 2.cp复制文件命令 3.mv 命令的使用 Linux基础知识第三讲,拷贝文件跟移动文件命令 一丶常用命 ...

  5. Linux基础知识入门

    [Linux基础]Linux基础知识入门及常见命令.   前言:最近刚安装了Linux系统, 所以学了一些最基本的操作, 在这里把自己总结的笔记记录在这里. 1,V8:192.168.40.10V1: ...

  6. linux基础知识的总结

    例如以下内容是我对linux基础知识的总结,由于本人在初期学习linux的时候走了不少的弯路,对于基础的掌握耗费了不少的时间,所以为了后来者对linux的基础部分有个清晰的了解,特对基础知识进行了总结 ...

  7. Linux基础知识与命令1(su passwd)

    一.Linux的基本原则 1.linux由一个个目的单一的小程序组成,我们一般需要组合小程序来完成复杂的任务 2.Linux的一切都是文件(文件类似于一棵树,包括外设,接口) 3.Linux尽量避免捕 ...

  8. (转)Linux基础知识学习

    Linux基础知识学习 原文:http://blog.csdn.net/ye_wei_yang/article/details/52777499 一.Linux的磁盘分区及目录 Linux的配置是通过 ...

  9. linux 基础知识及命令总结

    1.mkdir   创建目录 -p 创建多级目录  mkdir -p /data/test -m, --mode=模式 设置权限模式(类似chmod),而不是rwxrwxrwx 减umask -p, ...

随机推荐

  1. React Native初始化项目后执行react-native run-ios,构建失败

    今天是肿么了......一上班创建React Native项目,react-native run-ios运行就报错,运行不了...呜呜...... 一开始以为自己react-native run-io ...

  2. 增强for循环的简单总结

    整体来说:增强型for循环使用起来比较方便,代码也比较简单,如果只是操作集合中元素的而不使用索引的话,建议用此方法.对于普通for循环,如果需要使用索引进行其它操作的话,建议用这个. 详细来说:1,区 ...

  3. Git学习教程三之分支管理

    实战流程: 1:代码库克隆一份至本地 2:新分支操作 2.1  在需要的文件中创建并指向新的分支方便写代码  git checkout -b <name>                2 ...

  4. window 安装指定的node版本

    有时候不同的项目需要不同的node版本,window切换node版本命令很不管用,甚至需要卸载后重新装,同事分享了一下他的做法,很便利. 1.打开node官网 https://nodejs.org/e ...

  5. RNN汇总

    RNN(Recurrent Neural Network)循环神经网络. 对于CNN来说,比如图像处理,它逐渐从局部空间抽象到全局空间,有一种空间层次感,通道可以与空间一起卷积,也可以分开卷积.同时由 ...

  6. Spring Cloud health节点通过注册中心扫描状态的简单实现

    package com.zjs.web; import com.netflix.appinfo.InstanceInfo; import com.zjs.FallbackApiApplication; ...

  7. ESXi 制作模板并优化 Centos

    1.修改网络配置 vi /etc/sysconfig/network-scripts/ifcfg-eth0 #编辑network配置文件修改以下两项 (eth1同理) ONBOOT=yes BOOTP ...

  8. python数值列表

    使用range函数生成数值列表 使用range函数打印1~5的数字 for i in range(1,6): print(i) 输出 1 2 3 4 5 利用range函数生成数值列表 >> ...

  9. [转帖]图说Docker架构的各种信息

    图说Docker架构的各种信息 2018-07-18 15:16:04作者:linux人稿源:运维之美   https://ywnz.com/linuxyffq/2344.html 介绍Docker架 ...

  10. 老贾的幸福生活day6 整型和布尔值的转换 字符串讲解 for 循环简介

    整型和布尔值的转换: 整型: python 2 整型 int long(长整型) /获取的是整型 python 3 整型 int 获取的是浮点数(小数) 十进制转二进制: print(bin(36)) ...