目录

Linux权限相关知识点

【Linux】权限相关知识点

思考

我们平时使用Linux创建文件或目录时的默认权限是多少?

[root@localhost test]: mkdir dir 
[root@localhost test]: touch file
[root@localhost test]: ll
total 0
drwxr-xr-x 2 root root 6 Mar  8 15:23 dir   #755
-rw-r--r-- 1 root root 0 Mar  8 15:23 file  #644

可以看到文件创建时获得的权限是644

目录创建时获得的权限是755


通过对上面问题的思考,我们现在引入下面的概念:

umask

默认情况下内核给文件分配的权限是 666 ,给目录分配的权限是 777

而用户创建文件或目录时,会与umask的掩码相减获得对应的权限值

如下所示掩码值为 0022

那么创建文件时就是,666-022=644

创建目录时为,777-022=755

[root@localhost test]: umask 
0022
[root@localhost test]: umask -S
u=rwx,g=rx,o=rx

接下来我们试着修改umask的值,再去创建文件和目录会发生什么?

[root@localhost test]: umask 0777
[root@localhost test]: mkdir new_dir
[root@localhost test]: touch new_file
[root@localhost test]: ll
total 0
drwxr-xr-x 2 root root 6 Mar  8 15:23 dir
-rw-r--r-- 1 root root 0 Mar  8 15:23 file
d--------- 2 root root 6 Mar  8 15:45 new_dir
---------- 1 root root 0 Mar  8 15:49 new_file

可以看到新的文件于目录的权限全部为000了。


umask的值,默认是在/etc/profile中的脚本初始化后得到的

cat /etc/profile

if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
    umask 002
else
    umask 022
fi

chmod

格式1:

用户类别

u: 所有者(user)

g: 所属组(group)

o: 其他用户(others)

a: 所有用户(等同于 ugo,默认值)

操作符

+: 添加权限

-: 移除权限

=: 直接设置权限(覆盖原有权限)

chmod u+x file        # 给所有者添加执行权限  
chmod go-w file       # 移除组和其他用户的写权限  
chmod a=rw file       # 所有用户设置为读写权限  
chmod -R o+rX dir     # 递归给其他用户添加读权限,目录加执行权限  

格式2:

权限计算

将权限对应的数值相加:

r (读) = 4

w (写) = 2

x (执行) = 1

chmod nnn 文件或目录
chmod 777 dir_file

目录权限:

目录的 x 权限表示可进入(cd),r 权限表示可列出内容。

若目录无 x 权限,即使有 r 权限也无法读取内部文件列表。

chown

必须是root

用户和组必须存在

格式:

chown 所有者 文件
chown :所属组 文件
chown 所有者:所属组 文件

chown luobozi:atri /home/ytl

chgrp

必须是root或者文件的所有者

必须是新的组的成员

格式:

chgrp 所属组 文件

查看常见目录的权限

[root@localhost perm]: ll /home
total 0
drwx------ 2 kotonghitoli bondband  62 Mar  7 14:57 gdyg
drwx------ 2 luobozi      luobozi   62 Mar  7 10:50 luobozi
drwx------ 2 luobozi_1    luobozi_1 62 Mar  7 16:17 luobozi_1
drwx------ 2 luobozi_2    luobozi_2 62 Mar  7 16:17 luobozi_2
drwx------ 2 luobozi_3    luobozi_3 62 Mar  7 16:17 luobozi_3
drwx------ 2 luobozi_4    luobozi_4 62 Mar  7 16:17 luobozi_4
drwx------ 2 luobozi_5    luobozi_5 62 Mar  7 16:17 luobozi_5
[root@localhost perm]: ll -d /
dr-xr-xr-x. 22 root root 4096 Mar  8 16:28 /
[root@localhost perm]: ll -d /root
dr-xr-x---. 15 root root 4096 Mar  8 15:23 /root
[root@localhost perm]: ll -d /tmp/
drwxrwxrwt. 9 root root 255 Mar  8 16:30 /tmp/

sudo 授权

在Linux中root用户的权限最大

普通用户的权限很小,那么如何让普通用户也具有一定的权限呢?

修改配置文件,使得普通用户可以使用sudo提权

1.创建一个king用户,使他可以使用sudo提权执行所有命令

root@localhost ~]: useradd king
[root@localhost ~]: echo "123456" |passwd king --stdin
Changing password for user king.
passwd: all authentication tokens updated successfully.

#添加如下内容
vim /etc/sudoers
king    ALL=(ALL)       ALL

#使用sudo 提权查看root的家目录
su - king
Last login: Sat Mar  8 17:13:00 CST 2025 on pts/1
[king@localhost ~]$ sudo ls /root
[sudo] password for king: 
2025-1-14  anaconda-ks.cfg    dbbackup    functions  grep_file  shell  shell_bk_db   while_read.sh

2.定义命令别名USERADMINS包含useradd、userdel、passwd、groupadd、groupdel命令

#添加如下内容
vim /etc/sudoers
Cmnd_Alias USERADMINS = /usr/sbin/useradd, /usr/sbin/userdel, /usr/bin/passwd,/usr/sbin/groupadd,/usr/sbin/groupdel

3.新建用户组 k ,里面的成员有 mikoto、reisi、weissman

设置sudoers文件允许k组里的成员可以使用 USERADMINS 命令别名里的命令,在任何机器上

[root@localhost ~]: groupadd k
[root@localhost ~]: useradd mikoto -G k
[root@localhost ~]: useradd reisi -G k
[root@localhost ~]: useradd weissman -G k
[root@localhost ~]: echo "123456" |passwd mikoto --stdin
[root@localhost ~]: echo "123456" |passwd reisi --stdin
[root@localhost ~]: echo "123456" |passwd weissman --stdin
vim  /etc/sudoers
%k ALL = USERADMINS

验证是否成功:

[root@localhost ~]: su - weissman
[weissman@localhost ~]$ sudo useradd neko -G weissman

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

[sudo] password for weissman: 
[weissman@localhost ~]$ id neko
uid=3013(neko) gid=3015(neko) groups=3015(neko),3012(weissman)

selinux

是Linux系统里的安全子系统(security linux)

它可以限制哪些进程能访问哪些类型的文件

默认情况下它是开启状态,一般情况下我们都是将它关闭的,修改配置文件永久关闭

[root@localhost ~]: cat /etc/selinux/config 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 


[root@localhost ~]: setenforce 0 临时关闭命令
[root@localhost ~]: getenforce 
Disabled

chattr

设置文件的隐藏属性

选项 [+-] 加是授予 减是取消

-R 递归修改

+i 锁定文件,任何人都不能改动,包括root用户

+a 设置文件只能增加,不能修改和删除

[root@localhost ~]: mkdir test1
[root@localhost ~]: chattr +i test1/
[root@localhost ~]: lsattr -d test1
----i----------- test1
[root@localhost ~]: mkdir test1/dir1
mkdir: cannot create directory ‘test1/dir1’: Permission denied
[root@localhost ~]: chattr -i test1
[root@localhost ~]: mkdir test1/dir1
[root@localhost ~]: ll test1
total 0
drwxr-xr-x 2 root root 6 Mar  8 18:05 dir1

lsattr

查看文件的隐藏属性

-R 递归修改

-d 查看目录本身的属性

[root@localhost ~]: lsattr -d test1
----i----------- test1

SET位权限 特别权限位

SUID (4): 文件以所有者权限执行(如 /usr/bin/passwd)。

SGID (2): 目录中新文件继承组权限。

粘滞位 (1): 仅文件所有者可删除目录内文件(如 /tmp)。

suid:如果摸个文件具有suid权限位,普通用户在执行这个命令的时候,会以文件拥有者身份去执行

粘滞位(sticky)

使用 chmod 修改set位

[root@localhost ~]: ll /bin/passwd 
-rwsr-xr-x. 1 root root 27856 Apr  1  2020 /bin/passwd
#可以看到passwd的执行位置就是s

修改mkdir的suid权限位置

注意观察属主的执行位置的权限变化

第1位(可选)

(SUID=4,SGID=2,粘滞位=1)

[root@localhost ~]: chmod u+s /bin/mkdir
[root@localhost ~]: ll /bin/mkdir 
-rwsr-xr-x. 1 root root 79768 Aug 20  2019 /bin/mkdir


[root@localhost ~]: chmod 0755 /bin/mkdir 
[root@localhost ~]: ll /bin/mkdir 
-rwxr-xr-x. 1 root root 79768 Aug 20  2019 /bin/mkdir


[root@localhost ~]: chmod 4755 /bin/mkdir 
[root@localhost ~]: ll /bin/mkdir 
-rwsr-xr-x. 1 root root 79768 Aug 20  2019 /bin/mkdir

ACL 访问控制列表

一个文件/目录的访问控制列表,可以针对任意指定的用户/组使用权限字符分配rwx权限

setfacl

格式 setfacl 选项 规则 文件

-m 新增或修改ACL中的规则

-b 删除所有ACL规则

-x 删除指定的ACL规则

setfacl -m user:(uid/name):(perms)  #指定某个使用者的权限
setfacl -m group:(uid/name):(perms) #指定某一个群组的权限
setfacl -m other::(perms)  #指定其它使用者的权限
setfacl -m mask::(perms)   #设定有效的最大权限

user、group、other、mask 简写为: u,g,o,m

perms使用 rwx

[root@localhost test]: setfacl -m u:luobozi:rw test.txt
[root@localhost test]: getfacl test.txt
# file: test.txt
# owner: root
# group: root
user::rw-
user:luobozi:rw-
group::r--
mask::rw-
other::r--

getfacl

格式:getfacl 文件

[root@localhost test]: getfacl test.txt
# file: test.txt
# owner: root
# group: root
user::rw-
user:luobozi:rw-
group::r--
mask::rw-
other::r--

acl练习

  1. 创建三个组 homura 、silver、scepter4
  2. 创建三个用户, anna 属于homura组, seri 属于scepter4组, neko 属于silver组
  3. 在根目录下创建新目录 sword ,再将/etc/passwd文件复制到sword目录下
  4. 设置权限,passwd文件能被homura组读写,seri这个用户能读写执行,neko这个用户不能进行任何操作。
[root@localhost test]: groupadd homura
[root@localhost test]: groupadd silver
[root@localhost test]: groupadd scepter4
[root@localhost test]: useradd -G homura anna
[root@localhost test]: useradd -G scepter4 seri
[root@localhost test]: useradd -G silver neko

[root@localhost sword]: mkdir /sword
[root@localhost ~]: chmod 777 /sword/
[root@localhost sword]: cp /etc/passwd /sword

[root@localhost sword]: setfacl -m g:homura:rw passwd
[root@localhost sword]: setfacl -m u:seri:rwx passwd
[root@localhost sword]: setfacl -m u:neko:--- passwd


[root@localhost sword]: getfacl passwd
# file: passwd
# owner: root
# group: root
user::rw-
user:seri:rwx
user:neko:---
group::r--
group:homura:rw-
mask::rwx
other::r--

验证结果

[root@localhost sword]: su neko
neko@localhost sword]$ echo "hellp" >> passwd
bash: passwd: Permission denied
[neko@localhost sword]$ cat passwd
cat: passwd: Permission denied

[root@localhost sword]: su seri
[seri@localhost sword]$ ./passwd   #可以执行
./passwd: line 1: root:x:0:0:root:/root:/bin/bash: No such file or directory
[seri@localhost sword]$ echo "hello" >> passwd
[seri@localhost sword]$ tail -1 passwd
hello

[root@localhost sword]: su anna
[anna@localhost sword]$ ./passwd   #无法执行
bash: ./passwd: Permission denied
[anna@localhost sword]$ echo "hello,anna" >> passwd
[anna@localhost sword]$ tail -1 passwd
hello,anna