【Linux】权限相关知识点

devtools/2025/3/9 22:07:22/

思考

我们平时使用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/profileif [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; thenumask 002
elseumask 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 weissmanWe 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)

linux_186">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

http://www.ppmy.cn/devtools/165860.html

相关文章

定时任务和分布式任务框架

文章目录 一 Spring Task1.@Scheduled注解介绍2 基本用法(1)使用@EnableScheduling修饰启动类(2)创建定时任务的类(3)fixedDelay(4)fixedRate(5)cron3 执行多个任务4 设置异步执行5 @Async使用自定义线程池6 缺点二 xxl-job介绍架构图与其他任务调度平台的比较运行调…

【leetcode hot 100 206】反转链表

解法一:(头插法)在遍历链表时,将当前节点的 next 指针改为指向前一个节点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode() {}* ListNode(int val)…

html+js 轮播图

<!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>轮播图示例</title><style>/* 基本样式…

PythonCrowler

requests模块 python中原生的一款基于网络请求的模块,作用是模拟浏览器发送请求 指定url-发送请求-获取响应数据-持久化存储 pro1:爬取搜狗首页的页面数据 basic crowler import requests if __name__ __main__:urlhttps://www.sogou.comresrequests.get(url)page_datare…

游戏元宇宙崛起:AI代理IP驱动虚拟世界“无限可能”​

在科技飞速发展的当下&#xff0c;游戏元宇宙正以一种前所未有的姿态崛起&#xff0c;它犹如一颗璀璨的新星&#xff0c;吸引着无数人的目光。而AI代理IP&#xff0c;正成为驱动这个虚拟世界展现“无限可能”的关键力量。 「快代理&#xff5c;11年专注企业级代理IP云服务 —…

Redis 日常运维与故障处理

Redis 日常运维与故障处理 Redis 作为一个高性能的键值存储系统,被广泛应用于缓存、消息队列、排行榜等场景。为了确保 Redis 的稳定运行和高效性能,日常运维和故障处理至关重要。本文将详细介绍 Redis 的日常运维任务、常见故障及其解决方法,帮助运维人员有效管理和维护 R…

游戏引擎学习第140天

回顾并为今天的内容做准备 目前代码的进展到了声音混音的部分。昨天我详细解释了声音的处理方式&#xff0c;声音在技术上是一个非常特别的存在&#xff0c;但在游戏中进行声音混音的需求其实相对简单明了&#xff0c;所以今天的任务应该不会太具挑战性。 今天我们会编写一个…

yum修改阿里云

第一步&#xff1a;打开FinalShell&#xff0c;点击haodoop100&#xff0c;输入命令&#xff1a; sudo curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo 第二步&#xff1a;继续输入命令&#xff1a;sudo yum clean all …