Linux系统配置及服务管理-第3章 用户的权限(连载)

本文深入讲解Linux系统的文件权限管理,包括基本权限UGO的概念及应用、ACL(访问控制列表)的设置与操作、特殊权限如suid的作用及风险、文件属性chattr的使用方法以及进程掩码umask对新建文件权限的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一,基本权限UGO

赋于某个用户或组 能够以何种方式 访问某个文件(图片文件,视频文件,普通文件)

1、权限设置对象:属主(u) 属组(g) 其他人(o) 所有人(a(u+g+o))
属主:文件的主人:u 属组:文件属于组员权限:g 其他人:除了主人和组员之外的用户:other
特殊对象:所有人(包含主人,组员,其他):all 全部

2、权限类型:读:r=4 写:w=2 执行:x=1

3、查看权限记录

[root@centos ~]#ls  -l  /root/1.txt

得出
-rw-r–r--. 1 root root 53 3月 11 13:55 /root/1.txt

-文件类型
rw-主人的权限,属主
r–属组的权限,
r–其他人的权限
.权限的扩展
1文件链接(第七章文件链接)
root文件的属主
root文件的属组
53大小
3月 11 13:55文件最后的修改时间
/root/1.txt 文件的名和路径

4.设置权限

4.1更改权限
使用符号
语法: chmod 对象(u/g/o/a)赋值符(+/-/=)权限类型(r/w/x) 文件/目录

1.了解普通文件的基本权限

[root@centos ~]# cd /tmp
[root@centos tmp]# touch file66
[root@centos tmp]# ll file66

-rw-r–r--. 1 root root 0 3月 11 16:27 file66
权限-rw-r–r--. 1 属主root 属组root 文件file66

2.编写程序(在创建的file66中编写一个程序做后面的运行测试)
echo “郑州欢迎你!”
read -p “请输入你的姓名:” name
echo "欢迎 $name 下次再来 "

**3.增加执行权限:
[root@centos tmp]# chmod u+x file66 //属主增加执行

4.运行测试。成功

[root@centos tmp]# ./file66
*郑州欢迎你!
请输入你的姓名:****
*欢迎* *** *下次再来**

5.去除权限。运行失败

[root@centos tmp]# chmod u-x file66
[root@centos tmp]# ./file66
-bash: ./file66: 权限不够

使用数字:
读:r=4
写:w=2
执行: x=1
[root@centos tmp]# chmod 644 file66 //属主的权限可以读写,属组和 其他人只能读

[root@centos tmp]# ll file66
-rw-r--r--. 1 root root 99 7月  28 03:56 file66

4.2更改属主、属组(chown命令和chgrp命令)

1.chown命令:

chown: 设置一个文件属于谁,属主
语法: chown 用户名.组名 文件 //改属主、属组
chown 用户名 文件 //改属主
chown .组名 文件 //改属组

[root@centos tmp]# ll file66
-rw-r--r--. 1 root root 99 7月  28 03:56 file66

属主root 数组root
使用chown 修改一个文件属于谁

[root@centos tmp]# chown user01.zz file66
[root@centos tmp]# ll file66

```powershell
-rw-r--r--. 1 user01 zz 99 7月  28 03:56 file66

属主user01 属组zz

2.chgrp命令
chgrp: 设置一个文件属于哪个组,属组
语法: chgrp 组名 文件 -R是递归的意思

[root@centos ~]# chgrp it file1 //改文件属组
[root@centos ~]# chgrp -R it dir1 //改文件属组

案例
案例需求:文件file20.txt,属主是user11,读写执行 7(可以看内容,可以改内容,可以执行) 属组是mm(user22),读取 4(只能看,不能改,不能执行)其他人 没有权限 0(既不能看,又不能改和执行)

1.使用root账户,来到/tmp目录

[root@centos tmp]# cd /tmp

2.创建文件file20.txt

[root@centos tmp]# pwd
/tmp
[root@centos tmp]# touch file20.txt
[root@centos tmp]# ll file20.txt 
-rw-r--r--. 1 root root 0 7月  28 04:58 file20.txt

主人:读写(rw) 组:读® 其他人:读®

3.准备测试账号
创建用户 ( user11 ,user22 ,user33 ),创建组(mm) ,将用户user22追加到组mm

[root@centos tmp]# useradd user11
[root@centos tmp]# useradd user22
[root@centos tmp]# useradd user33
[root@centos tmp]# groupadd mm
[root@centos tmp]# usermod -aG mm user22
[root@centos tmp]# id user22
uid=1008(user22) gid=1008(user22) 组=1008(user22),1679(mm)

4.授予文件属主和属组,以及其他人的权限。

[root@centos tmp]# chmod 740 file20.txt 
[root@centos tmp]# ll file20.txt 
-rwxr-----. 1 root root 0 7月  28 04:58 file20.txt

5.修改文件属主和属组。chown

[root@centos tmp]# chown user11.mm file20.txt 
[root@centos tmp]# ll file20.txt 
-rwxr-----. 1 user11 mm 0 7月  28 04:58 file20.txt

属主:user11 属组:mm组员user22)

6.测试
使用user11,访问文件。写入文件,执行文件

[root@centos tmp]# su - user100
[user11@centos ~]$ cd /tmp/
[user11@centos tmp]$ cat file20.txt 
[user11@centos tmp]$ vim file20txt 
[user11@centos tmp]$ ./file20.txt 
123
456
789
[user11@centos tmp]$ exit

登出
使用mm成员,访问文件,不可写和执行

[root@centos tmp]# su - user222
[user22@centos ~]$ cd /tmp/
[user22@centos tmp]$ cat file20.txt 
[user22@centos tmp]$ vim file20.txt 
写入失败,强制退出:q!
[user22@centos tmp]$ ./file20.txt
-bash: ./file20.txt: 权限不够

使用其他用户user33,访问文件失败。写入失败,执行失败。

[root@centos tmp]# su - user33
[user300@centos ~]$ cd /tmp/
[user33@centos tmp]$ cat file20txt 
cat: file20.txt: 权限不够
[user33@centos tmp]$ vim file20.txt 
写入失败,强制退出:q!
[user33centos tmp]$ ./file20.txt
-bash: ./file20.txt: 权限不够
[user33@centos tmp]$ exit

登出

二.基本权限 ACL

ACL和UGO区别
ACL文件权限管理: 设置不同用户,不同的基本权限(r、w、x)。对象数量不同。
UGO设置基本权限: 只能一个用户,一个组和其他人
ACL语法:setfacl -m u:user01:rw /home/test.txt
命令 设置 用户或组:用户名:权限 文件对象

用法:

1.准备文件/home/test.txt 准备用户a1 和用户b1两个用户。

[root@centos ~]# touch /home/test.txt     //
[root@centos ~]# useradd a1
[root@centos ~]# useradd b1

2. 查看文件有哪些ACL权限。

[root@centos ~]# getfacl  /home/test.txt
getfacl: Removing leading '/' from absolute path names
file: home/test.txt
owner: root
group: root
user::rw-
group::r--
other::r--

owner: root 属主:root
group: root 属组:root
user::rw 用户:属主:rw
group::r 组:属组:r
other::r other:其他人:r

3.设置用户a1,b1权限

[root@centos ~]# setfacl -m u:a1:rw /home/test.txt   //设置用户a1权限
[root@centos ~]# setfacl -m u:b1:-  /home/test.txt    //设置用户b1权限
[root@centos ~]# setfacl -m o::rw /home/test.txt     //设置其他用户other权限
[root@centos ~]# getfacl /home/test.txt 
getfacl: Removing leading '/' from absolute path names
powershell
#file: home/test.txt
#owner: root
#group: root
user::rw-
user:a1:rw-
user:b1:---
group::r--
mask::rw-
other::rw-

file: home/test.txt 文件名
owner: root 属主:root
group: root 属组:root
user::rw 用户:属主:rw
user:a1:rw- 用户:a1:rw-
user:b1:— 用户:b1:—
group::r-- 组:属组:r–
mask::rw- 掩码::rw-
other::rw- other:其他人:rw-

设置组的权限:增加hr组对test.txt文件读取的权限
首先创建组hr:[root@centos ~]# groupadd hr
增加hr组对test.txt文件读取的权限:
[root@centos ~]# setfacl -m g:hr:r /home/test.txt //增加hr组对test.txt文件读取的权限:

[root@centos ~]# getfacl /home/test.txt 
getfacl: Removing leading '/' from absolute path names
# file: home/test.txt
# owner: root
# group: root
user::rw-
user:a1:rw-
user:b1:---
group::r--
group:hr:r--              增加了hr组对test.txt文件读取的权限
mask::rw-
other::rw-

删除ACL
删除部分:删除组hr的读取的权限
[root@centos ~]# setfacl -x g:hr /home/test.txt //删除组hr的读取的权限

[root@centos ~]# getfacl /home/test.txt 
getfacl: Removing leading '/' from absolute path names
# file: home/test.txt
# owner: root
# group: root
user::rw-
user:a1:rw-
user:b1:---
group::r--             hr没有了读取的权限
mask::rw-
other::rw-

删除所有:
[root@centos ~]# setfacl -b /home/test.txt //删出以上创建的所有用户和组的权限

[root@centos ~]# getfacl /home/test.txt
getfacl: Removing leading '/' from absolute path names
# file: home/test.txt
# owner: root
# group: root
user::rw-
group::r--
other::rw-

三.特殊权限

特殊位 suid:高级权限的类型,suid针对文件/程序时,具备临时获得属主的权限。
设置suid,使普通用户通过suid临时提权,查看超管root用户的文件

1.为cat程序添加上suid权限。

[root@centos ~]# chmod u+s /usr/bin/cat     //给文件设置suid
[root@centos ~]# ll /usr/bin/cat
-rwsr-xr-x. 1 root root 54080 8月  20 2019 /usr/bin/cat   

2.使用普通用户运行cat。暂时获得root权限

[root@centos ~]# su - a1
上一次登录:三 7月 29 02:42:10 CST 2020pts/3 上   
[a1@centos ~]$ cat /root/file1.txt
r123345
23432                 //,普通用户,看到了root的内容
3254

这个行为很危险,请在试验后,将cat的suid权限除去。(要切换到到root)

[root@centos ~]# chmod u-s /usr/bin/cat
[root@centos ~]# ll /usr/bin/cat
-rwxr-xr-x. 1 root root 54080 8月  20 2019 /usr/bin/cat

文件属性chattr
用途:常用于锁定某个文件,拒绝修改。
案例:
1 先创建新文件进行对比。查看默认权限。

[root@centos ~]# touch file100
[root@centos ~]# lsattr file100
---------------- file100

2 加上不能删除的属性(i

[root@centos ~]# chattr +i file100
[root@centos ~]# lsattr file100

----i----------- file100 //i不能更改,重命名,删除

  1. 尝试删除
[root@centos ~]# rm -rf file100

rm: 无法删除"file100": 不允许的操作

4 将属性还原。

[root@centos ~]# chattr -i file100

再次尝试删除

[root@centos ~]# rm -rf file100
[root@centos ~]# cat file100
cat: file100: 没有那个文件或目录    //已经删除了文件file100

进程掩码 umask
新建文件、目录的默认权限会受到umask的影响,umask表示要减掉的权限
示例:观察系统默认掩码 ,在shell进程中创建文件,先查看当前用户的umask权限

[root@centos ~]# umask
0022   

//系统原来的进程掩码

[root@centos ~]# touch file111
[root@centos ~]# mkdir dir111
[root@centos ~]# ll -d dir111 file111
drwxr-xr-x. 2 root root 6 7月  29 03:23 dir111     
-rw-r--r--. 1 root root 0 7月  29 03:23 file111

修改shell umask值(临时)

[root@centos ~]# umask 000
[root@centos ~]# mkdir 99
[root@centos ~]# touch file99
[root@centos ~]# ll -d 99 file99
drwxrwxrwx. 2 root root 6 7月  29 03:26 99
-rw-rw-rw-. 1 root root 0 7月  29 03:26 file99
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值