一, 属主, 属组
在 Linux 文件系统中, 用户如果要对文件进行操作, 首先要对文件的权限进行检查, 先判断用户是否是此文件的属主如果是则执行属主权限, 如果不是那就查看该用户是否是该文件属组内的用户, 如果是则执行属组权限, 如果不是执行 other 权限.
二, 文件和目录的读写执行
1. 文件的读, 写, 执行
文件能否读写取决于用户对文件是否有读写执行权限.
用户有对文件读权限则可以访问此文件, 查看文件内的内容, 但无法对内容进行修改
示例:
- [root@centos7 data]# echo hello world> a
- [root@centos7 data]# cat a
- hello world
- [root@centos7 data]# chmod 004 a
- [root@centos7 data]# ll
- total 4
- -------r-- 1 root root 12 Mar 14 23:20 a
- [root@centos7 data]# su masuri
- [masuri@centos7 data]$ ll
- total 4
- -------r-- 1 root root 12 Mar 14 23:20 a
- [masuri@centos7 data]$ cat a
- hello world
- [masuri@centos7 data]$ echo hello Word>a
- bash: a: Permission denied
用户有对文件写权限则可以对此文件的内容进行修改, 如果用户只有写权限, 则无法查看内部容, 但可以修改其内部的内容.
示例:
- [root@centos7 data]# chmod 002 a
- [root@centos7 data]# cat a
- hello world
- [root@centos7 data]# ls a
- a
- [root@centos7 data]# ll a
- --------w- 1 root root 12 Mar 14 23:20 a
- [root@centos7 data]# su masuri
- [masuri@centos7 data]$ cat a
- cat: a: Permission denied
- [masuri@centos7 data]$ echo abc>> a
- [masuri@centos7 data]$ exit
- exit
- [root@centos7 data]# cat a
- hello world
- abc
注意: 用户有对文件的执行权限则可以将文件进行执行, 此类操作非常危险. 一般文件不建议有执行权限.
2. 目录的读写执行
目录的读权限:
目录有读权限则表示用户可以查看目录, 可以复制目录, 但无法对目录内的文件进行操作, 若要对目录内的文件进行操作则必须要有执行权限
示例 1: 只有读权限没有执行权限时
- [root@centos7 data]# mkdir -m 004 test
- [root@centos7 data]# echo hello world> test/a
- [root@centos7 data]# su masuri
- [masuri@centos7 data]$ ls test
ls: cannot access test/a: Permission denied 没有执行权限无法查看文件
- a
- [masuri@centos7 data]$ cp -R test test2
cp: cannot stat 'test/a': Permission denied 没有执行权限无法复制目录下的内容只能复制目录本身
- [masuri@centos7 data]$ ll
- total 0
- d------r-- 2 root root 15 Mar 14 23:37 test
- d------r-- 2 masuri masuri 6 Mar 14 23:49 test2
- [masuri@centos7 data]$ rm test/a
rm: cannot remove 'test/a': Permission denied 没有写和执行无法删除文件
示例 2: 有读和执行权限
- [root@centos7 data]# chmod 005 test
- [root@centos7 data]# ll test -d
- d------r-x 2 root root 15 Mar 14 23:37 test
- [root@centos7 data]# su masuri
[masuri@centos7 data]$ cp -R test test3 可复制
- [masuri@centos7 data]$ ls
- test test2 test3
- [masuri@centos7 data]$ ls test
- a
- [masuri@centos7 data]$ cat test/a
- hello world
- [masuri@centos7 data]$ rm test/a
- rm: remove write-protected regular file 'test/a'? y
rm: cannot remove 'test/a': Permission denied 无法删除
目录的写权限:
当目录只有写权限时, 无法对目录下的文件执行任何操作. 若要能实现对目录下的文件进行操作, 必须要有执行权限
示例:
1. 只有写权限
- [root@centos7 data]# mkdir -m 002 test
- [root@centos7 data]# echo helloworld> test/a
- [root@centos7 data]# ll
- total 0
- d-------w- 2 root root 15 Mar 15 00:10 test
- [root@centos7 data]# su masuri
- [masuri@centos7 data]$ echo hello> test/a
- bash: test/a: Permission denied
- [masuri@centos7 data]$ ls test
- ls: cannot open directory test: Permission denied
- [masuri@centos7 data]$ ls test/a
- ls: cannot access test/a: Permission denied
2. 有写和执行时
- [root@centos7 data]# chmod 003 test
- [root@centos7 data]# ll -d test
- d-------wx 2 root root 15 Mar 15 00:10 test
- [root@centos7 data]# ls test
- a
- [root@centos7 data]# su masuri
- [masuri@centos7 data]$ ls test
ls: cannot open directory test: Permission denied 没有读权限无法查看
[masuri@centos7 data]$ echo hello> test/b 好像可以写入?
[masuri@centos7 data]$ rm test/a 要想也可以删除?
rm: remove write-protected regular file 'test/a'? y
[masuri@centos7 data]$ exit 转回 root 看结果.
- exit
- [root@centos7 data]# ls
- test
- [root@centos7 data]# ls test\ 原来的按文件被删除
- b
- [root@centos7 data]# cat b
- cat: b: No such file or directory
- [root@centos7 data]# cat test/b b 文件内为刚才写入的数据
- hello
总结: 目录的执行权限非常重要, 没有执行权, 即使有目录的读写权限也无法对目录下的文件进行操作.
3.chmod 中大写 X 的意义
当对目录递归施加大 X 时, 其下的所有目录自动添加执行权限, 但文件不会添加
但若文件原本有执行权限时, 则会为其添加执行权限
示例:
- [root@centos7 data]# mkdir testdir
- [root@centos7 data]# cd testdir
- [root@centos7 testdir]# touch a b 创建 a b 两个文件
- [root@centos7 testdir]# chmod 100 a 修改权限 a 为可执行
- [root@centos7 testdir]# chmod 000 b 修改权限 b 为什么都没有
- [root@centos7 testdir]# mkdir -m 000 dir 创建一个 dir 目录并设置权限为 000
- [root@centos7 testdir]# ll
- total 0
- ---x------ 1 root root 0 Mar 15 00:48 a
- ---------- 1 root root 0 Mar 15 00:48 b
- d--------- 2 root root 6 Mar 15 00:48 dir
- [root@centos7 testdir]# cd ..
- [root@centos7 data]# chmod -R a+X testdir 对 testdir 目录递归设置大 X 权限
- [root@centos7 data]# cd testdir/
- [root@centos7 testdir]# ll
- total 0
---x--x--x 1 root root 0 Mar 15 00:48 a 对比上面当文件有执行权限时全部都加了执行权限
---------- 1 root root 0 Mar 15 00:48 b 当文件没有执行权限时则不添加
d--x--x--x 2 root root 6 Mar 15 00:48 dir
三, 特殊权限位
SUID:
作用在二进制程序上, 当用户执行该程序时, 运行该程序的身份为该程序的属主而非用户自身
示例:
[root@centos7 data]# su masuri
[masuri@centos7 data]$ ll `which cat` 此时 suid 没有修改
-rwxr-xr-x. 1 root root 54160 Oct 31 03:16 /usr/bin/cat
[masuri@centos7 data]$ cat /etc/shadow shadow 文件无法访问
- cat: /etc/shadow: Permission denied
- [masuri@centos7 data]$ exit
- exit
- [root@centos7 data]# chmod u+s `which cat` 修改 suid
- [root@centos7 data]# ll `which cat`
- -rwsr-xr-x. 1 root root 54160 Oct 31 03:16 /usr/bin/cat
[masuri@centos7 data]$ cat /etc/shadow 此时 shadow 文件可以访问
- root:$6$FBXKJJRgWCz23UDt$ji24UW5dVeYK55JOkBzBbmXaSGKAwhM1sjY9rg3TguL1GaTEmrlSzbDYNIu7p57/hehYEIE3LYLMHv2IqxIb70::0:99999:7:::
- bin:*:17834:0:99999:7:::
- daemon:*:17834:0:99999:7:::
- adm:*:17834:0:99999:7:::
- lp:*:17834:0:99999:7:::
- SGID:
作用在目录上, 当用户在拥有 SGID 权限的目录下创建文件时, 该文件的属组自动变为该目录的属组.
示例:
- [root@centos7 data]# groupadd wang
- [root@centos7 data]# mkdir testdir
- [root@centos7 data]# chown .wang testdir
- [root@centos7 data]# ll
- total 0
- drwxr-xr-x 2 root wang 6 Mar 15 01:01 testdir
- [root@centos7 data]# chmod g+s testdir
- [root@centos7 data]# touch testdir/{
- a,b
- }
- [root@centos7 data]# ll testdir/
- total 0
- -rw-r--r-- 1 root wang 0 Mar 15 01:03 a
- -rw-r--r-- 1 root wang 0 Mar 15 01:03 b
- STICKY:
作用在目录上, 表示该目录下创建的文件只有该文件的属主才能进行删除.
示例:
- [root@centos7 data]# mkdir -m 777 testdir 创建目录并赋予 777 的权限
- [root@centos7 data]# ll
- total 0
- drwxrwxrwx 2 root root 6 Mar 15 01:13 testdir
- [root@centos7 data]# touch testdir/{
- a,b
- } 在目录下创建 a 和 b 两个文件
- [root@centos7 data]# ls testdir/
- a b
- [root@centos7 data]# su masuri 切换至 masuri 用户
[masuri@centos7 data]$ rm -rf testdir/a 此时可以删除 root 创建的 a 文件
- [masuri@centos7 data]$ ll testdir/
- total 0
- -rw-r--r-- 1 root root 0 Mar 15 01:13 b
[masuri@centos7 data]$ exit 切回 root
- exit
- [root@centos7 data]# chmod o+t testdir 对 testdir 加 sticky 权限
- [root@centos7 data]# su masuri 切回 masuri 用户
[masuri@centos7 data]$ touch testdir/a 在 testder 目录下添加 a 文件
- [masuri@centos7 data]$ exit
- exit
- [root@centos7 data]# su wang 切换至 wang 用户
[wang@centos7 data]$ touch testdir/c 创建一个 c 文件
[wang@centos7 data]$ rm testdir/a
rm: remove write-protected regular empty file 'testdir/a'? y 删除 masuri 用户的 a 文件
rm: cannot remove 'testdir/a': Operation not permitted 无法删除
[wang@centos7 data]$ rm testdir/c 但是可以删除自己创建的 C 文件
- [wang@centos7 data]$ ll testdir/
- total 0
- -rw-rw-r-- 1 masuri masuri 0 Mar 15 01:20 a
- -rw-r--r-- 1 root root 0 Mar 15 01:13 b
四, 访问控制列表 acl
setfacl:
命令格式:
- setfacl [-bkndRLPvh] [{
- -m|-x
- } acl_spec] [{
- -M|-X
- } acl_file] file ...
- setfacl --restore=file
说明: 访问控制列表给与了文件更加灵活的权限设置,除了文件的所有者,所属组和其它人,可以对更多的用户设置权限 < br ztid="138" ow="0" oh="0"> | 选项 | 参数 |
---|---|---|
-m | 设置权限 | |
-R | 递归 | |
-M | ||
-x | 删除 acl 权限 | |
-X file | 参照 file 文件删除 acl 权限 | |
-k dir | 删除默认 acl 设置权限 | |
-b file | 清空 acl 权限 |
示例:
1. 对用户设置访问控制权限
- [root@centos7 data]# mkdir testdir
- [root@centos7 data]# touch testdir/{
- a,b
- }
- [root@centos7 data]# setfacl -m u:masuri:--- testdir
- [root@centos7 data]# su masuri
- [masuri@centos7 data]$ cd testdir
- bash: cd: testdir: Permission denied
- [masuri@centos7 data]$ ll
- total 0
- drwxr-xr-x+ 2 root root 24 Mar 15 03:19 testdir
- [masuri@centos7 data]$ getfacl testdir
- # file: testdir
- # owner: root
- # group: root
- user::rwx
- user:masuri:---
- group::r-x
- mask::r-x
- other::r-x
2. 备份 acl 权限和恢复 acl 权限
- [root@centos7 data]# getfacl testdir
- # file: testdir
- # owner: root
- # group: root
- user::rwx
- user:masuri:---
- group::r-x
- mask::r-x
- other::r-x
- [root@centos7 data]# getfacl testdir> acl.txt 读 acl 权限保存至文件中
- [root@centos7 data]# setfacl -b testdir 清除所有 acl 权限
- [root@centos7 data]# ll
- total 4
- -rw-r--r-- 1 root root 103 Mar 15 03:24 acl.txt
- drwxr-xr-x 2 root root 24 Mar 15 03:19 testdir
- [root@centos7 data]# setfacl --restore=acl.txt 从文件中读取 acl 权限并设置
- [root@centos7 data]# ll
- total 4
- -rw-r--r-- 1 root root 103 Mar 15 03:24 acl.txt
- drwxr-xr-x+ 2 root root 24 Mar 15 03:19 testdir
- [root@centos7 data]# getfacl testdir/
- # file: testdir/
- # owner: root
- # group: root
- user::rwx
- user:masuri:---
- group::r-x
- mask::r-x
- other::r-x
3.mask 属性, mask 的作用为限高杆, 显示文件或目录被读取时的最高权限.
- [root@centos7 data]# mkdir testdir
- [root@centos7 data]# ll testdir -d
- drwxr-xr-x 2 root root 6 Mar 15 03:35 testdir
- [root@centos7 data]# setfacl -m u:masuri:rwx testdir 给 masuri 设 acl 权限 rwx
- [root@centos7 data]# setfacl -m mask::r testdir 设置 mask 属性 r
- [root@centos7 data]# su masuri 切换至 masuri
[masuri@centos7 data]$ touch testdir/a 在 testdir 下创建文件
touch: cannot touch 'testdir/a': Permission denied 无法创建
- [masuri@centos7 data]$ getfacl testdir
- # file: testdir
- # owner: root
- # group: root
- user::rwx
- user:masuri:rwx #effective:r-- 最大权限为 r
- group::r-x #effective:r--
- mask::r--
- other::r-x
需要注意的事项:
1. 复制带有 acl 的文件时, 需要时用 cp -a 或者 -p 参数否则 acl 属性会丢失
2.acl 的生效顺序: 所有者, 自定义用户, 自定义组, 其他人.
五, 其他
chattr +i 此命令可以锁定重要文件防止误删, 即使为 root 用户也无法删除, 无法修改
chattr +a 此命令可以防止文件删除, 可以追加文件
lsattr 查看用 chattr 所设置的权限.
- [root@centos7 ~]# chattr +i a.txt 锁定 a.txt 文件
- [root@centos7 ~]# lsattr a.txt
- ----i----------- a.txt
- [root@centos7 ~]# rm -rf a.txt 无法删除
- rm: cannot remove 'a.txt': Operation not permitted
- [root@centos7 ~]# echo lsafjla>a.txt 无法覆盖
- -bash: a.txt: Permission denied
- [root@centos7 ~]# chattr -i a.txt 解除锁定
- [root@centos7 ~]# echo hello world> a.txt 可以写入
- [root@centos7 ~]# chattr +a a.txt 锁定 a.txt 设置为只能追加
- [root@centos7 ~]# echo hello world> a.txt 无法覆盖文件内容
- -bash: a.txt: Operation not permitted
- [root@centos7 ~]# echo hello world>> a.txt 可以追加
- [root@centos7 ~]# cat a.txt
- hello world
- hello world
- [root@centos7 ~]# lsattr a.txt
- -----a---------- a.txt
- [root@centos7 ~]# chattr -i a.txt
来源: http://www.bubuko.com/infodetail-2990771.html