Linux文件权限策略setfacl命令

“春风得意马蹄疾,一日看尽长安花。” ——《登高》 杜甫

前言

Linux的文件权限策略是使用setfacl命令来管理的。setfacl命令允许用户在文件和目录上添加删除修改权限。它可以根据用户、组、用户组和其他条件来定义文件和目录的权限。

命令语法

setfacl命令的语法如下:

    [root@mysql5_7 ~]# setfacl -m user:username permissions file/directory

其中,-m选项用于添加或修改权限,user:username表示要授予权限的用户名,permissions表示要授予或修改的权限。

例如,如果要授予用户 test 读、写和执行权限,可以使用以下命令:

    setfacl -m user:test:rwx file.txt

要列出当前文件和目录的所有权限,可以使用 getfacl 命令:

    [root@mysql5_7 ~]# getfacl file.txt
    # file: file.txt
    # owner: root
    # group: root
    user::rw-
    group::r--
    other::r--

这将列出file.txt文件的所有权限,并将它们打印到终端上。

操作案例

创建了一个名为test的用户。

    [root@mysql5_7 ~]# useradd  test

使用getfacl命令查看file.txt文件的ACL权限。

    [root@mysql5_7 ~]# getfacl file.txt
    # file: file.txt
    # owner: root
    # group: root
    user::rw-
    group::r--
    other::r--

使用setfacl命令将test用户添加到file.txt文件的ACL权限中,并赋予读写权限。

    [root@mysql5_7 ~]# setfacl -m user:test:rw file.txt

使用echo命令并通过管道方式将密码"123456"传递给passwd命令,将test用户的密码更改为"123456"。

    [root@mysql5_7 ~]# echo "123456" | passwd --stdin test
    Changing password for user test.
    passwd: all authentication tokens updated successfully.

使用ls -l命令查看file.txt文件的详细权限信息。

    [root@mysql5_7 ~]# ls -l file.txt
    -rw-rw-r--+ 1 root root 0 Aug  9 20:33 file.txt

使用chmod命令将file.txt文件的权限修改为只有root用户有访问权限。

    [root@mysql5_7 ~]# chmod 000 file.txt
    

使用ls -l命令再次查看file.txt文件的详细权限信息。

    [root@mysql5_7 ~]# ls -l file.txt
    ----------+ 1 root root 0 Aug  9 20:33 file.txt

使用su命令切换到test用户。

    [root@mysql5_7 ~]# su test
    [test@mysql5_7 root]$ ls
    ls: cannot open directory .: Permission denied

尝试使用ls命令查看根目录下的文件,但因权限不足而失败。

使用usermod命令将test用户添加到wheel组(提权)

    [root@mysql5_7 /]# usermod test -G  wheel 

使用echo命令将"1234567"写入file.txt文件。

    [test@mysql5_7 /]$ echo "1234567" > file.txt

使用cat命令查看file.txt文件的内容。

    [test@mysql5_7 /]$ cat file.txt
    1234567

创建了一个名为test2的用户。

    [root@mysql5_7 /]# useradd test2
    [root@mysql5_7 /]# echo "123456" | passwd --stdin test2
    Changing password for user test2.
    passwd: all authentication tokens updated successfully.

使用ls -l命令查看file.txt文件的详细权限信息,发现已经对其他用户进行了限制。

    [test2@mysql5_7 /]$ ls -l file.txt
    ----rwx---+ 1 root root 8 Aug 11 23:41 file.txt
    [test2@mysql5_7 /]$ vim file.txt
    [test2@mysql5_7 /]$ cat file.txt
    cat: file.txt: Permission denied
    [test2@mysql5_7 /]$ echo "12334" > file.txt
    bash: file.txt: Permission denied
    [test2@mysql5_7 /]$

使用vim命令尝试编辑file.txt文件,但因权限不足而失败。

使用cat命令尝试查看file.txt文件的内容,但因权限不足而失败。

使用echo命令尝试向file.txt文件写入内容,但因权限不足而失败
删除 test 用户,策略也会相应地被删除

[root@mysql5_7 /]# userdel -r test
[root@mysql5_7 /]# getfiles file.txt
-bash: getfiles: command not found
[root@mysql5_7 /]# getfacl file.txt
# file: file.txt
# owner: root
# group: root
user::---
user:1000:rwx
group::r--
mask::rwx
other::---

# 可以看到用户策略消失了

要删除所有权限,可以使用以下命令:

    setfacl -u username:username file.txt

其中,-u选项用于删除权限。

除了-m选项之外,setfacl命令还可以使用其他选项来指定其他权限和条件。有关setfacl命令的完整语法和选项

    [root@mysql5_7 ~]# setfacl --help
    setfacl 2.2.51 -- set file access control lists
    Usage: setfacl [-bkndRLP] { -m|-M|-x|-X ... } file ...
      -m, --modify=acl        modify the current ACL(s) of file(s)
      -M, --modify-file=file  read ACL entries to modify from file
      -x, --remove=acl        remove entries from the ACL(s) of file(s)
      -X, --remove-file=file  read ACL entries to remove from file
      -b, --remove-all        remove all extended ACL entries
      -k, --remove-default    remove the default ACL
          --set=acl           set the ACL of file(s), replacing the current ACL
          --set-file=file     read ACL entries to set from file
          --mask              do recalculate the effective rights mask
      -n, --no-mask           don't recalculate the effective rights mask
      -d, --default           operations apply to the default ACL
      -R, --recursive         recurse into subdirectories
      -L, --logical           logical walk, follow symbolic links
      -P, --physical          physical walk, do not follow symbolic links
          --restore=file      restore ACLs (inverse of `getfacl -R')
          --test              test mode (ACLs are not modified)
      -v, --version           print version and exit
      -h, --help              this help text

总结

本文介绍了Linux中使用setfacl命令来管理文件和目录的权限策略。setfacl命令允许用户添加、删除和修改文件和目录的权限,并根据用户、组、用户组和其他条件来定义权限。文章给出了setfacl命令的语法和示例,以及如何使用getfacl命令列出当前文件和目录的所有权限,并使用setfacl -u username:username命令删除所有权限。此外,文章还提到了setfacl命令的其他选项和完整语法。要了解更多相关信息,请参阅Linux文档或相关文档。

posted @ 2023-08-12 11:05  云中醉  阅读(578)  评论(0)    收藏  举报