0%

Linux文件权限总结

文件权限是Linux的基础内容,本文对Linux的基础权限、默认权限、隐藏属性、特殊权限进行总结。

基础权限

使用命令行 ls -l查看权限信息:

1
2
3
4
5
6
[root@VM-8-5-centos ~]# ls -l
total 17580
-rw-r--r-- 1 root root 635 Apr 4 16:38 test.php
-rw-r--r-- 1 root root 4 Jun 15 22:20 hello
drwxr-xr-x 7 root root 4096 Jun 6 13:11 redis
......

每个文件/目录输出第一组共10个字符,例如-rw-r--r--,其中第一个字符为文件类型,-表示文件,d表示目录。
后续的9个字符,每3个字符一组分为3组,分别为文件所有者文件所属用户组其他用户的权限。
linux权限位
每一组有rwx3个权限位,分别代表读、写、执行权限,如果没有对应的权限,则显示为-

以上图为例:
文件所有者:权限位为rwx,即文件的所有者拥有读、写、执行的权限;
文件所属组:权限位为r-x,即文件所属的用户组拥有读和执行的权限;
其他用户:权限位为r-x,即文件所属的用户组拥有读和执行的权限。

对于文件和目录,rwx权限位有不同的含义,如下:

文件 目录
r 可以查看文件的内容 可以查看目录下的文件名
w 可以修改文件内容 可以在目录下创建、删除、移动文件。需要同时有x权限
x 当作脚本执行该文件 通过cd命令切换工作目录到该目录

权限表示方法

除了使用权限位rwx来表示权限,还可以使用数字来表示:

  • r = 4
  • w = 2
  • x = 1

例如同时有rwx权限时,用数字表示为7,即4+2+1。r-x用数字表示为6,即4+2。

设置权限

  • chmod命令
  • chown命令
  • chgrp命令

特殊权限

特殊权限有3个,分别是SET UIDSET GIDSticky Bit

  • SET UID

    s出现在文件拥有者的x权限位时,运行该脚本的用户将拥有文件所有者的权限

    前提是文件所有者、运行脚本的用户都有该文件的执行权限。

    例如:

    1
    2
    3
    4
    [root@VM-8-5-centos ~]# ls -l /etc/shadow
    ---------- 1 root root 1027 Jun 15 22:57 /etc/shadow
    [root@VM-8-5-centos ~]# ls -l /bin/passwd
    -rwsr-xr-x. 1 root root 34928 May 11 2019 /bin/passwd

    Linux用户的密码保存在/etc/shadow文件中,普通用户没有/etc/shadow文件的读、写权限,那普通用户如何修改密码呢?

    答案就在/bin/passwd文件的权限中:文件所有者的x权限位变成了s,意味着普通用户使用passwd修改密码时,将拥有文件所有者root的权限,这样普通用户就可以修改/etc/shadow文件了。

  • SET GID

    SET UID类似,当s出现在文件所属组的x权限位时,运行该脚本的用户将拥有文件所属用户组的权限

    例如locate命令是一个搜索文件的命令,文件的数据库保存在/var/lib/mlocate/mlocate.db,用户组slocate可以有读权限,其他用户没有这个文件的查看权限:

    1
    2
    3
    [root@VM-0-14-centos mlocate]# ls -lh
    total 16M
    -rw-r----- 1 root slocate 16M Jun 17 23:45 mlocate.db

    但是其他用户依然可以使用locate命令来查找文件,这是因为/usr/bin/locate文件有SET GID权限,让其他用户在执行locate命令时拥有slocate用户组的权限:

    1
    2
    [root@VM-8-5-centos ~]# ls -l /usr/bin/locate
    -rwx--s--x. 1 root slocate 48552 May 11 2019 /usr/bin/locate
  • Sticky Bit

    对于一个目录,当t出现在其它用户的x权限位时,在该目录中创建的文件/目录,只有创建者和root用户可以删除。Sticky Bit只对目录有意义,对文件无效。

​ 特殊权限的查看和设置与基本权限类似,例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 设置 SET UID
[root@VM-8-5-centos ~]# chmod a+x httpd
[root@VM-8-5-centos ~]# chmod u+s httpd
[root@VM-8-5-centos ~]# ls -lh
...
-rwsr-xr-x 1 root root 6.9K Jun 20 20:06 httpd

# 设置 SET GID
[root@VM-8-5-centos ~]# chmod g+s httpd
[root@VM-8-5-centos ~]# ls -lh
...
-rwsr-sr-x 1 root root 6.9K Jun 20 20:06 httpd

# 设置 Sticky Bit
[root@VM-8-5-centos ~]# chmod o+t folder/
[root@VM-8-5-centos ~]# ls -lh
....
drwxr-xr-t 2 root root 4.0K Jun 17 22:53 folder

默认权限

查看默认权限

文件创建后的默认权限使用命令umaskumask -S查看:

1
2
3
4
[root@VM-8-5-centos ~]# umask
0022
[root@VM-8-5-centos ~]# umask -S
u=rwx,g=rx,o=rx

umask命令输出表示:文件/目录创建后,需要扣除的权限。4位数字分别代表:特殊权限、所有者权限、所属用户组权限、其它用户权限。

上述输出中的0022表示所属用户组和其他用户需要扣除w权限。下面分别创建一个文件file和目录folder

1
2
3
4
5
6
[root@VM-8-5-centos ~]# touch file
[root@VM-8-5-centos ~]# mkdir folder
[root@VM-8-5-centos ~]# ls -lh
total 18M
-rw-r--r-- 1 root root 0 Jun 17 22:53 file
drwxr-xr-x 2 root root 4.0K Jun 17 22:53 folder

由于文件创建后默认没有执行权限,所以文件没有x权限位。umask输出为0022,则所属用户组权限、其他用户权限需要扣除w权限,所以文件创建后的默认权限为rw-r--r--,目录创建后的默认权限为rwxr-xr-x

设置默认权限

使用umask MODE设置默认权限,例如umask 0002,表示扣除其他用户的w权限位。

隐藏属性

文件的隐藏属性,可以用来设置文件只允许追加写入、不允许删除等特性。

有些文件系统只支持部分属性,例如xfs文件系统,只支持aAdiS属性。使用man xfsman ext4等命令查看。

常用命令如下:

  • lsattr 查看已设置的属性。

  • chattr +a 文件名

    a表示”append”,设置后文件只能追加写入,不能删除已有文件内容、不能删除文件,也不能修改文件名。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    # 创建一个文件
    [root@VM-8-5-centos ~]# touch log.txt
    [root@VM-8-5-centos ~]# echo "hello 1" >> log.txt

    # 设置只能追加写入
    [root@VM-8-5-centos ~]# chattr +a log.txt

    # 追加写入成功
    [root@VM-8-5-centos ~]# echo "hello 2" >> log.txt

    # 覆盖写入失败
    [root@VM-8-5-centos ~]# echo "hello" > log.txt
    -bash: log.txt: Operation not permitted

    # 删除失败
    [root@VM-8-5-centos ~]# rm -f log.txt
    rm: cannot remove 'log.txt': Operation not permitted

    # 修改文件名失败
    [root@VM-8-5-centos ~]# mv log.txt log2.txt
    mv: cannot move 'log.txt' to 'log2.txt': Operation not permitted

    # 查看文件属性,有a标志位
    [root@VM-8-5-centos ~]# lsattr log.txt
    -----a--------e----- log.txt

    # 取消a属性后可以覆盖写入和删除
    [root@VM-8-5-centos ~]# chattr -a log.txt
    [root@VM-8-5-centos ~]# echo hello > log.txt
    [root@VM-8-5-centos ~]# rm -f log.txt
  • chattr +i 文件名

    i代表”immutable”,意思不变的。设置该属性后,不能修改文件、重命名、删除文件,不能创建硬链接(但可以创建软链接),不能修改文件属性(例如访问时间)。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    # 创建文件并设置i属性
    [root@VM-8-5-centos ~]# touch config.ini
    [root@VM-8-5-centos ~]# chattr +i config.ini
    [root@VM-8-5-centos ~]# lsattr config.ini
    ----i---------e----- config.ini

    # 无法覆盖写入
    [root@VM-8-5-centos ~]# echo hello > config.ini
    -bash: config.ini: Operation not permitted

    # 无法追加写入
    [root@VM-8-5-centos ~]# echo hello >> config.ini
    -bash: config.ini: Operation not permitted

    # 无法删除
    [root@VM-8-5-centos ~]# rm -f config.ini
    rm: cannot remove 'config.ini': Operation not permitted

    # 无法重命名
    [root@VM-8-5-centos ~]# mv config.ini config2.ini
    mv: cannot move 'config.ini' to 'config2.ini': Operation not permitted

    # 无法创建硬链接
    [root@VM-8-5-centos ~]# ln config.ini config2.ini
    ln: failed to create hard link 'config2.ini' => 'config.ini': Operation not permitted

    # 可以创建软连接
    [root@VM-8-5-centos ~]# ln -s config.ini config2.ini

    # 也不能通过软连接更改该文件
    [root@VM-8-5-centos ~]# echo hello > config2.ini
    -bash: config2.ini: Operation not permitted

    # 不能修改文件的访问时间
    [root@VM-8-5-centos ~]# touch config.ini
    touch: setting times of 'config.ini': Operation not permitted

    # 取消i属性
    [root@VM-8-5-centos ~]# chattr -i config.ini

    # 取消i属性后可以删除
    [root@VM-8-5-centos ~]# rm -f config.ini
    [root@VM-8-5-centos ~]#
  • chattr +s 文件名

    文件删除后,使用0填充文件的区块(而不是只删除文件“目录”,防止恢复文件)。