linux课题之文件系统权限

12次阅读

共计 8693 个字符,预计需要花费 22 分钟才能阅读完成。

[TOC]

用户和群组

  • 文件拥有者
  • 群组:只有在组中文件才可用
  • 其他人:其他组中的人

文件权限

drwxr-xr-x. 4 root root 31 Oct 4 00:54 zhanghao

  • drwxr-xr-x:

    • 第一个字符 d: 表示文件类型

      • d:目录
      • –:文件
      • l:链接文件
      • b:可随机存储设备
      • c:键盘等一次性读取设备
    • 第一组 rwx:表示文件所属用户的使用权限,rwx 表示可读写可执行
    • 第二组 r -x:表示用户组权限
    • 第三组 r -x:表示其他人权限
  • 4: 表示有多少个文件链接到这个 i -node
  • root root: 文件的所属主和组
  • 时间:文件创建时间可以使用 ls -l –full-time 显示可读时间
文件权限解答
例题:假设 test1, test2, test3 同属于 testgroup 这个群组,如果有下面的两个文件,请说明两个
文件的拥有者与其相关的权限为何?-rw-r--r-- 1 root root 238 Jun 18 17:22 test.txt
-rwxr-xr-- 1 test1 testgroup 5238 Jun 19 10:25 ping_tsai
  • 答:

    • 文件 test.txt 的拥有者为 root,所属群组为 root。至于权限方面则只有 root 这个帐号可以存取此文件,其他人则仅能读此文件;文件拥有者 test1[rwx]可以在本目录中进行任何工作;
    • 另一个文件 ping_tsai 的拥有者为 test1,而所属群组为 testgroup。其中:test1 可以针对此文件具有可读可写可执行的权力;而同群组的 test2, test3 两个人与 test1 同样是 testgroup 的群组帐号,则仅可读可执行但不能写(亦即不能修改);至于没有加入 testgroup 这一个群组的其他人则仅可以读,不能写也不能执行!
    • 而 testgroup 这个群组 [r-x] 的帐号,例如 test2, test3 亦可以进入本目录进行工作,但是不能

在本目录下进行写入的动作;

 * 至于 other 的权限中 [r--] 虽然有 r,但是由于没有 x 的权限,因此 others 的使用者,并不能进

入此目录!

[zhanghao@zhanghao-server ~]$ cd /home/test1/
-bash: cd: /home/test1/: Permission denied
[zhanghao@zhanghao-server ~]$ ls
[zhanghao@zhanghao-server ~]$ exit
logout
[root@zhanghao-server test1]# su - test2
[test2@zhanghao-server ~]$ cd /home/test1/
-bash: cd: /home/test1/: Permission denied
[test2@zhanghao-server ~]$ ll /home/test1/
ls: cannot open directory /home/test1/: Permission denied
[test2@zhanghao-server ~]$ exit
logout
[root@zhanghao-server test1]# ll /home/
total 0
drwx------. 3 test1 test 95 Oct 4 03:54 test1
drwx------. 2 test2 test 83 Oct 4 03:55 test2
drwx------. 2 zhanghao zhanghao 83 Oct 4 03:55 zhanghao

从上面看,test1 和 test2 虽然是属于同一个 test 组,但是 /home 下面目录对于组没有任何的权限,给组一个读权限

[root@zhanghao-server test1]# chmod 0611 /home/test1
[root@zhanghao-server test1]# su - test2
Last login: Thu Oct 4 03:55:27 EDT 2018 on pts/0
[test2@zhanghao-server ~]$ cd /home/test1/
[test2@zhanghao-server test1]$ ls
ls: cannot open directory .: Permission denied

从上面可以看出其他用户在 test1 目录下面没有任何的读写,没办法 ls,touch。cd 属于执行权限

[root@zhanghao-server home]# chmod 755 test1
[root@zhanghao-server home]# ll -h
total 0
drwxr-xr-x. 3 test1 test 95 Oct 4 03:54 test1
drwx------. 2 test2 test 83 Oct 4 03:55 test2
drwx------. 2 zhanghao zhanghao 83 Oct 4 03:55 zhanghao
[root@zhanghao-server home]# su - test2
Last login: Thu Oct 4 03:58:41 EDT 2018 on pts/0
[test2@zhanghao-server ~]$ cd /home/test1
[test2@zhanghao-server test1]$ ls
test
[test2@zhanghao-server test1]$ touch nihao
touch: cannot touch‘nihao’: Permission denied
[test2@zhanghao-server test1]$ cd test/
[test2@zhanghao-server test]$ ls
nihao
[test2@zhanghao-server test]$ cat nihao o
test1
cat: o: No such file or directory
[test2@zhanghao-server test]$ cat nihao
test1
[test2@zhanghao-server test]$ vim nihao
-bash: vim: command not found
[test2@zhanghao-server test]$ vi nihao

vim 保存显示只读文件。无法 touch,

修改文件权限

  • chgrp : 修改文件组
  • chown:修改文件拥有者
  • chmod:修改文件的权限,SUID,SGID,SBIT
chgrp

/zhanghao 目录其他人权限为 0,新建用户 zhtest 在 zhanghao 组中,可以有执行和读的权限,不能写。

[zhanghao@zhanghao-server home]$ touch zhanghao
[zhanghao@zhanghao-server home]$ exit
logout
[root@zhanghao-server ~]# su - test1
Last login: Thu Oct 4 03:53:52 EDT 2018 on pts/0
[test1@zhanghao-server ~]$ cd /home/
[test1@zhanghao-server home]$ ls
nihao test1 test2 zhanghao
[root@zhanghao-server /]# chmod 0750 /zhanghao/
[root@zhanghao-server /]# ll /
total 24
lrwxrwxrwx. 1 root root 7 Oct 3 00:14 bin -> usr/bin
dr-xr-xr-x. 4 root root 4096 Oct 3 04:00 boot
drwxr-xr-x. 19 root root 3160 Oct 4 02:29 dev
drwxr-xr-x. 80 root root 8192 Oct 4 03:55 etc
drwxr-xr-x. 5 root root 61 Oct 4 04:09 home
lrwxrwxrwx. 1 root root 7 Oct 3 00:14 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Oct 3 00:14 lib64 -> usr/lib64
drwxr-xr-x. 2 root root 6 Mar 10 2016 media
dr-xr-xr-x. 9 root root 4096 Jul 11 2017 mnt
drwxr-xr-x. 2 root root 6 Mar 10 2016 opt
dr-xr-xr-x. 106 root root 0 Oct 3 23:55 proc
dr-xr-x---. 3 root root 147 Oct 4 00:17 root
drwxr-xr-x. 26 root root 800 Oct 4 00:20 run
lrwxrwxrwx. 1 root root 8 Oct 3 00:14 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 Mar 10 2016 srv
dr-xr-xr-x. 13 root root 0 Oct 3 23:55 sys
drwxrwxrwt. 11 root root 4096 Oct 4 03:26 tmp
drwxr-xr-x. 13 root root 155 Oct 3 00:14 usr
drwxr-xr-x. 19 root root 267 Oct 3 03:55 var
drwxr-x---. 4 zhanghao zhanghao 31 Oct 4 00:54 zhanghao
[root@zhanghao-server /]# su - test1
Last login: Thu Oct 4 04:18:33 EDT 2018 on pts/0
[test1@zhanghao-server ~]$ ls
test
[test1@zhanghao-server ~]$ cd /zhanghao/
-bash: cd: /zhanghao/: Permission denied
[test1@zhanghao-server ~]$

[root@zhanghao-server /]# useradd zhtest -g zhanghao
[root@zhanghao-server /]# su - zhtest
[zhtest@zhanghao-server ~]$ cd /zhanghao/
[zhtest@zhanghao-server zhanghao]$ ls
test test1
[zhtest@zhanghao-server zhanghao]$ touch niaho
touch: cannot touch‘niaho’: Permission denied
  • 修改组权限

将 /zhanghao 目录的组权限修改为 test,让 test 组下面的用户 test1,有读和执行的权限。

[root@zhanghao-server /]# chgrp test /zhanghao
[root@zhanghao-server /]# ll -h
total 24K
lrwxrwxrwx. 1 root root 7 Oct 3 00:14 bin -> usr/bin
dr-xr-xr-x. 4 root root 4.0K Oct 3 04:00 boot
drwxr-xr-x. 19 root root 3.1K Oct 4 02:29 dev
drwxr-xr-x. 80 root root 8.0K Oct 4 04:21 etc
drwxr-xr-x. 6 root root 75 Oct 4 04:21 home
lrwxrwxrwx. 1 root root 7 Oct 3 00:14 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Oct 3 00:14 lib64 -> usr/lib64
drwxr-xr-x. 2 root root 6 Mar 10 2016 media
dr-xr-xr-x. 9 root root 4.0K Jul 11 2017 mnt
drwxr-xr-x. 2 root root 6 Mar 10 2016 opt
dr-xr-xr-x. 106 root root 0 Oct 3 23:55 proc
dr-xr-x---. 3 root root 147 Oct 4 00:17 root
drwxr-xr-x. 26 root root 800 Oct 4 00:20 run
lrwxrwxrwx. 1 root root 8 Oct 3 00:14 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 Mar 10 2016 srv
dr-xr-xr-x. 13 root root 0 Oct 3 23:55 sys
drwxrwxrwt. 11 root root 4.0K Oct 4 03:26 tmp
drwxr-xr-x. 13 root root 155 Oct 3 00:14 usr
drwxr-xr-x. 19 root root 267 Oct 3 03:55 var
drwxr-x---. 4 zhanghao test 31 Oct 4 00:54 zhanghao
[root@zhanghao-server /]# su - test1
Last login: Thu Oct 4 04:19:58 EDT 2018 on pts/0
[test1@zhanghao-server ~]$ cd /zhanghao/
[test1@zhanghao-server zhanghao]$ ls
test test1
[test1@zhanghao-server zhanghao]$ touch nihao
touch: cannot touch‘nihao’: Permission denied
chown

修改文件拥有者

[root@zhanghao-server /]# chown -R root:root /zhanghao
[root@zhanghao-server /]# ll /
total 24
lrwxrwxrwx. 1 root root 7 Oct 3 00:14 bin -> usr/bin
dr-xr-xr-x. 4 root root 4096 Oct 3 04:00 boot
drwxr-xr-x. 19 root root 3160 Oct 4 02:29 dev
drwxr-xr-x. 80 root root 8192 Oct 4 04:21 etc
drwxr-xr-x. 6 root root 75 Oct 4 04:21 home
lrwxrwxrwx. 1 root root 7 Oct 3 00:14 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Oct 3 00:14 lib64 -> usr/lib64
drwxr-xr-x. 2 root root 6 Mar 10 2016 media
dr-xr-xr-x. 9 root root 4096 Jul 11 2017 mnt
drwxr-xr-x. 2 root root 6 Mar 10 2016 opt
dr-xr-xr-x. 107 root root 0 Oct 3 23:55 proc
dr-xr-x---. 3 root root 147 Oct 4 00:17 root
drwxr-xr-x. 26 root root 800 Oct 4 00:20 run
lrwxrwxrwx. 1 root root 8 Oct 3 00:14 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 Mar 10 2016 srv
dr-xr-xr-x. 13 root root 0 Oct 3 23:55 sys
drwxrwxrwt. 11 root root 4096 Oct 4 03:26 tmp
drwxr-xr-x. 13 root root 155 Oct 3 00:14 usr
drwxr-xr-x. 19 root root 267 Oct 3 03:55 var
drwxr-x---. 4 root root 31 Oct 4 00:54 zhanghao

chown -R 用户: 组 文件,可以单纯的改组

[root@zhanghao-server /]# chown -R :test zhanghao
[root@zhanghao-server /]# ll -h
total 24K
lrwxrwxrwx. 1 root root 7 Oct 3 00:14 bin -> usr/bin
dr-xr-xr-x. 4 root root 4.0K Oct 3 04:00 boot
drwxr-xr-x. 19 root root 3.1K Oct 4 02:29 dev
drwxr-xr-x. 80 root root 8.0K Oct 4 04:21 etc
drwxr-xr-x. 6 root root 75 Oct 4 04:21 home
lrwxrwxrwx. 1 root root 7 Oct 3 00:14 lib -> usr/lib
lrwxrwxrwx. 1 root root 9 Oct 3 00:14 lib64 -> usr/lib64
drwxr-xr-x. 2 root root 6 Mar 10 2016 media
dr-xr-xr-x. 9 root root 4.0K Jul 11 2017 mnt
drwxr-xr-x. 2 root root 6 Mar 10 2016 opt
dr-xr-xr-x. 107 root root 0 Oct 3 23:55 proc
dr-xr-x---. 3 root root 147 Oct 4 00:17 root
drwxr-xr-x. 26 root root 800 Oct 4 00:20 run
lrwxrwxrwx. 1 root root 8 Oct 3 00:14 sbin -> usr/sbin
drwxr-xr-x. 2 root root 6 Mar 10 2016 srv
dr-xr-xr-x. 13 root root 0 Oct 3 23:55 sys
drwxrwxrwt. 11 root root 4.0K Oct 4 03:26 tmp
drwxr-xr-x. 13 root root 155 Oct 3 00:14 usr
drwxr-xr-x. 19 root root 267 Oct 3 03:55 var
drwxr-x---. 4 root test 31 Oct 4 00:54 zhanghao
chmod
  • 权限

    • r:4
    • w:2
    • x:1
  • 附加者

    • u:所属主
    • g:所属组
    • o:其他人

chmod 755 filename
chmod u=rwx,go=rx filename

隐藏权限

umask

显示当前用户创建目录和文件夹的时候,默认的权限,用 777 减去 umask 值是默认的权限值,umask 后面加上数字表示设置。

[root@zhanghao-server ~]# umask
0022
[root@zhanghao-server ~]# umask -S
u=rwx,g=rx,o=rx
chattr 修改隐藏权限
[root@study ~]# chattr [+-=][ASacdistu] 文件或目录名称
选项与参数:+:增加某一个特殊参数,其他原本存在参数则不动。-:移除某一个特殊参数,其他原本存在参数则不动。=:设置一定,且仅有后面接的参数
A:当设置了 A 这个属性时,若你有存取此文件(或目录)时,他的存取时间 atime 将不会被修改,可避免 I/O 较慢的机器过度的存取磁盘。(目前建议使用文件系统挂载参数处理这个项目)S:一般文件是非同步写入磁盘的(原理请参考 [前一章 sync](../Text/index.html#sync) 的说明),如果加上 S 这个属性时,当你进行任何文件的修改,该更动会“同步”写入磁盘中。a:当设置 a 之后,这个文件将只能增加数据,而不能删除也不能修改数据,只有 root 才能设置这属性
c:这个属性设置之后,将会自动的将此文件“压缩”,在读取的时候将会自动解压缩,但是在储存的时候,将会先进行压缩后再储存(看来对于大文件似乎蛮有用的!)d:当 dump 程序被执行的时候,设置 d 属性将可使该文件(或目录)不会被 dump 备份
i:这个 i 可就很厉害了!他可以让一个文件“不能被删除、改名、设置链接也无法写入或新增数据!”对于系统安全性有相当大的助益!只有 root 能设置此属性
s:当文件设置了 s 属性时,如果这个文件被删除,他将会被完全的移除出这个硬盘空间,所以如果误删了,完全无法救回来了喔!u:与 s 相反的,当使用 u 来设置文件时,如果该文件被删除了,则数据内容其实还存在磁盘中,可以使用来救援该文件喔!注意 1:属性设置常见的是 a 与 i 的设置值,而且很多设置值必须要身为 root 才能设置
注意 2:xfs 文件系统仅支持 AadiS 而已
范例:请尝试到 /tmp 下面创建文件,并加入 i 的参数,尝试删除看看。[root@study ~]# cd /tmp
[root@study tmp]# touch attrtest <== 创建一个空文件
[root@study tmp]# chattr +i attrtest <== 给予 i 的属性
[root@study tmp]# rm attrtest <== 尝试删除看看
rm: remove regular empty file `attrtest'? y
rm: cannot remove `attrtest': Operation not permitted
# 看到了吗?呼呼!连 root 也没有办法将这个文件删除呢!赶紧解除设置!范例:请将该文件的 i 属性取消![root@study tmp]# chattr -i attrtest 

文件特殊权限

SUID(set UID)(4)

SUID 只能在二进制执行文件上生效,比如 /usr/bin/passwd 上面。普通用户执行具有 SUID 权限的二进制权限的时候,会临时具有 root 用户的超级权限。也就是说普通用户也是可以执行。

  • /usr/bin/passwd 之所以需要 s 权限是因为普通用户修改密码后,写入 shadow 文件没有权限,只有 root 超级权限才可以网 /etc/shadown 里面写入配置。

SGID(2)

SGID 对于目录和文件也有效,程序执行需要用户有执行 x 权限。假如我用 zhanghao 用户执行,zhanghao 用户的组是 zhanghao,但是我要执行文件的用户组是 haozi,那么我用 zhanghao 执行就会有 haozi 组的权限,对于目录和文件来说,有 SGID 权限,使用者可以对这个目录进行读和执行。

SBIT(1)

只是对于目录有效,对于文件已经没有任何效果。

  • 使用者对于这个目录有 w 和 x 的权限
  • 使用者在目录下面创建的目录,只有自己和 root 用户才可以删除。

比如 /tmp 目录。拥有 SBIT 目录,在里面创建的文件只有自己或者 root 权限才可以删除。

设置特殊权限

### SUID
$ chmod 4755

### SGID
$ chmod 2755

### SBIT
$ chmod 1755

正文完
 0