Linux文件系统权限实例分析


Linux文件系统权限

一、属主、属组

在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
说明:
访问控制列表给与了文件更加灵活的权限设置,除了文件的所有者,所属组和其它人,可以对更多的用户设置权限
选项参数
-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

linuxboy的RSS地址:https://www.linuxboy.net/rssFeed.aspx

本文永久更新链接地址:https://www.linuxboy.net/Linux/2019-03/157537.htm

相关内容

    暂无相关文章