Linux文件系统权限
Linux文件系统权限
一、属主、属组在linux文件系统中,用户如果要对文件进行操作,首先要对文件的权限进行检查,先判断用户是否是此文件的属主如果是则执行属主权限,如果不是那就查看该用户是否是该文件属组内的用户,如果是则执行属组权限,如果不是执行other权限。 二、文件和目录的读写执行1. 文件的读、写、执行文件能否读写取决于用户对文件是否有读写执行权限。用户有对文件读权限则可以访问此文件,查看文件内的内容,但无法对内容进行修改 [[email?protected] data]# echo hello world > a [[email?protected] data]# cat a hello world [[email?protected] data]# chmod 004 a [[email?protected] data]# ll total 4 -------r-- 1 root root 12 Mar 14 23:20 a [[email?protected] data]# su masuri [[email?protected] data]$ ll total 4 -------r-- 1 root root 12 Mar 14 23:20 a [[email?protected] data]$ cat a hello world [[email?protected] data]$ echo hello word >a bash: a: Permission denied 用户有对文件写权限则可以对此文件的内容进行修改,如果用户只有写权限,则无法查看内部容,但可以修改其内部的内容。 [[email?protected] data]# chmod 002 a [[email?protected] data]# cat a hello world [[email?protected] data]# ls a a [[email?protected] data]# ll a --------w- 1 root root 12 Mar 14 23:20 a [[email?protected] data]# su masuri [[email?protected] data]$ cat a cat: a: Permission denied [[email?protected] data]$ echo abc >> a [[email?protected] data]$ exit exit [[email?protected] data]# cat a hello world abc 注意:用户有对文件的执行权限则可以将文件进行执行,此类操作非常危险。一般文件不建议有执行权限。 2.目录的读写执行目录的读权限:目录有读权限则表示用户可以查看目录,可以复制目录,但无法对目录内的文件进行操作,若要对目录内的文件进行操作则必须要有执行权限 [[email?protected] data]# mkdir -m 004 test [[email?protected] data]# echo hello world > test/a [[email?protected] data]# su masuri [[email?protected] data]$ ls test ls: cannot access test/a: Permission denied 没有执行权限无法查看文件 a [[email?protected] data]$ cp -R test test2 cp: cannot stat ‘test/a’: Permission denied 没有执行权限无法复制目录下的内容只能复制目录本身 [[email?protected] 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 [[email?protected] data]$ rm test/a rm: cannot remove ‘test/a’: Permission denied 没有写和执行无法删除文件 示例2:有读和执行权限 [[email?protected] data]# chmod 005 test [[email?protected] data]# ll test -d d------r-x 2 root root 15 Mar 14 23:37 test [[email?protected] data]# su masuri [[email?protected] data]$ cp -R test test3 可复制 [[email?protected] data]$ ls test test2 test3 [[email?protected] data]$ ls test a [[email?protected] data]$ cat test/a hello world [[email?protected] data]$ rm test/a rm: remove write-protected regular file ‘test/a’? y rm: cannot remove ‘test/a’: Permission denied 无法删除 目录的写权限:当目录只有写权限时,无法对目录下的文件执行任何操作。若要能实现对目录下的文件进行操作,必须要有执行权限 [[email?protected] data]# mkdir -m 002 test [[email?protected] data]# echo helloworld > test/a [[email?protected] data]# ll total 0 d-------w- 2 root root 15 Mar 15 00:10 test [[email?protected] data]# su masuri [[email?protected] data]$ echo hello > test/a bash: test/a: Permission denied [[email?protected] data]$ ls test ls: cannot open directory test: Permission denied [[email?protected] data]$ ls test/a ls: cannot access test/a: Permission denied 2.有写和执行时 [[email?protected] data]# chmod 003 test [[email?protected] data]# ll -d test d-------wx 2 root root 15 Mar 15 00:10 test [[email?protected] data]# ls test a [[email?protected] data]# su masuri [[email?protected] data]$ ls test ls: cannot open directory test: Permission denied 没有读权限无法查看 [[email?protected] data]$ echo hello > test/b 好像可以写入? [[email?protected] data]$ rm test/a 要想也可以删除? rm: remove write-protected regular file ‘test/a’? y [[email?protected] data]$ exit 转回root看结果。 exit [[email?protected] data]# ls test [[email?protected] data]# ls test 原来的按文件被删除 b [[email?protected] data]# cat b cat: b: No such file or directory [[email?protected] data]# cat test/b b文件内为刚才写入的数据 hello 总结:目录的执行权限非常重要,没有执行权,即使有目录的读写权限也无法对目录下的文件进行操作。3.chmod中大写X的意义 [[email?protected] data]# mkdir testdir [[email?protected] data]# cd testdir [[email?protected] testdir]# touch a b 创建a b两个文件 [[email?protected] testdir]# chmod 100 a 修改权限a为可执行 [[email?protected] testdir]# chmod 000 b 修改权限b为什么都没有 [[email?protected] testdir]# mkdir -m 000 dir 创建一个dir目录并设置权限为000 [[email?protected] 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 [[email?protected] testdir]# cd .. [[email?protected] data]# chmod -R a+X testdir 对testdir目录递归设置大X权限 [[email?protected] data]# cd testdir/ [[email?protected] 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:作用在二进制程序上,当用户执行该程序时,运行该程序的身份为该程序的属主而非用户自身 [[email?protected] data]# su masuri [[email?protected] data]$ ll `which cat` 此时suid没有修改 -rwxr-xr-x. 1 root root 54160 Oct 31 03:16 /usr/bin/cat [[email?protected] data]$ cat /etc/shadow shadow文件无法访问 cat: /etc/shadow: Permission denied [[email?protected] data]$ exit exit [[email?protected] data]# chmod u+s `which cat` 修改suid [[email?protected] data]# ll `which cat` -rwsr-xr-x. 1 root root 54160 Oct 31 03:16 /usr/bin/cat [[email?protected] 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权限的目录下创建文件时,该文件的属组自动变为该目录的属组。 [[email?protected] data]# groupadd wang [[email?protected] data]# mkdir testdir [[email?protected] data]# chown .wang testdir [[email?protected] data]# ll total 0 drwxr-xr-x 2 root wang 6 Mar 15 01:01 testdir [[email?protected] data]# chmod g+s testdir [[email?protected] data]# touch testdir/{a,b} [[email?protected] 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:作用在目录上,表示该目录下创建的文件只有该文件的属主才能进行删除。 [[email?protected] data]# mkdir -m 777 testdir 创建目录并赋予777的权限 [[email?protected] data]# ll total 0 drwxrwxrwx 2 root root 6 Mar 15 01:13 testdir [[email?protected] data]# touch testdir/{a,b} 在目录下创建a和b两个文件 [[email?protected] data]# ls testdir/ a b [[email?protected] data]# su masuri 切换至masuri用户 [[email?protected] data]$ rm -rf testdir/a 此时可以删除root创建的a文件 [[email?protected] data]$ ll testdir/ total 0 -rw-r--r-- 1 root root 0 Mar 15 01:13 b [[email?protected] data]$ exit 切回root exit [[email?protected] data]# chmod o+t testdir 对testdir加sticky权限 [[email?protected] data]# su masuri 切回masuri用户 [[email?protected] data]$ touch testdir/a 在testder目录下添加a文件 [[email?protected] data]$ exit exit [[email?protected] data]# su wang 切换至wang用户 [[email?protected] data]$ touch testdir/c 创建一个c文件 [[email?protected] data]$ rm testdir/a rm: remove write-protected regular empty file ‘testdir/a’? y 删除masuri用户的a文件 rm: cannot remove ‘testdir/a’: Operation not permitted 无法删除 [[email?protected] data]$ rm testdir/c 但是可以删除自己创建的C文件 [[email?protected] 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 [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ... setfacl --restore=file
示例: [[email?protected] data]# mkdir testdir [[email?protected] data]# touch testdir/{a,b} [[email?protected] data]# setfacl -m u:masuri:--- testdir [[email?protected] data]# su masuri [[email?protected] data]$ cd testdir bash: cd: testdir: Permission denied [[email?protected] data]$ ll total 0 drwxr-xr-x+ 2 root root 24 Mar 15 03:19 testdir [[email?protected] 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权限 [[email?protected] data]# getfacl testdir # file: testdir # owner: root # group: root user::rwx user:masuri:--- group::r-x mask::r-x other::r-x [[email?protected] data]# getfacl testdir > acl.txt 读acl权限保存至文件中 [[email?protected] data]# setfacl -b testdir 清除所有acl权限 [[email?protected] 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 [[email?protected] data]# setfacl --restore=acl.txt 从文件中读取acl权限并设置 [[email?protected] 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 [[email?protected] 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的作用为限高杆,显示文件或目录被读取时的最高权限。 [[email?protected] data]# mkdir testdir [[email?protected] data]# ll testdir -d drwxr-xr-x 2 root root 6 Mar 15 03:35 testdir [[email?protected] data]# setfacl -m u:masuri:rwx testdir 给masuri设acl权限rwx [[email?protected] data]# setfacl -m mask::r testdir 设置mask属性r [[email?protected] data]# su masuri 切换至masuri [[email?protected] data]$ touch testdir/a 在testdir下创建文件 touch: cannot touch ‘testdir/a’: Permission denied 无法创建 [[email?protected] 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 需要注意的事项: 五、其他chattr +i 此命令可以锁定重要文件防止误删,即使为root用户也无法删除,无法修改 [[email?protected] ~]# chattr +i a.txt 锁定a.txt文件 [[email?protected] ~]# lsattr a.txt ----i----------- a.txt [[email?protected] ~]# rm -rf a.txt 无法删除 rm: cannot remove ‘a.txt’: Operation not permitted [[email?protected] ~]# echo lsafjla >a.txt 无法覆盖 -bash: a.txt: Permission denied [[email?protected] ~]# chattr -i a.txt 解除锁定 [[email?protected] ~]# echo hello world > a.txt 可以写入 [[email?protected] ~]# chattr +a a.txt 锁定a.txt设置为只能追加 [[email?protected] ~]# echo hello world > a.txt 无法覆盖文件内容 -bash: a.txt: Operation not permitted [[email?protected] ~]# echo hello world >> a.txt 可以追加 [[email?protected] ~]# cat a.txt hello world hello world [[email?protected] ~]# lsattr a.txt -----a---------- a.txt [[email?protected] ~]# chattr -i a.txt (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |