加入收藏 | 设为首页 | 会员中心 | 我要投稿 航空爱好网 (https://www.ikongjun.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Unix > 正文

文件权限说明

发布时间:2022-10-05 16:47:51 所属栏目:Unix 来源:
导读:  普通用户的 umask 默认是 002

  root 用户的 umask 默认是 022

  持久保存 umask 配置:

  参数含义

  -S

  以文字的方式来表示权限掩码

  [root@c7-1 ~]#umask
  0022
  普通用户的 umask 默认是 002
 
  root 用户的 umask 默认是 022
 
  持久保存 umask 配置:
 
  参数含义
 
  -S
 
  以文字的方式来表示权限掩码
 
  [root@c7-1 ~]#umask
  0022
  [root@c7-1 ~]#umask -S
  u=rwx,g=rx,o=rx
  [root@c7-1 ~]#umask -p
  umask 0022
  [root@c7-1 ~]#touch 1 && mkdir 2
  [root@c7-1 ~]#ll
  total 0
  -rw-r--r-- 1 root root 0 Aug  7 08:49 1
  drwxr-xr-x 2 root root 6 Aug  7 08:49 2
  #文件 1 的权限为 666-022 = 644
  #目录 2 的权限为 777-022 = 755
  4.文件特殊权限
 
  文件除了 r,w,x 三种权限之外还有三种特殊权限 SUID,SGID,Sticky
 
  (1)SUID
 
  前提:进程有属主和属组;文件有属主和属组
 
  二进制的可执行文件上SUID权限功能:
 
  SUID权限设定:
 
  chmod u+s FILE
  chmod 6xxx FILE
  chmod u-s FILE
  示例:
 
  [root@c7-1 ~]#ll /usr/bin/passwd
  -rwsr-xr-x. 1 root root 27856 Aug  9  2019 /usr/bin/passwd
  [root@c7-1 ~]#touch test
  [root@c7-1 ~]#ll
  total 0
  -rw-r--r-- 1 root root 0 Aug  7 14:47 test
  [root@c7-1 ~]#chmod u+s test
  [root@c7-1 ~]#ll
  total 0
  -rwSr--r-- 1 root root 0 Aug  7 14:47 test
  (2)SGID
 
  二进制的可执行文件上SGID权限功能:
 
  目录上的SGID权限功能:
 
  默认情况下,用户创建文件时UNIX 文件权限,其属组为此用户所属的主组,一旦某目录被设定了SGID,则对此目录有写权限的用户在此目录中创建的文件所属的组为此目录的属组,通常用于创建一个协作目录
 
  SGID权限设定:
 
  chmod g+s FILE/DIR
  chmod 2xxx FILE/DIR
  chmod g-s FILE/DIR
  示例:
 
  [root@c7-1 ~]#ll
  total 0
  -rwSr--r-- 1 root root 0 Aug  7 14:47 test
  [root@c7-1 ~]#chmod g+s test
  [root@c7-1 ~]#ll
  total 0
  -rwSr-Sr-- 1 root root 0 Aug  7 14:47 test
  #为 user1/2/3 创建一个协作目录,其他普通用户无法操作
  groupadd total
  mkdir /opt/total
  chgrp total /opt/total
  chmod 2770 /opt/total
  useradd -G total user1
  useradd -G total user2
  useradd -G total user3
  echo "123456" | passwd --stdin user1
  echo "123456" | passwd --stdin user2
  echo "123456" | passwd --stdin user3
  (3)Sticky
 
  具有写权限的目录通常用户可以删除该目录中的任何文件,无论该文件的权限或拥有权在目录设置 Sticky 位,只有文件的所有者或 root 可以删除该文件,sticky 设置在文件上无意义
 
  Sticky权限设定:
 
  chmod o+t DIR...
  chmod 1xxx DIR
  chmod o-t DIR...
  示例:
 
  [root@c7-1 ~]#ll
  total 0
  drwxr-xr-x 2 root root 6 Aug  7 15:00 test
  [root@c7-1 ~]#chmod o+t test/
  [root@c7-1 ~]#ll
  total 0
  drwxr-xr-t 2 root root 6 Aug  7 15:00 test
  (4)SUID,SGID,Sticky特殊权限数字表示
 
  000 0
  001 1
  010 2
  011 3
  100 4
  101 5
  110 6
  111 7
  示例:
 
  [root@c7-1 ~]#ll
  total 0
  -rw-r--r-- 1 root root 0 Aug  7 15:06 test
  [root@c7-1 ~]#chmod 7777 test
  [root@c7-1 ~]#ll
  total 0
  -rwsrwsrwt 1 root root 0 Aug  7 15:06 test
  [root@c7-1 ~]#chmod 6777 test
  [root@c7-1 ~]#ll
  total 0
  -rwsrwsrwx 1 root root 0 Aug  7 15:06 test
  [root@c7-1 ~]#chmod 4777 test
  [root@c7-1 ~]#ll
  total 0
  -rwsrwxrwx 1 root root 0 Aug  7 15:06 test
  [root@c7-1 ~]#chmod 2777 test
  [root@c7-1 ~]#ll
  total 0
  -rwxrwsrwx 1 root root 0 Aug  7 15:06 test
  5.设定文件的特殊属性
 
  设置文件的特殊属性,可以防止 root 用户误操作删除或修改文件
 
  chattr +i #不能删除,改名,更改
  chattr +a #只能追加内容,不能删除,改名
  lsattr #显示特定属性
  示例:
 
  [root@c7-1 ~]#touch test
  [root@c7-1 ~]#ll
  total 0
  -rw-r--r-- 1 root root 0 Aug  7 15:15 test
  [root@c7-1 ~]#chattr +i test
  [root@c7-1 ~]#lsattr test
  ----i----------- test
  [root@c7-1 ~]#rm -rf test
  rm: cannot remove ‘test’: Operation not permitted
  [root@c7-1 ~]#chattr -i test
  [root@c7-1 ~]#rm -rf test
  [root@c7-1 ~]#ll
  total 0
  6.ACL访问控制列表
 
  在linux中,文件与目录设置不止基础权限:r,w,x,特殊权限:suid,sgid,sticky,还有文件扩展权限ACL,ACL的全称是 Access Control List (访问控制列表) ,一个针对文件/目录的访问控制列表。它在UGO权限管理的基础上为文件系统提供一个额外的、更灵活的权限管理机制。它被设计为UNIX文件权限管理的一个补充。ACL允许你给任何的用户或用户组设置任何文件/目录的访问权限。
 
  tune2fs -o acl /dev/sdb1
  mount -o acl /dev/sdb1 /mnt/test
  ACL相关命令:
 
  setfacl #可以设置 ACL 权限
  getfacl #可查看设置的 ACL 权限
  (1)setfacl
 
  格式:
 
  setfacl [-bkndRLP] { -m|-M|-x|-X ... } file ...
  用法:
 
    -m, --modify=acl 更改文件的访问控制列表
    -M, --modify-file=file 从文件读取访问控制列表条目更改
    -x, --remove=acl 根据文件中访问控制列表移除条目
    -X, --remove-file=file 从文件读取访问控制列表条目并删除
    -b, --remove-all 删除所有扩展访问控制列表条目
    -k, --remove-default 移除默认访问控制列表
        --set=acl 设定替换当前的文件访问控制列表
        --set-file=file 从文件中读取访问控制列表条目设定
        --mask 重新计算有效权限掩码
    -n, --no-mask 不重新计算有效权限掩码
    -d, --default 应用到默认访问控制列表的操作
    -R, --recursive 递归操作子目录
    -L, --logical 依照系统逻辑,跟随符号链接
    -P, --physical 依照自然逻辑,不跟随符号链接
        --restore=file 恢复访问控制列表,和“getfacl -R”作用相反
        --test 测试模式,并不真正修改访问控制列表属性
    -v, --version           显示版本并退出
    -h, --help               显示本帮助信息
  (2)getfacl
 
  格式:
 
  getfacl [-aceEsRLPtpndvh] file ...
  用法:
 
    -a, --access            仅显示文件访问控制列表
    -d, --default           仅显示默认的访问控制列表
    -c, --omit-header       不显示注释表头
    -e, --all-effective     显示所有的有效权限
    -E, --no-effective      显示无效权限
    -s, --skip-base         跳过只有基条目(base entries)的文件
    -R, --recursive         递归显示子目录
    -L, --logical           逻辑遍历(跟随符号链接)
    -P, --physical          物理遍历(不跟随符号链接)
    -t, --tabular           使用制表符分隔的输出格式
    -n, --numeric           显示数字的用户/组标识
    -p, --absolute-names    不去除路径前的 '/' 符号
    -v, --version           显示版本并退出
    -h, --help              显示本帮助信息
  (3)范例
 
  [root@c7-1 ~]#ll
  total 0
  -rw-r--r-- 1 root root 0 Aug  7 15:28 test
  [root@c7-1 ~]#getfacl test #查看 test 文件的权限
  # file: test
  # owner: root
  # group: root
  user::rw-
  group::r--
  other::r--
  [root@c7-1 ~]#setfacl -m u:syhj:rwx ./test #设置 syhj 用户对 test 文件的权限
  [root@c7-1 ~]#getfacl test
  # file: test
  # owner: root
  # group: root
  user::rw-
  user:syhj:rwx
  group::r--
  mask::rwx
  other::r--
  [root@c7-1 ~]#ll
  total 4
  -rw-rwxr--+ 1 root root 21 Aug  7 16:27 test
  # mask 表示最大有效权限,特定用户的权限和 mask 的权限做与运算,得到的权限就是特定用户真正的权限,
  # 我们一般不更改 mask 权限,只要给予 mask 最大权限 rwx,那么任何权限和 mask 权限相与,得出的值都是权限本身
  [root@c7-1 ~]#getfacl test
  # file: test
  # owner: root
  # group: root
  user::rw-
  user:syhj:rwx
  group::r--
  mask::rwx
  other::r--
  [root@c7-1 ~]#setfacl -m mask::rw test
  [root@c7-1 ~]#getfacl test
  # file: test
  # owner: root
  # group: root
  user::rw-
  user:syhj:rwx #effective:rw- ,通过运算,syhj 用户有效权限为 rw
  group::r--
  mask::rw-
  other::r--
  #清除所有ACL权限
  setfacl -b file1
  #复制file1的acl权限给file2
  getfacl file1 | setfacl --set-file=- file2
 

(编辑:航空爱好网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章