Linux系统中,一切皆文件。对于存在于Linux系统的文件来讲系统中的用户分别属于三种不同的角色,分别是属主、属组、其他。
属主:所有者 owner|user u
属组:属于哪个组 group g
其它用户:不是所有者,也不是组中的用户 other o
三个角色对文件拥有三种不同的权限:
读权限 read r 数值4
写权限 write w 数值2
执行权限 execute x 数值1
权限对于文件和目录的解读
目录权限
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)
示例
修改文件的属主
修改文件的属主和属组
依据参考修改文件的属主和属组,修改文件夹下所有文件的属主和属组
(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)
示例
递归修改文件夹下所有文件的属组
修改链接的属组--dereference修改链接指向的文件(硬连接会跟着一起修改),--no-dereference只修改软链接本身的属组。
(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=赋予新权限
示例
文件权限修改
目录权限修改
修改目录的其他人只有r权限
修改目录的其他人只有w权限
修改目录的其他人只有x权限
修改目录的其他人只有rx权限
修改目录的其他人只有wx权限
(4) 文件目录的特殊权限
特殊权限
SUID:主要用于修改二进制可执行文件,用户会继承此程序所有者的权限;
SGID:用于二进制可执行文件,用户会继承此程序的所有组的权限;
用于目录上,此目录中新建的文件所属组将自动从此目录继承;
STICKY:作用域目录上,此目录中的文件只能由所有者自己删除;
特殊权限 | 命令中使用的字符 | 数字 | 备注 |
SUID | s | 4 | 属主没有可执行权限,增加SUID,则属主会显示S |
SGID | s | 2 | 属组没有可执行权限,增加SGID,则属组会显示S |
STICKY | t | 1 | other没有可执行权限,增加STICKY,则属主会显示T |
①SUID权限
SUID权限在目录上设置无意义。
删除命令tail的x权限,属主显示S
②SGID权限
为目录设置SGID权限
删除属组的x权限,属组会显示S
为二进制文件设置SGID权限
为设置SGID权限之前
使用ps -g查看tail进程
为设置SGID权限之后
查看进程组可以看到tom发起的tail程序继承了magedu组
③STICKY权限
STICKY在普通文件上设置无意义。
在tmp文件夹先新建test目录并且为other增加w权限
用户tom可以删除用户magedu的文件
为tmp下的test目录设置STICKY权限
用户tom不能再删除用户magedu的文件
可以使用数值代替具体的权限来更改目录的权限,设置的权限是权限对应的数字之和。
例如
(4)新建目录或文件的默认权限
在linux 系统中,新建文件或目录,都有一个默认权限;
umask 值间接影响新建文件和新建目录的权限:
- 新建文件:666-umask,按位对应相减,如果所得结果某位存在执行(奇数)权限,则该位+1;
- 新建目录:777-umask;
ubuntu中普通用户和超级管理员的umask不一样,超级管理员是0022,普通用户是0002
rockey9.2中的超级用户和普通用户的umask是一样的
ubuntu新建文件和文件夹的权限
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
(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防止误删除
+a一般用于日志
(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一起使用
示例
给组加ACL权限
移除具体用户或组的ACL权限
移除所有ACL权限
--set替换
修改mask的acl权限
mask
- mask之影响除所有者和other的之外的人和组的最大权限
- mask需要与用户的权限进行逻辑与运算后,才能变成有限的权限(Effective Permission)
- 用户或组的设置必须存在于mask权限设定范围内才会生效
对于脚本程序来讲,必须先要有读权限,才能执行。