首页 > 系统相关 >Linux中的权限属性以及ACL相关的命令

Linux中的权限属性以及ACL相关的命令

时间:2023-11-12 15:00:47浏览次数:37  
标签:文件 -- ACL 修改 file Linux 权限 目录

Linux系统中,一切皆文件。对于存在于Linux系统的文件来讲系统中的用户分别属于三种不同的角色,分别是属主、属组、其他。

属主:所有者  owner|user   u

属组:属于哪个组 group g

其它用户:不是所有者,也不是组中的用户 other o

三个角色对文件拥有三种不同的权限:

读权限   read          r        数值4

写权限   write         w       数值2

执行权限  execute   x        数值1

Linux中的权限属性以及ACL相关的命令_删除文件

权限对于文件和目录的解读

目录权限

r           可读取目录中的文件名,的但是文件元数据无法查看

w          可在目录中删除或创建文件

x           可进入目录,可以查看文件的元数据,可查看文件内容,属于目录的最小权限

文件权限

r          可以查看文件内容

w         可以修改文件内容

x          可以执行文件,即发起可执行文件进程

用户的最终权限,是从左向右进行顺序匹配,即,所有者,所属组,其他人,一旦匹配权限立即生

效,不再向右查看其权限。r和w权限对root 用户无效,对没有读写权限的文件,root用户也可读可写

只要所有者,所属组或other三者之一有x权限,root就可以执行。


(1) 修改文件或目录的属主属组chown

       chown [OPTION]... [OWNER][:[GROUP]] FILE...(:号也可以换成.)

       chown [OPTION]... --reference=RFILE FILE...

       Change the owner and/or group of each FILE to OWNER and/or GROUP. With --reference, change the owner and group of each FILE to those of RFILE.

      OPTION

-c, --changes
          like verbose but report only when a change is made

   -f, --silent, --quiet
          suppress most error messages

   -v, --verbose
          output a diagnostic for every file processed


   --dereference
          affect the referent of each symbolic link (this is the default), rather than the symbolic link itself

   -h, --no-dereference
          affect symbolic links instead of any referenced file (useful only on systems that can change the ownership of a symlink)

   --from=CURRENT_OWNER:CURRENT_GROUP
          change the owner and/or group of each file only if its current owner and/or group match those specified here.  Either may  be  omitted,  in  which
          case a match is not required for the omitted attribute

   --no-preserve-root
          do not treat '/' specially (the default)

   --preserve-root
          fail to operate recursively on '/'

   --reference=RFILE
          use RFILE's owner and group rather than specifying OWNER:GROUP values

   -R, --recursive
          operate on files and directories recursively

   The  following options modify how a hierarchy is traversed when the -R option is also specified.  If more than one is specified, only the final one takes
   effect.

   -H     if a command line argument is a symbolic link to a directory, traverse it

   -L     traverse every symbolic link to a directory encountered

   -P     do not traverse any symbolic links (default)

示例

修改文件的属主

Linux中的权限属性以及ACL相关的命令_修改文件_02


修改文件的属主和属组

Linux中的权限属性以及ACL相关的命令_删除文件_03


      依据参考修改文件的属主和属组,修改文件夹下所有文件的属主和属组

Linux中的权限属性以及ACL相关的命令_sed_04


Linux中的权限属性以及ACL相关的命令_删除文件_05

(2) 修改文件或目录的属组信息chgrp

chgrp [OPTION]... GROUP FILE...
   chgrp [OPTION]... --reference=RFILE FILE...

DESCRIPTION

Change the group of each FILE to GROUP. With --reference, change the group of each FILE to that of RFILE.

-c, --changes
          like verbose but report only when a change is made

   -f, --silent, --quiet
          suppress most error messages

   -v, --verbose
          output a diagnostic for every file processed

   --dereference
          affect the referent of each symbolic link (this is the default), rather than the symbolic link itself

   -h, --no-dereference
          affect symbolic links instead of any referenced file (useful only on systems that can change the ownership of a symlink)

   --no-preserve-root
          do not treat '/' specially (the default)

   --preserve-root
          fail to operate recursively on '/'

   --reference=RFILE
          use RFILE's group rather than specifying a GROUP value

          use RFILE's group rather than specifying a GROUP value

   -R, --recursive
          operate on files and directories recursively

   The  following options modify how a hierarchy is traversed when the -R option is also specified.  If more than one is specified, only the final one takes
   effect.

   -H     if a command line argument is a symbolic link to a directory, traverse it

   -L     traverse every symbolic link to a directory encountered

   -P     do not traverse any symbolic links (default)

示例

递归修改文件夹下所有文件的属组

Linux中的权限属性以及ACL相关的命令_删除文件_06


修改链接的属组--dereference修改链接指向的文件(硬连接会跟着一起修改),--no-dereference只修改软链接本身的属组。

Linux中的权限属性以及ACL相关的命令_删除文件_07

(3)修改文件或目录的权限chmod

chmod [OPTION]... MODE[,MODE]... FILE...
   chmod [OPTION]... OCTAL-MODE FILE...
   chmod [OPTION]... --reference=RFILE FILE...

OPTIONS Change the mode of each FILE to MODE. With --reference, change the mode of each FILE to that of RFILE.

Change the mode of each FILE to MODE.  With --reference, change the mode of each FILE to that of RFILE.

   -c, --changes
          like verbose but report only when a change is made

   -f, --silent, --quiet
          suppress most error messages

   -v, --verbose
          output a diagnostic for every file processed

   --no-preserve-root
          do not treat '/' specially (the default)

   --preserve-root
          fail to operate recursively on '/'

   --reference=RFILE
          use RFILE's mode instead of MODE values

   -R, --recursive
          change files and directories recursively

命令中出现的字符

作用

+

增加权限

-

删除权限

=

覆盖原来的权限,赋予新的权限

修改属主的权限使用 chmod u+增加权限 u-删除权限 u=赋予新权限

修改属组的权限使用 chmod g+增加权限 g-删除权限 u=赋予新权限    

修改其他权限使用    chmod o+增加权限 o-删除权限 o=赋予新权限

示例

文件权限修改

Linux中的权限属性以及ACL相关的命令_修改文件_08


目录权限修改

修改目录的其他人只有r权限

Linux中的权限属性以及ACL相关的命令_sed_09

Linux中的权限属性以及ACL相关的命令_修改文件_10


修改目录的其他人只有w权限

Linux中的权限属性以及ACL相关的命令_修改文件_11

Linux中的权限属性以及ACL相关的命令_修改文件_12


修改目录的其他人只有x权限

Linux中的权限属性以及ACL相关的命令_修改文件_13

Linux中的权限属性以及ACL相关的命令_sed_14


修改目录的其他人只有rx权限

Linux中的权限属性以及ACL相关的命令_修改文件_15

Linux中的权限属性以及ACL相关的命令_sed_16


修改目录的其他人只有wx权限

Linux中的权限属性以及ACL相关的命令_删除文件_17

Linux中的权限属性以及ACL相关的命令_删除文件_18


(4) 文件目录的特殊权限

特殊权限

SUID:主要用于修改二进制可执行文件,用户会继承此程序所有者的权限;

SGID:用于二进制可执行文件,用户会继承此程序的所有组的权限;

          用于目录上,此目录中新建的文件所属组将自动从此目录继承;

STICKY:作用域目录上,此目录中的文件只能由所有者自己删除;

特殊权限

命令中使用的字符

数字

备注

SUID

s

4

属主没有可执行权限,增加SUID,则属主会显示S

SGID

s

2

属组没有可执行权限,增加SGID,则属组会显示S

STICKY

t

1

other没有可执行权限,增加STICKY,则属主会显示T

①SUID权限

SUID权限在目录上设置无意义。

Linux中的权限属性以及ACL相关的命令_修改文件_19

Linux中的权限属性以及ACL相关的命令_修改文件_20

删除命令tail的x权限,属主显示S

Linux中的权限属性以及ACL相关的命令_删除文件_21

Linux中的权限属性以及ACL相关的命令_删除文件_22


②SGID权限

为目录设置SGID权限

Linux中的权限属性以及ACL相关的命令_删除文件_23

删除属组的x权限,属组会显示S

Linux中的权限属性以及ACL相关的命令_修改文件_24

为二进制文件设置SGID权限

为设置SGID权限之前

Linux中的权限属性以及ACL相关的命令_删除文件_25

Linux中的权限属性以及ACL相关的命令_删除文件_26

使用ps -g查看tail进程

Linux中的权限属性以及ACL相关的命令_修改文件_27

为设置SGID权限之后

Linux中的权限属性以及ACL相关的命令_删除文件_28

Linux中的权限属性以及ACL相关的命令_删除文件_29

查看进程组可以看到tom发起的tail程序继承了magedu组

Linux中的权限属性以及ACL相关的命令_删除文件_30

③STICKY权限

STICKY在普通文件上设置无意义。

在tmp文件夹先新建test目录并且为other增加w权限

Linux中的权限属性以及ACL相关的命令_修改文件_31

用户tom可以删除用户magedu的文件

Linux中的权限属性以及ACL相关的命令_修改文件_32

为tmp下的test目录设置STICKY权限

Linux中的权限属性以及ACL相关的命令_修改文件_33

用户tom不能再删除用户magedu的文件


Linux中的权限属性以及ACL相关的命令_删除文件_34


可以使用数值代替具体的权限来更改目录的权限,设置的权限是权限对应的数字之和。

例如

Linux中的权限属性以及ACL相关的命令_删除文件_35

(4)新建目录或文件的默认权限

在linux 系统中,新建文件或目录,都有一个默认权限;

umask 值间接影响新建文件和新建目录的权限:

  • 新建文件:666-umask,按位对应相减,如果所得结果某位存在执行(奇数)权限,则该位+1;
  • 新建目录:777-umask;

    ubuntu中普通用户和超级管理员的umask不一样,超级管理员是0022,普通用户是0002

Linux中的权限属性以及ACL相关的命令_删除文件_36

rockey9.2中的超级用户和普通用户的umask是一样的

Linux中的权限属性以及ACL相关的命令_sed_37

ubuntu新建文件和文件夹的权限

Linux中的权限属性以及ACL相关的命令_sed_38


Linux中的权限属性以及ACL相关的命令_sed_39

umask: umask [-p] [-S] [mode]

Display or set file mode mask.

Options:
  -p	if MODE is omitted, output in a form that may be reused as input
  -S	makes the output symbolic; otherwise an octal number is output 

Linux中的权限属性以及ACL相关的命令_修改文件_40

Linux中的权限属性以及ACL相关的命令_删除文件_41

(4)修改文件的特殊属性

查看文件的特殊属性lsattr

lsattr [ -RVadlpv ] [ files...  ]

DESCRIPTION

lsattr lists the file attributes on a second extended file system. See chattr(1) for a description of the attributes and what they mean.

OPTIONS

   -R     Recursively list attributes of directories and their contents.
   -V     Display the program version.

   -a     List all files in directories, including files that start with `.'.

   -d     List directories like other files, rather than listing their contents.

   -l     Print the options using long names instead of single character abbreviations.

   -p     List the file's project number.

   -v     List the file's version/generation number.

设置文件的特殊属性chattr

chattr [ -RVf ] [ -v version ] [ -p project ] [ mode ] files...

mode 为 +-=[aAcCdDeFijmPsStTux]

OPTIONS

   -R     Recursively change attributes of directories and their contents.
   -V     Be verbose with chattr's output and print the program version.

   -f     Suppress most error messages.

   -v version
          Set the file's version/generation number.

   -p project
          Set the file's project number.

常用属性

a  对文件:可追加内容,不可被删除,不可被修改,不可被重命名;对目录,可新建,修改文件,但不可删除文件

A 不更新atime,节省IO

c 文件会被压缩保存

i 对文件:不可被删除不可被修改不可重命名;对目录:可修改查看目录中的文件,不可新建文件,不可删除文件

s 彻底删除文件,用0填充原来的数据块

u 防止误删除,这里是指原来存储该文件的块不会被新的数据覆盖

示例

+i防止误删除

Linux中的权限属性以及ACL相关的命令_修改文件_42

 +a一般用于日志

Linux中的权限属性以及ACL相关的命令_删除文件_43

(5)文件的ACL访问控制权限

ACL只单独的针对具体的用户设定各种不同的权限;

Centos7以后默认的xfs和ext4文件系统具有ACL功能。

Centos7之前的版本,默认手工创建的ext4的文件系统无ACL功能,需手动增加:

tune2fs –o acl /dev/sdb1

mount –o acl /dev/sdb1 /mnt/test

ACL生效顺序:

所有者,自定义用户,所属组,自定义组,其他人

命令setfacl设置ACL权限

命令getacl查看设置的ACL权限

setfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ...

#常用选项

-m|--modify=acl  修改acl权限

-M|--modify-file=file 从文件读取规则

-x|--remove=acl 删除文件acl 权限

-X|--remove-file=file 从文件读取规则

-b|--remove-all 删除文件所有acl权限

-k|--remove-default 删除默认acl规则

--set=acl 用新规则替换旧规则,会删除原有ACL项,用新的替代,一定要包含

UGO的设置,不能象 -m一样只有 ACL

--set-file=file 从文件读取新规则

--mask 重新计算mask值

-n|--no-mask 不重新计算mask值

-d|--default 在目录上设置默认acl

-R|--recursive 递归执行

-L|--logical 将acl 应用在软链接指向的目标文件上,与-R一起使用

-P|--physical 将acl 不应用在软链接指向的目标文件上,与-R一起使用


示例

Linux中的权限属性以及ACL相关的命令_sed_44


给组加ACL权限

Linux中的权限属性以及ACL相关的命令_sed_45


Linux中的权限属性以及ACL相关的命令_sed_46

移除具体用户或组的ACL权限

Linux中的权限属性以及ACL相关的命令_修改文件_47


移除所有ACL权限

Linux中的权限属性以及ACL相关的命令_修改文件_48


--set替换

Linux中的权限属性以及ACL相关的命令_sed_49

修改mask的acl权限

mask

  • mask之影响除所有者和other的之外的人和组的最大权限
  • mask需要与用户的权限进行逻辑与运算后,才能变成有限的权限(Effective Permission)
  • 用户或组的设置必须存在于mask权限设定范围内才会生效

Linux中的权限属性以及ACL相关的命令_删除文件_50

对于脚本程序来讲,必须先要有读权限,才能执行。

标签:文件,--,ACL,修改,file,Linux,权限,目录
From: https://blog.51cto.com/u_16350741/8329571

相关文章

  • Linux文件管理
    Linux的所有文件都在根目录之下。常见的目录有:/bin:存放二进制可执行文件,常用命令一般在此/home:存放用户文件的根目录,是用户主目录的点。/root:超级用户的主目录;常见的命令:cd:切换目录   ../返回上一级目录   .表示当前目录pwd:查看当前所在目录  添加-p参数......
  • 性能测试复习准备——linux环境下安装redis(7.0.5)
    参考博客:https://blog.csdn.net/qq_52227892/article/details/130649748  参考博客:https://www.cnblogs.com/756623607-zhang/p/17412640.html  使用的redis版本下载:本文中安装的版本为:http://download.redis.io/releases/redis-7.0.5.tar.gz  ===================......
  • windows11配置wsl2虚拟linux环境
    windows11配置wsl2虚拟linux环境wsl(WindowsSubsystemforLinux)是microsoft官方为windows开发的模拟Linux方法。避免了虚拟机vmware的性能损耗开销,或者双系统两者不能同时运行的问题。wsl2似乎可以满足大部分Linux需求。方便且优雅!安装wsl2安装方法官方的介绍文档:WSL的基......
  • linux的学习2
    用户管理:id:显示用户以及所属群组的实际与有效ID1.useradd-m用户名(描述:-m自动创建这个用户的主目录/home/用户名)2.useradd-g组名用户名(描述:添加新用户到某个组)3.userdel-r用户名(描述:-r删除用户的时候将他的目录也一并删掉)4.usermod-g用户组用户名(描述:修改用......
  • LINUX 图形界面无网卡,命令行DOWN
    本文只针对部分情况,网卡未加入托管导致本人遇到的问题虚拟机开启ifconfig没有ens33网卡,无法上网,同时图形化模式没有有线连接选项手动启动网卡提示:Connection'ens33'isnotavailableondeviceens33becausedeviceisstrictlyunmanaged1有一种临时方案:dhclien......
  • Linux设备树dtb文件生成问题(make dtbs)
    在makedtbs的时候遇到了无信息提示的问题,正确结果应该是如图所示且移植dtb文件时需要到dts文件夹下进行cp覆盖 ......
  • Linux命令(118)之paste
    linux命令之paste1.paste介绍linux命令paste命令是把每个文件以列对列的方式,一列列地加以合并2.paste用法paste[参数]filename...paste参数参数说明-d使用指定的分隔符进行合并-s以行来指定文件3.实例3.1.使用冒号(:)合并文件命令:paste-d:ztj-1.txtztj-2.txt[root@rhel77zt......
  • Oracle 监控客户端的连接数量趋势
    Oracle监控客户端的连接数量趋势背景前期简单总结了table方式将表信息展示出来的方法但是感觉这样非常不直观.想着能够做出一个趋势来.时序数据库的最佳的使用方式.之前的确是太靠自己的自学领悟了.发现系统的培训和学习很重要.靠自己很难,还是需要靠知识的传递想......
  • Linux命令(117)之split
    linux命令之split1.split介绍linux命令split是按照指定的大小或行数分割文件。输出文件名为“前缀aa”、“前缀ab”。默认前缀以“x”开头,默认文件大小为1000行2.split用法split[参数]filename[前缀]split参数参数说明-l指定输出文件有多少行-a指定长度的后缀,默认:2-b指定输出文......
  • linux useradd命令 添加用户
      [root@MongoDB~]#useraddwww  useradd是添加用户的命令,-s是指定用户登入后所使用的shell。默认值为/bin/bash。如果不想让用户登录系统可以用-s/sbin/nologin.此用户就不可以登录系统useraddftp-s/usr/bin/nologin -M:不要自动建立用户的home目录。-r......