6.1.文件的一般权限

6.1.1.权限含义

文件详细信息

  • 用命令 ll 或 ls -l 查看文件详细信息

6.1.2.权限构成

文件权限构成

根据文件归属性对象分为:

  • owner:所有者,缩写 u

  • group :所属组,缩写 g

  • other :其他人,缩写 o

访问者三种权限

组成模式分析

字符表示 二进制表示 数字表示
--- 000 0
--x 001 1
-w- 010 2
-wx 011 3
r-- 100 4
r-x 101 5
rw- 110 6
rwx 111 7

文件与目录权限含义

权限 对文件 对目录
r(read) 可以读取文件的内容 可以列出目录下的内容,即目录下的文件的文件名
w(write) 可以更改文件的内容 可以创建或者删除目录中的任意文件 (只有 w 权限无法创建删除文件,需要和 x 权限一起使用)
x(execute) 可以作为可执行文件,如脚本执行 (只有 x 权限不可执行文件,需要与 r 权限连用) 可以切换到目录(cd)
文件可能出现的权限 目录可能出现的权限
---、r--、r-x、rw-、rwx ---、r-x、rwx

root 不受读写权限限制

只受执行权限限制

6.1.3.修改文件所属者和组

6.1.3.1.chown
  • 修改文件或目录的所有者和所属组

格式

chown   -选项     所有者:所属组     文件名/目录名
​
# chown  root  file 修改所属者为 root
# chown  root:  file 修改所属者为 root 所属组为所属者的基本组
# chown  redhat:root  file 修改所属者为 redhat 所属组为 root
# chown  :root  file 修改所属组为 root

选项

参数 功能
-R 递归修改目录即所有子文件、子目录的所属者/组
# 修改文件的所属者/组
[root@rhcsa ~]# touch t1.txt
[root@rhcsa ~]# ll t1.txt
-rw-r--r-- 1 root root 0 Jan  3 14:58 t1.txt
​
[root@rhcsa ~]# chown zyz:zyz t1.txt
[root@rhcsa ~]# ll t1.txt
-rw-r--r-- 1 zyz zyz 0 Jan  3 14:58 t1.txt
​
# 递归修改目录即目录下的子文件、子目录的所属者/组
[root@rhcsa ~]# mkdir test
[root@rhcsa ~]# touch test/file{1..3}
[root@rhcsa ~]# 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
​
[root@rhcsa ~]# chown -R zyz:zyz test/
[root@rhcsa ~]# 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 file3

6.1.4.修改文件权限

6.1.4.1.chmod
  • chmod(change mode):修改文件或目录的权限

# 字符型
chmod   -选项     [ugoa] [+-=] [rwx]      文件或目录名...
​
# 数字型
chmod   -选项     nnn     文件或目录名...
# nnn: 用数字表示的权限
选项 功能
-R 递归修改目录下所有文件,以及子目录下所有文件权限
  • 例 1

[root@rhcsa ~]# touch t1.txt
[root@rhcsa ~]# ll t1.txt
-rw-r--r-- 1 root root 0 Jan  3 14:50 t1.txt
​
[root@rhcsa ~]# chmod +x t1.txt
[root@rhcsa ~]# ll t1.txt
-rwxr-xr-x 1 root root 0 Jan  3 14:50 t1.txt
​
[root@rhcsa ~]# chmod o+w t1.txt
[root@rhcsa ~]# ll t1.txt
-rwxr-xrwx 1 root root 0 Jan  3 14:50 t1.txt
  • 例 2

[root@rhcsa ~]# touch t2.txt
[root@rhcsa ~]# ll t2.txt
-rw-r--r-- 1 root root 0 Jan  3 14:53 t2.txt
​
[root@rhcsa ~]# chmod 777 t2.txt
[root@rhcsa ~]# ll t2.txt
-rwxrwxrwx 1 root root 0 Jan  3 14:53 t2.txt
​
[root@rhcsa ~]# chmod 000 t2.txt
[root@rhcsa ~]# ll t2.txt
---------- 1 root root 0 Jan  3 14:53 t2.txt
  • 例 3

[root@rhcsa ~]# mkdir -p m1/m2
[root@rhcsa ~]# touch m1/m2/test.txt
[root@rhcsa ~]# tree m1/
m1/
└── m2
    └── test.txt
​
1 directory, 1 file
​
[root@rhcsa ~]# ll -d m1/
drwxr-xr-x 3 root root 16 Jan  3 14:56 m1/
[root@rhcsa ~]# ll -d m1/m2/
drwxr-xr-x 2 root root 22 Jan  3 14:56 m1/m2/
[root@rhcsa ~]# ll m1/m2/test.txt
-rw-r--r-- 1 root root 0 Jan  3 14:56 m1/m2/test.txt
​
[root@rhcsa ~]# chmod -R 777 m1/
​
[root@rhcsa ~]# ll -d m1/
drwxrwxrwx 3 root root 16 Jan  3 14:56 m1/
[root@rhcsa ~]# ll -d m1/m2/
drwxrwxrwx 2 root root 22 Jan  3 14:56 m1/m2/
[root@rhcsa ~]# ll m1/m2/test.txt
-rwxrwxrwx 1 root root 0 Jan  3 14:56 m1/m2/test.txt

6.2.特殊权限

  • Linux 系统中,用户对文件或目录的访问权限除了 rwx 一般权限外,还有

    • SET UID(SUID)

    • SET GID(SGID)

    • Sticky Bit(粘滞位)

  • 用于对文件或目录进行更加灵活方便的访问控制

6.2.1.SUID

  • 可执行文件设置:普通用户执行此文件时临时获得该文件所有者权限

    • 所有者 u 执行位显示 s(文件本身有 x 权限) 或 S(文件本身没有 x 权限)

    • 设置 SUID:chmod u+s

    • 取消 SUID:chmod u-s

  • 普通用户修改自身密码,修改的新密码需要保存到 /etc/shadow 文件中,而 /etc/shadow 文件的权限为 ---(只有 root 可修改),那么怎么修改自己的密码呢

    • passwd 命令用于修改用户密码,其所属所有者为 root,且设置了 SUID 权限。

    • 普通用户执行 passwd 时,可获得 root 权限,从而能够修改 /etc/shadow 文件中的密码信息。

[root@rhcsa ~]# ll /usr/bin/passwd
-rwsr-xr-x. 1 root root 32648 Aug 10  2021 /usr/bin/passwd
[root@rhcsa ~]# ll /etc/shadow
---------- 1 root root 1140 Jan  2 18:03 /etc/shadow

  • suid 仅对二进制文件有效

  • 在执行过程中,调用者会暂时获得该文件的所有者权限

  • 该权限只在程序执行的过程中有效

6.2.2.SGID

  • 目录设置:在此目录下新创建的文件和子目录继承其组属性(所属组为该目录所属组)(团队协作)

  • 可执行文件设置:普通用户执行此文件时临时获得文件所属组权限

    • 所属组 g 执行位显示 s(文件/目录本身有 x 权限) 或 S(文件/目录本身没有 x 权限)

    • 设置 SGID:chmod g+s

    • 取消 SGID:chmod g-s

一般 SGID 多用在特定的多人团队的项目开发上,在系统中用的很少

# 实验准备
[root@rhcsa ~]# mkdir  /test
[root@rhcsa ~]# ll  -d  /test
drwxr-xr-x. 2 root root 6  6月 8日 18:18 /test
[root@rhcsa ~]# groupadd  kaifa
[root@rhcsa ~]# chown  :kaifa  /test
# 该目录所属组修改为了 kaifa
[root@rhcsa ~]# ll -d  /test
drwxr-xr-x. 2 root kaifa 6  6月 8日 18:18 /test
​
# 在该目录没有 SGID 的权限下,创建文件,文件所属组不会继承父目录的所属组
[root@rhcsa ~]# touch  /test/rootfile1
[root@rhcsa ~]# ll /test/
总计 0
-rw-r--r--. 1 root root 0  6月 8日 18:20 rootfile1
​
# 添加 SGID 权限
[root@rhcsa ~]# chmod  g+s  /test
[root@rhcsa ~]# ll -d  /test
drwxr-sr-x. 2 root kaifa 6  6月 8日 18:18 /test
​
# 在该目录有 SGID 的权限下,创建文件,文件所属组会继承父目录的所属组
[root@rhcsa ~]# touch  /test/rootfile2
[root@rhcsa ~]# 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 权限
[root@rhcsa ~]# ll  -d  /test
drwxr-sr-x. 2 root kaifa 40  6月 8日 18:22 /test
[root@rhcsa ~]# chmod  o+w  /test
[root@rhcsa ~]# ll  -d  /test
drwxr-srwx. 2 root kaifa 40  6月 8日 18:22 /test
# 用 redhat 用户登录
[redhat@rhcsa ~]$ touch  /test/redhatfile1
[redhat@rhcsa ~]$ 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

6.2.3.Sticky Bit

  • 目录设置:公共可写目录,在该目录下,用户只能 “删除/重命名/移动” 自己创建的文件(root 不受限制)

    • 其他人 o 执行位显示 t(目录本身有 x 权限) 或 T(目录本身没有 x 权限)

    • 设置 Sticky Bit:chmod o+t

    • 取消 Sticky Bit:chmod o-t

# 上述公共目录里的文件,所有人都可以删除,不安全
[redhat@rhcsa ~]$ 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 的文件删除
[redhat@rhcsa ~]$ rm -rf /test/rootfile1
[redhat@rhcsa ~]$ 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 账户添加)
​

6.2.4.设置特殊权限

  • 字符格式:

    • SUID:chmod u ± s

    • SGID:chmod g ± s

    • Sticky Bit:chmod o ± t

  • 数字格式:chmod nnnn

    • 后三位是一般权限,第一位是特殊权限(第一位标志数字如下)

特殊权限 二进制表示 数字表示
- 000 0
Sticky 001 1
SGID 010 2
SGID、Sticky 011 3
SUID 100 4
SUID、Sticky 101 5
SUID、SGID 110 6
SUID、SGID、Sticky 111 7

6.3.ACL 权限

  • 给指定用户,指定目录分配指定权限(更细化的权限设置)

  • 上述权限的设置都是针对所属者所属组其他人,这三个大类的限制,如果要对其他人中的某一个人进行限制,就要用到 ACL 访问控制列表来更加精确的限制某个人的权限

6.3.1.getfacl

  • 查看 ACL 权限

getfacl     文件名
[root@rhcsa ~]# touch temp.cfg
[root@rhcsa ~]# getfacl temp.cfg        # 默认无 ACL 权限
# file: temp.cfg
# owner: root
# group: root
user::rw-
group::r--
other::r--

6.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

[root@rhcsa ~]# mkdir /project
[root@rhcsa ~]# groupadd QQgroup
[root@rhcsa ~]# useradd zhangsan
[root@rhcsa ~]# useradd lisi
[root@rhcsa ~]# gpasswd -M zhangsan,lisi QQgroup
[root@rhcsa ~]# chown root:QQgroup /project
[root@rhcsa ~]# chmod 770 /project
[root@rhcsa ~]# ll -d /project/
drwxrwx---. 2 root QQgroup 6 Jan  3 17:09 /project/
[root@rhcsa ~]# getfacl /project/
getfacl: Removing leading '/' from absolute path names
# file: project/
# owner: root
# group: QQgroup
user::rwx
group::rwx
other::---
​
[root@rhcsa ~]# useradd pt
[root@rhcsa ~]# setfacl -m u:pt:rx /project/
[root@rhcsa ~]# ll -d /project/
drwxrwx---+ 2 root QQgroup 6 Jan  3 17:09 /project/     # 注意:某文件基本权限后有+标志则说明其具有 acl 权限
[root@rhcsa ~]# 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::---
​
# 测试
[root@rhcsa ~]# su - pt
[pt@rhcsa ~]$ cd /project/
[pt@rhcsa project]$ touch a1
touch: cannot touch 'a1': Permission denied
# 删除 ACL 权限
[root@rhcsa ~]# setfacl -x u:pt /project/
[root@rhcsa ~]# 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 文件名

6.4.权限掩码

6.4.1.umask

  • 显示或者设置文件和目录的默认权限

  • 在 Linux 系统中,当用户创建一个新的文件或目录时,系统都会为新建的文件或目录分配默认的权限,该默认权限与 umask 值有关

  • 文件:实际默认权限 = 理论默认权限(0666) - umask 值

  • 目录:实际默认权限 = 理论默认权限(0777) - umask 值

# 查看默认 umask 值
[root@rhcsa ~]# umask
0022
​
# 新建文件,查看默认权限
[root@rhcsa ~]# touch file
[root@rhcsa ~]# ll file
-rw-r--r-- 1 root root 0 Jan  3 17:28 file
​
# 新建目录,查看默认权限
[root@rhcsa ~]# mkdir dir1
[root@rhcsa ~]# ll -d dir1/
drwxr-xr-x 2 root root 6 Jan  3 17:29 dir1/
# 临时修改 umask 值
[root@rhcsa ~]# umask 000
​
# 永久修改 umask 值
# 方法一
[root@rhcsa ~]# vim /etc/profile
umask 0000              # 在文件最后添加此行内容
[root@rhcsa ~]# source /etc/profile
​
# 方法二
[root@rhcsa ~]# vim ~/.bashrc
umask 0000
[root@rhcsa ~]# source ~/.bashrc
Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐