Linux权限解析

一、权限概述

1.权限定义

权限主要用于约束用户能对系统所做的操作。或者说,权限是指某个特定的用户具有特定的系统资源使用权力;

2.权限的作用

Linux是一个多用户系统,因此系统中不可能只存在一个root用户;权限就是为了保护每个登录用户的隐私和工作环境;

3. 权限与用户的关系

在Linux系统中,针对文件(含目录)定义了三种身份,属主(owner)、属组(group)和其他人(others),每一种身份又对应三种权限,分别是可读(readable)可写(writable)可执行(excutable)

在Linux系统中针对文件设定权限,可以赋予某个用户或用户组能够以何种方式访问某个文件;

4. 用户访问文件流程

step01:判断用户是否为文件属主;如果该用户是文件属主,则按属主权限进行访问;
step02:判断用户是否属于文件属组;如果该用户属于文件属组,则按属组权限进行访问;
step03:若用户既不是属主,也不在属组范围内,则按匿名权限进行访问;

5. 权限位标记

标记 含义 权重
r 读取权限 4
w 写入权限 2
x 执行权限 1
- 无权限(占位符) 0

二、权限基本操作

Linux下使用chmod命令变更权限;
可以使用-R选项chmod -R递归变更权限;一般用于对目录变更权限
root用户可以变更所有文件的权限;
普通用户仅能变更属于自己的文件权限

1.ugo授权法

标记 含义 备注
u 属主(User)
g 属组(Group)
o 其他人(Others)
a 所有人(All) a == ugo
chmod u=rwx 文件名 #赋予文件属组rwx权限
chmod g+rx 文件名 #针对文件属组增加rx权限
chmod o-w 文件名 #针对文件的匿名用户回收w权限
[root@Tech ~]# ll
total 0
-rw-r--r--. 1 Young root 0 Jan 19 09:44 authority.file
[root@Tech ~]# chmod a=rwx authority.file 
[root@Tech ~]# ll
total 0
-rwxrwxrwx. 1 Young root 0 Jan 19 09:44 authority.file
[root@Tech ~]# chmod ug-x,o=- authority.file 
[root@Tech ~]# ll
total 0
-rw-rw----. 1 Young root 0 Jan 19 09:44 authority.file
[root@Tech ~]# chmod g-w,o+r authority.file 
[root@Tech ~]# ll
total 0
-rw-r--r--. 1 Young root 0 Jan 19 09:44 authority.file

2.数字授权法

标记 含义 备注
7 rwx
6 rw-
5 r-x
4 r--
3 -wx
2 -w- 一般不单独使用w权限,常配合r权限使用
1 --x 一般不单独使用x权限,常配合r权限使用
0 ---
chmod 644 文件名 #赋予文件的权限为"rw-r--r--"
[root@Tech ~]# ll
total 0
-rw-r--r--. 1 Young root 0 Jan 19 09:44 authority.file
[root@Tech ~]# chmod 777 authority.file 
[root@Tech ~]# ll
total 0
-rwxrwxrwx. 1 Young root 0 Jan 19 09:44 authority.file
[root@Tech ~]# chmod 644 authority.file 
[root@Tech ~]# ll
total 0
-rw-r--r--. 1 Young root 0 Jan 19 09:44 authority.file
在实际工作中,为了保证系统安全,一般针对文件给予的最大权限为666,针对目前给予的最大权限为777;

3.变更文件属主和属主

chown和chgrp命令的详细使用说明请参照 Linux常用命令-用户与权限类 章节

  • chown命令

chown 用户名.用户组名 文件名 #变更文件的属主和属组权限

  • chgrp命令

chown 用户组名 文件名 #变更文件的=属组权限

三.权限说明

1. 概述

对象 读取权限(r) 写入权限(w) 执行权限(x)
文件 阅读文件内容 新增,修改文件内容 执行文件、命令或脚本;
(需配合r权限使用)
目录 浏览目录及子目录 创建、删除和重命名目录内的
文件(需配合x权限使用)
访问目录的内容
(取决于目录中文件权限)
Linux目录系统默认权限:755
Linux文件系统默认权限:644
文件名不是文件属性,保存在文件所在目录的block中;因此重命名文件需要对该目录具有权限;但是重命名文件需要进入目录,也需要获取目录的执行权限。
删除文件时,不仅要删除目录名还要删除文件的inode空间,所以删除文件要获取文件所在目录的写和执行权限。

2.权限验证

针对文件

文件的移动、复制、删除以及重命名是面向其所在目录的权限;
文件自身的权限仅针对查看,编辑以及执行等操作;
[Young@Tech ~]$ ll -d /opt/
drwxr-xr-x. 2 root root 33 Jan 19 15:15 /opt/
[Young@Tech ~]$ cd /opt/
[Young@Tech /opt]$ whoami
Young
#读权限
[Young@Tech /opt]$ ll
total 4
-rw-r--r--. 1 root root 47 Jan 19 15:15 authority_test.file
[Young@Tech /opt]$ cat authority_test.file 
I am Young.
This is test file for authority.
[Young@Tech /opt]$ echo "clean all" >authority_test.file 
-bash: authority_test.file: Permission denied

#写权限
[Young@Tech /opt]$ cat authority_test.file 
cat: authority_test.file: Permission denied
[Young@Tech /opt]$ echo "date" >authority_test.file 
[Young@Tech /opt]$ mv authority_test.file authority_test.file.bak
mv: cannot move 'authority_test.file' to 'authority_test.file.bak': Permission denied

#执行权限
[Young@Tech /opt]$ ll
total 4
-rw-r----x. 1 root root 5 Jan 19 15:23 authority_test.file
[Young@Tech /opt]$ cat authority_test.file 
cat: authority_test.file: Permission denied
[Young@Tech /opt]$ echo "date" >authority_test.file 
-bash: authority_test.file: Permission denied
[Young@Tech /opt]$ ./authority_test.file 
bash: ./authority_test.file: Permission denied

#执行权限+读权限
[Young@Tech /opt]$ ll
total 4
-rw-r--r-x. 1 root root 5 Jan 19 15:23 authority_test.file
[Young@Tech /opt]$ ./authority_test.file 
Wed Jan 19 15:25:16 CST 2022
[Young@Tech /opt]$ cat authority_test.file 
date
[Young@Tech /opt]$ echo "test" >authority_test.file 
-bash: authority_test.file: Permission denied

常见的文件权限有r,rw,rx三种;一般不赋予文件rwx权限。


针对目录

目录的读权限非常有限,仅能查看目录下的文件名,不能查看文件属性,也无法进入目录;
目录的写权限需要配合其他权限使用,单独赋予写权限没有任何意义;
目录的执行权限非常有限,能进入目录,并根据目录下文件的权限对文件进行操作;
目录常见的权限为rx;
rx权限可以进入目录,查看目录下文件名称和属性;但是不能操作目录(不能针对文件进行创建、复制、删除、重命名等操作),至于针对文件的操作,需要查看文件本身的权限;
注意:如果此时目录增加写权限,则针对该目录下文件就具备了写的权限;(无视文件本身属性)
#读权限
[Young@Tech ~]$ ll  authority_dir/
ls: cannot access 'authority_dir/test.file': Permission denied
total 0
-????????? ? ? ? ?            ? test.file
[Young@Tech ~]$ cat authority_dir/test.file 
cat: authority_dir/test.file: Permission denied

#写权限
[Young@Tech ~]$ ll -d authority_dir/
drwxr-x-w-. 2 root root 23 Jan 19 15:53 authority_dir/
[Young@Tech ~]$ ls authority_dir/
ls: cannot open directory 'authority_dir/': Permission denied
[Young@Tech ~]$ mv authority_dir/test.file authority_dir/test.bak
mv: failed to access 'authority_dir/test.bak': Permission denied
[Young@Tech ~]$ cat authority_dir/test.file
cat: authority_dir/test.file: Permission denied
[Young@Tech ~]$ echo "123" > authority_dir/test.bak
-bash: authority_dir/test.bak: Permission denied
[Young@Tech ~]$ cd authority_dir/
-bash: cd: authority_dir/: Permission denied

#执行权限
[Young@Tech ~]$ ll -d authority_dir/
drwxr-x--x. 2 root root 23 Jan 19 15:53 authority_dir/
[Young@Tech ~]$ cd authority_dir/
[Young@Tech ~/authority_dir]$ ls
ls: cannot open directory '.': Permission denied
[Young@Tech ~/authority_dir]$ touch new_file
touch: cannot touch 'new_file': Permission denied
[Young@Tech ~/authority_dir]$ mv test.file test.bak
mv: cannot move 'test.file' to 'test.bak': Permission denied
[Young@Tech ~/authority_dir]$ rm -f test.file
rm: cannot remove 'test.file': Permission denied
[Young@Tech ~/authority_dir]$ cat test.file
123

#读权限+写权限
[Young@Tech ~]$ ll -d authority_dir/
drwxr-xrw-. 2 root root 23 Jan 19 15:53 authority_dir/
[Young@Tech ~]$ cd authority_dir/
-bash: cd: authority_dir/: Permission denied
[Young@Tech ~]$ ll authority_dir/
ls: cannot access 'authority_dir/test.file': Permission denied
total 0
-????????? ? ? ? ?            ? test.file
[Young@Tech ~]$ mv authority_dir/test.file authority_dir/test.bak
mv: failed to access 'authority_dir/test.bak': Permission denied
[Young@Tech ~]$ cp authority_dir/test.file authority_dir/test.bak
cp: failed to access 'authority_dir/test.bak': Permission denied
[Young@Tech ~]$ rm -f authority_dir/test.file 
rm: cannot remove 'authority_dir/test.file': Permission denied
[Young@Tech ~]$ cat authority_dir/test.file 
cat: authority_dir/test.file: Permission denied

#执行权限+读权限
[Young@Tech ~]$ ll -d authority_dir/
drwxr-xr-x. 2 root root 23 Jan 19 15:53 authority_dir/
[Young@Tech ~]$ cd authority_dir/
[Young@Tech ~/authority_dir]$ ll
total 4
-rw-r--r--. 1 root root 4 Jan 19 16:05 test.file
[Young@Tech ~/authority_dir]$ mv test.file test.bak
mv: cannot move 'test.file' to 'test.bak': Permission denied
[Young@Tech ~/authority_dir]$ cp test.file test.file.bak
cp: cannot create regular file 'test.file.bak': Permission denied
[Young@Tech ~/authority_dir]$ rm -rf test.file 
rm: cannot remove 'test.file': Permission denied
[Young@Tech ~/authority_dir]$ touch new.file
touch: cannot touch 'new.file': Permission denied

#写权限+执行权限
[Young@Tech ~]$ ll -d authority_dir/
drwxr-x-wx. 2 root root 23 Jan 19 15:53 authority_dir/
[Young@Tech ~]$ cd authority_dir/
[Young@Tech ~/authority_dir]$ ls
ls: cannot open directory '.': Permission denied
[Young@Tech ~/authority_dir]$ echo "123456" test.file
123456 test.file
[Young@Tech ~/authority_dir]$ echo "123456" >test.file
-bash: test.file: Permission denied
[Young@Tech ~/authority_dir]$ mv test.file test.bak
[Young@Tech ~/authority_dir]$ touch new file
[Young@Tech ~/authority_dir]$ cp test.bak test.file
[Young@Tech ~/authority_dir]$ rm -f test.bak

#读权限+写权限+执行权限
[Young@Tech ~]$ ll -d authority_dir/
drwxr-xrwx. 2 root root 63 Jan 19 16:32 authority_dir/
[Young@Tech ~]$ cd authority_dir/
[Young@Tech ~/authority_dir]$ ll
total 8
-rw-rw-r--. 1 Young Young 0 Jan 19 16:29 file
-rw-rw-r--. 1 Young Young 0 Jan 19 16:29 new
-rw-r--r--. 1 root  root  4 Jan 19 16:32 root.file
-rw-r--r--. 1 Young Young 4 Jan 19 16:32 test.file
[Young@Tech ~/authority_dir]$ cat root.file 
123
[Young@Tech ~/authority_dir]$ echo "I am Young" >root.file 
-bash: root.file: Permission denied
[Young@Tech ~/authority_dir]$ vim root.file 
[Young@Tech ~/authority_dir]$ cat root.file 
I am Young.
I edit this file with vim.

四、特殊权限

1. SUID

进程能够以何种身份去操作一个文件,取决于运行这个进程的用户对这个文件有何种权限;

概述

Set UID简称SUID,该权限的系统权重为4000(000代表基础权限);

作用

当程序(命令)具备SUID权限时,无论哪个用户运行该程序(进程),都将以该程序的属主身份运行该程序;

特性

  • 优点:可以让普通用户执行无法执行的命令
  • 缺点:权限范围过大,使用风险系数过高;
在实际生产环境中,我们几乎不会改变文件的特殊权限;仅需要明白其含义即可。

授权方式

chmod u+s 文件名 #为文件增设SUID权限
chmod 4*** 文件名 #重新定义文件权限并设置SUID权限
chmod u-s 文件名 #为文件去除SUID权限
[root@Tech ~]# mkdir SUID_test.dir
[root@Tech ~]# ll
total 0
drwxr-xr-x. 2 root root 6 Jan 22 14:46 SUID_test.dir
[root@Tech ~]# chmod u+s SUID_test.dir
[root@Tech ~]# ll
total 0
drwsr-xr-x. 2 root root 6 Jan 22 14:46 SUID_test.dir
[root@Tech ~]# stat SUID_test.dir | awk 'NR==4 {print $1,$2}'
Access: (4755/drwsr-xr-x)
[root@Tech ~]# chmod 4644 SUID_test.dir
[root@Tech ~]# ll
total 0
drwSr--r--. 2 root root 6 Jan 22 14:46 SUID_test.dir
[root@Tech ~]# chmod u-s  SUID_test.dir
[root@Tech ~]# ll
total 0
drw-r--r--. 2 root root 6 Jan 22 14:46 SUID_test.dir
若原文件属主对该文件没有执行权限,当文件被设置SUID权限时,则在文件属主的权限执行位上显示"S";
若原文件属主对该文件拥有执行权限,当文件被设置SUID权限时,则在文件属主的权限执行位上显示"s";

2. SGID

概述

Set GID简称SGID,该权限的系统权重为2000(000代表基础权限);

作用

当目录具备SGID权限时,无论哪个用户在该目录下创建文件,文件的属组都将继承目录的权限;

特性

在实际生产环境中,基本上没有SGID的应用场景;
  • 针对用户组权限位修改,用户创建的目录或文件所属组和该目录的所属组一致;
  • 当某个目录设置SGID后,在该目录中新建的文件不是创建该文件的默认所属组;
  • 使用SGID可以使得多个用户之间共享一个目录的所有文件变的简单。

授权方式

chmod g+s 目录名 #为目录增设SGID权限
chmod 2*** 目录名 #重新定义文目录权限并设置SGID权限
chmod g-s 目录名 #为目录去除GUID权限
[root@Tech ~]# groupadd ops
[root@Tech ~]# mkdir test_SGID.dir 
[root@Tech ~]# ll -d test_SGID.dir/
drwxr-xr-x. 2 root root 6 Jan 24 09:53 test_SGID.dir/
[root@Tech ~]# chmod g+s test_SGID.dir/
[root@Tech ~]# ll -d test_SGID.dir/
drwxr-sr-x. 2 root root 6 Jan 24 09:53 test_SGID.dir/
[root@Tech ~]# chmod g-s test_SGID.dir/
[root@Tech ~]# ll -d test_SGID.dir/
drwxr-xr-x. 2 root root 6 Jan 24 09:53 test_SGID.dir/
[root@Tech ~]# touch test_SGID.dir/new_file_root.txt
[root@Tech ~]# ll test_SGID.dir/new_file_root.txt
-rw-r--r--. 1 root root 0 Jan 24 09:55 test_SGID.dir/new_file_root.txt
[root@Tech ~]# chmod 2755 test_SGID.dir
[root@Tech ~]# ll -d test_SGID.dir/
drwxr-sr-x. 2 root ops 31 Jan 24 09:55 test_SGID.dir/
[root@Tech ~]# touch test_SGID.dir/new_file_root2.txt
[root@Tech ~]# ll test_SGID.dir/
total 0
-rw-r--r--. 1 root ops  0 Jan 24 09:56 new_file_root2.txt
-rw-r--r--. 1 root root 0 Jan 24 09:55 new_file_root.txt
若原目录属组没有执行权限,当目录被设置SGID权限时,则在目录属组的权限执行位上显示"S";
若原目录属组拥有执行权限,当文件被设置SGID权限时,则在目录属组的权限执行位上显示"s";

3. SBIT

概述

Set BIT简称SBIT,该权限的系统权重为1000(000代表基础权限);

作用

当目录具备SBIT(粘滞位)权限时,用户仅可以操作该目录下属于自己的文件,根据目录权限可以完成包括创建、删除、重命名以及编辑等操作;用户不可以操作属于别人的文件;除此以外,目录的属主以及root用户可以操作该目录下的文件。

授权方式

chmod o+t 目录 #为目录增设SBIT权限
chmod o-t 目录 #为目录去除SBIT权限
chmod 1*** 目录 #重新定义文目录权限并设置SBIT权限
[root@Tech ~]# ll -d /tmp/
drwxrwxrwt. 13 root root 4096 Jan 24 10:42 /tmp/
[root@Tech ~]# mkdir test_sticky.dir
[root@Tech ~]# ll -d test_sticky.dir
drwxr-xr-x. 2 root root 6 Jan 24 11:05 test_sticky.dir
[root@Tech ~]# chmod o+t test_sticky.dir/
[root@Tech ~]# ll -d test_sticky.dir/
drwxr-xr-t. 2 root root 6 Jan 24 11:05 test_sticky.dir/
[root@Tech ~]# chmod o-t test_sticky.dir/
[root@Tech ~]# ll -d test_sticky.dir/
drwxr-xr-x. 2 root root 6 Jan 24 11:05 test_sticky.dir/
[root@Tech ~]# chmod 1754 test_sticky.dir
[root@Tech ~]# ll -d test_sticky.dir/
drwxr-xr-T. 2 root root 6 Jan 24 11:05 test_sticky.dir/
若原目录匿名用户没有执行权限,当目录被设置SBIT权限时,则在目录匿名用户的权限执行位上显示"T";
若原目录匿名用户拥有执行权限,当目录被设置SBIT权限时,则在目录匿名用户的权限执行位上显示"t";

五、权限属性

1. 设置权限属性

chattr +权限属性 文件名 #增加文件特殊的权限属性
chattr -权限属性 文件名 #取消文件特殊的权限属性
chattr =权限属性 文件名 #设置文件特殊的权限属性
lsattr 文件名 #查看文件的特殊属性
chattr只有root用户可以使用,该命令用于修改文件系统的权限属性,赋予文件凌驾于基础权限之上的特殊权限;

2. 权限属性

  • a #文件或目录仅可追加内容;
  • i #不得任意更改文件或者目录的内容;
[root@Tech ~]# lsattr /var/log/secure
------------------ /var/log/secure
[root@Tech ~]# chattr +a /var/log/secure
[root@Tech ~]# lsattr /var/log/secure
-----a------------ /var/log/secure
[root@Tech ~]# echo "tested by Young" >> /var/log/secure
[root@Tech ~]# echo "tested by Young" > /var/log/secure
-bash: /var/log/secure: Operation not permitted
[root@Tech ~]# rm -f /var/log/secure
rm: cannot remove '/var/log/secure': Operation not permitted
[root@Tech ~]# tail -1 /var/log/secure
tested by Young
[root@Tech ~]# chattr =i /var/log/secure
[root@Tech ~]# lsattr /var/log/secure
----i------------- /var/log/secure
[root@Tech ~]# echo "tested by Young 2" >> /var/log/secure
-bash: /var/log/secure: Operation not permitted
[root@Tech ~]# rm -f /var/log/secure
rm: cannot remove '/var/log/secure': Operation not permitted
[root@Tech ~]# mv  /var/log/secure /var/log/secure.log
mv: cannot move '/var/log/secure' to '/var/log/secure.log': Operation not permitted
[root@Tech ~]# chattr -i /var/log/secure
[root@Tech ~]# lsattr /var/log/secure
------------------ /var/log/secure

六、进程掩码

1. 定义

进程掩码在系统中称为UMASK;UMASK设置了用户创建文件的默认权限;系统默认UMASK为022;

UMASK中,如果存在奇数,则文件在奇数位权限上进行加1计算;UMASK中存在奇数,对目录权限无影响;
[root@Tech ~]# umask
0022
[root@Tech ~]# umask 044 #临时更改umask值
[root@Tech ~]# umask 
0044
[root@Tech ~]# su - root
Last login: Wed Feb  2 17:26:47 CST 2022 from 10.0.0.1 on pts/0
[root@Tech ~]# umask 
0022
#UMASK存在奇数的情况;
[root@Tech ~]# touch test-022.file
[root@Tech ~]# umask 023
[root@Tech ~]# touch test-023.file
[root@Tech ~]# ll test-*
-rw-r--r--. 1 root root 0 Feb  2 18:18 test-022.file
-rw-r--r--. 1 root root 0 Feb  2 18:18 test-023.file

2. 相关文件

  • /etc/bashrc
  • /etc/profile
  • ~/.bashrc
  • ~/.bash_profile
[root@Tech ~]# grep -A4 umask /etc/profile | sed -n 5,9p
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
    umask 002
else
    umask 022
fi

[root@Tech ~]# grep -i umask /etc/login.defs 
UMASK           077

3.umask影响

  • 系统中

系统中修改umask,会影响创建新文件和目录的初始权限;

  • vsftpd服务

FTP服务中,修改umask会影响FTP服务中新创建文件或创建目录的权限;

  • /etc/login.defs

该文件的UMASK更改,会影响用户家目录的权限;