Linux 文件系统权限 📅 2026/7/5 7:57:55 1.文件的一般权限1.1.权限含义文件详细信息用命令 ll 或 ls -l 查看文件详细信息1.2.权限构成文件权限构成根据文件归属性对象分为:owner所有者缩写 ugroup 所属组缩写 gother 其他人缩写 o组成模式分析字符表示二进制表示数字表示---0000--x0011-w-0102-wx0113r--1004r-x1015rw-1106rwx1117文件与目录权限含义权限对文件对目录r(read)可以读取文件的内容可以列出目录下的内容即目录下的文件的文件名w(write)可以更改文件的内容可以创建或者删除目录中的任意文件 只有 w 权限无法创建删除文件需要和 x 权限一起使用x(execute)可以作为可执行文件如脚本执行只有 x 权限不可执行文件需要与 r 权限连用可以切换到目录cd文件可能出现的权限目录可能出现的权限---、r--、r-x、rw-、rwx---、r-x、rwx注意root 不受读写权限限制只受执行权限限制1.3.修改文件所属者和组1.3.1.chown修改文件或目录的所有者和所属组格式chown -选项 所有者:所属组 文件名/目录名 # chown root file 修改所属者为 root # chown root: file 修改所属者为 root 所属组为所属者的基本组 # chown redhat:root file 修改所属者为 redhat 所属组为 root # chown :root file 修改所属组为 root选项参数功能-R递归修改目录即所有子文件、子目录的所属者/组# 修改文件的所属者/组 [rootzzz ~]# touch t1.txt [rootzzz ~]# ll t1.txt -rw-r--r-- 1 root root 0 Jan 3 14:58 t1.txt [rootzzz ~]# chown zyz:zyz t1.txt [rootzzz ~]# ll t1.txt -rw-r--r-- 1 zyz zyz 0 Jan 3 14:58 t1.txt # 递归修改目录即目录下的子文件、子目录的所属者/组 [rootzzz ~]# mkdir test [rootzzz ~]# touch test/file{1..3} [rootzzz ~]# ll -d test/ ; ll test/ drwxr-xr-x. 2 root root 45 Feb 15 13:53 test/ total 0 -rw-r--r--. 1 root root 0 Feb 15 13:53 file1 -rw-r--r--. 1 root root 0 Feb 15 13:53 file2 -rw-r--r--. 1 root root 0 Feb 15 13:53 file3 [rootzzz ~]# chown -R zyz:zyz test/ [rootzzz ~]# ll -d test/ ; ll test/ drwxr-xr-x. 2 zyz zyz 45 Feb 15 13:53 test/ total 0 -rw-r--r--. 1 zyz zyz 0 Feb 15 13:53 file1 -rw-r--r--. 1 zyz zyz 0 Feb 15 13:53 file2 -rw-r--r--. 1 zyz zyz 0 Feb 15 13:53 file31.4.修改文件权限1.4.1.chmodchmodchange mode修改文件或目录的权限# 字符型 chmod -选项 [ugoa] [-] [rwx] 文件或目录名... # 数字型 chmod -选项 nnn 文件或目录名... # nnn: 用数字表示的权限选项功能-R递归修改目录下所有文件以及子目录下所有文件权限例如[rootzzz ~]# touch t1.txt [rootzzz ~]# ll t1.txt -rw-r--r-- 1 root root 0 Jan 3 14:50 t1.txt [rootzzz ~]# ll t1.txt -rwxr-xr-x 1 root root 0 Jan 3 14:50 t1.txt [rootzzz ~]# chmod ow t1.txt [rootzzz ~]# ll t1.txt -rwxr-xrwx 1 root root 0 Jan 3 14:50 t1.txt2.特殊权限Linux 系统中用户对文件或目录的访问权限除了 rwx 一般权限外还有SET UIDSUIDSET GIDSGIDSticky Bit粘滞位用于对文件或目录进行更加灵活方便的访问控制2.1.SUID可执行文件设置普通用户执行此文件时临时获得该文件所有者权限所有者 u 执行位显示 s文件本身有 x 权限 或 S文件本身没有 x 权限设置 SUIDchmod us取消 SUIDchmod u-s普通用户修改自身密码修改的新密码需要保存到 /etc/shadow 文件中而 /etc/shadow 文件的权限为 ---只有 root 可修改那么怎么修改自己的密码呢passwd 命令用于修改用户密码其所属所有者为 root且设置了 SUID 权限。普通用户执行 passwd 时可获得 root 权限从而能够修改 /etc/shadow 文件中的密码信息[rootzzz ~]# ll /usr/bin/passwd -rwsr-xr-x. 1 root root 32648 Aug 10 2021 /usr/bin/passwd [rootzzz ~]# ll /etc/shadow ---------- 1 root root 1140 Jan 2 18:03 /etc/shadow注意suid 仅对二进制文件有效在执行过程中调用者会暂时获得该文件的所有者权限该权限只在程序执行的过程中有效2.2.SGID目录设置在此目录下新创建的文件和子目录继承其组属性所属组为该目录所属组团队协作可执行文件设置普通用户执行此文件时临时获得文件所属组权限所属组 g 执行位显示 s文件/目录本身有 x 权限 或 S文件/目录本身没有 x 权限设置 SGIDchmod gs取消 SGIDchmod g-s注意一般 SGID 多用在特定的多人团队的项目开发上在系统中用的很少# 实验准备 [rootzzz ~]# mkdir /test [rootzzz ~]# ll -d /test drwxr-xr-x. 2 root root 6 6月 8日 18:18 /test [rootzzz ~]# groupadd kaifa [rootrhcsa ~]# chown :kaifa /test # 该目录所属组修改为了 kaifa [rootzzz ~]# ll -d /test drwxr-xr-x. 2 root kaifa 6 6月 8日 18:18 /test # 在该目录没有 SGID 的权限下创建文件文件所属组不会继承父目录的所属组 [rootzzz ~]# touch /test/rootfile1 [rootzzz ~]# ll /test/ 总计 0 -rw-r--r--. 1 root root 0 6月 8日 18:20 rootfile1 # 添加 SGID 权限 [rootzzz ~]# chmod gs /test [rootzzz ~]# ll -d /test drwxr-sr-x. 2 root kaifa 6 6月 8日 18:18 /test # 在该目录有 SGID 的权限下创建文件文件所属组会继承父目录的所属组 [rootzzz ~]# touch /test/rootfile2 [rootzzz ~]# ll /test 总计 0 -rw-r--r--. 1 root root 0 6月 8日 18:20 rootfile1 -rw-r--r--. 1 root kaifa 0 6月 8日 18:22 rootfile2 # 所属组继承父目录 # 给该目录其他人添加上 w 权限 [rootzzz ~]# ll -d /test drwxr-sr-x. 2 root kaifa 40 6月 8日 18:22 /test [rootzzz ~]# chmod ow /test [rootzzz ~]# ll -d /test drwxr-srwx. 2 root kaifa 40 6月 8日 18:22 /test # 用 redhat 用户登录 [redhatzzz ~]$ touch /test/redhatfile1 [redhatzzz ~]$ ll /test 总计 0 -rw-r--r--. 1 redhat kaifa 0 6月 8日 18:25 redhatfile1 # 普通用户创建文件所属组继承父目录 -rw-r--r--. 1 root root 0 6月 8日 18:20 rootfile1 -rw-r--r--. 1 root kaifa 0 6月 8日 18:22 rootfile22.3.Sticky Bit目录设置公共可写目录在该目录下用户只能 “删除/重命名/移动” 自己创建的文件root 不受限制其他人 o 执行位显示 t目录本身有 x 权限 或 T目录本身没有 x 权限设置 Sticky Bitchmod ot取消 Sticky Bitchmod o-t# 上述公共目录里的文件所有人都可以删除不安全 [redhatzzz ~]$ ll /test 总计 0 -rw-r--r--. 1 redhat kaifa 0 6月 8日 18:25 redhatfile1 -rw-r--r--. 1 root root 0 6月 8日 18:20 rootfile1 -rw-r--r--. 1 root kaifa 0 6月 8日 18:22 rootfile2 # redhat 用户可以把 root 的文件删除 [redhatzzz ~]$ rm -rf /test/rootfile1 [redhatzzz ~]$ ll /test 总计 0 -rw-r--r--. 1 redhat kaifa 0 6月 8日 18:25 redhatfile1 -rw-r--r--. 1 root kaifa 0 6月 8日 18:22 rootfile2 # 给其他人这一组添加上 t 的权限使用 root 账户添加2.4.设置特殊权限字符格式SUIDchmod u ± sSGIDchmod g ± sSticky Bitchmod o ± t数字格式chmod nnnn后三位是一般权限第一位是特殊权限第一位标志数字如下特殊权限二进制表示数字表示-0000Sticky0011SGID0102SGID、Sticky0113SUID1004SUID、Stikcy1015SUID、SGID1106SUID、SGID、Sticky11173.ACL 权限给指定用户指定目录分配指定权限更细化的权限设置上述权限的设置都是针对所属者所属组其他人这三个大类的限制如果要对其他人中的某一个人进行限制就要用到 ACL 访问控制列表来更加精确的限制某个人的权限3.1.getfacl查看 ACL 权限getfacl 文件名[rootzzz ~]# touch temp.cfg [rootzzz ~]# getfacl temp.cfg # 默认无 ACL 权限 # file: temp.cfg # owner: root # group: root user::rw- group::r-- other::r--3.2.setfacl设置 ACL 权限格式setfacl -选项 文件名选项参数功能-m设置 ACL 条目给用户设置 ACL 权限setfacl -m u:用户名:权限 文件名给用户组设置 ACL 权限setfacl -m g:组名:权限 文件名-x删除指定 ACL 条目-b删除所有 ACL 条目-d设置目录默认ACL条目针对目录在此目录下新创建的文件和子目录会继承该默认ACL-k删除目录默认 ACL 权限-R递归设置 ACL 权限例root 用户在根目录下创建目录 /project 及所属工作组 QQgroup所属组里面创建两个用户 zhangsan 和 lisi此文件权限是 770再创建一个旁听用户 pt给他设定 /project 目录的 ACL 为 r-x[rootzzz ~]# mkdir /project [rootzzz ~]# groupadd QQgroup [rootzzz ~]# useradd zhangsan [rootzzz ~]# useradd lisi [rootzzz ~]# gpasswd -M zhangsan,lisi QQgroup [rootzzz ~]# chown root:QQgroup /project [rootzzz ~]# chmod 770 /project [rootzzz ~]# ll -d /project/ drwxrwx---. 2 root QQgroup 6 Jan 3 17:09 /project/ [rootzzz ~]# getfacl /project/ getfacl: Removing leading / from absolute path names # file: project/ # owner: root # group: QQgroup user::rwx group::rwx other::--- [rootzzz ~]# useradd pt [rootzzz ~]# setfacl -m u:pt:rx /project/ [rootzzz ~]# ll -d /project/ drwxrwx--- 2 root QQgroup 6 Jan 3 17:09 /project/ # 注意:某文件基本权限后有标志则说明其具有 acl 权限 [rootzzz ~]# getfacl /project/ getfacl: Removing leading / from absolute path names # file: project/ # owner: root # group: QQgroup user::rwx user:pt:r-x group::rwx mask::rwx # mask 限制除了所有者user::和其他人other::之外的所有 ACL 权限的 “最大可用权限” other::--- # 测试 [rootzzz ~]# su - pt [ptzzz ~]$ cd /project/ [ptzzz project]$ touch a1 touch: cannot touch a1: Permission denied# 删除 ACL 权限 [rootzzz ~]# setfacl -x u:pt /project/ [rootzzz ~]# getfacl /project/ getfacl: Removing leading / from absolute path names # file: project/ # owner: root # group: QQgroup user::rwx group::rwx mask::rwx other::--- # 删除指定用户的 ACL 权限 setfacl -x u:用户名 文件名 # 删除指定组的 ACL 权限 setfacl -x g:组名 文件名 # 删除文件所有 ACL 权限 setfacl -b 文件名4.权限掩码4.1.umask显示或者设置文件和目录的默认权限在 Linux 系统中当用户创建一个新的文件或目录时系统都会为新建的文件或目录分配默认的权限该默认权限与 umask 值有关文件实际默认权限 理论默认权限0666 - umask 值目录实际默认权限 理论默认权限0777 - umask 值# 查看默认 umask 值 [rootzzz ~]# umask 0022 # 新建文件查看默认权限 [rootzzz ~]# touch file [rootzzz ~]# ll file -rw-r--r-- 1 root root 0 Jan 3 17:28 file # 新建目录查看默认权限 [rootzzz ~]# mkdir dir1 [rootzzz ~]# ll -d dir1/ drwxr-xr-x 2 root root 6 Jan 3 17:29 dir1/# 临时修改 umask 值 [rootzzz ~]# umask 000 # 永久修改 umask 值 # 方法一 [rootzzz ~]# vim /etc/profile umask 0000 # 在文件最后添加此行内容 [rootzzz ~]# source /etc/profile # 方法二 [rootzzz ~]# vim ~/.bashrc umask 0000 [rootzzz ~]# source ~/.bashrc