Linux用户权限详解

为什么有人冲了钱就能享受至尊VIP待遇?为什么冲了黄钻、绿钻、紫钻就会享受一些特殊活动呢?我们起初都是一群普通用户,为什么有些人就能通过某些手段得到一些异于常人的服务呢?这其中的奥秘是什么呢?接下来带大家了解这其中的原因。

系统的权限无非就是对文件进行增删改查,在电脑中,万物皆是文件,在Linux中,我们对文件操作又是怎么分配权限的呢?

1.权限的三类对象UGO

  1. 属主:u
  2. 属组:g
  3. 其他人:o
  4. 特殊对象:a(所有人  u+g+o)

 2.权限的三种类型

  1. 读: r = 4
  2. 写: w = 2
  3. 执行:x =1

 3.基本结构

[root@iZ2zef4rb5ixg6sieztgsdZ ~]# cd /tmp
[root@iZ2zef4rb5ixg6sieztgsdZ tmp]# ll
total 12
-rw------- 1 root root    0 Dec 20 20:30 AliyunAssistClientSingleLock.lock
drwxr-x--- 2 root root 4096 Dec 23 21:39 hsperfdata_root
drwx------ 3 root root 4096 Dec 23 17:41 systemd-private-e6ce977725fd412796e12d9e9217783a-chronyd.service-wy9Tvg
drwx------ 3 root root 4096 Dec 23 17:41 systemd-private-e6ce977725fd412796e12d9e9217783a-systemd-resolved.service-P1MToi

 

  •  语法一:chmod  对象加减权限  文件或者目录
    [root@iZ2zef4rb5ixg6sieztgsdZ tmp]# mkdir filedir   //创建一个文件夹
    [root@iZ2zef4rb5ixg6sieztgsdZ tmp]# ll
    drwxr-xr-x 2 root root 4096 Dec 28 16:25 filedir      //该文件支持读写运行
    [root@iZ2zef4rb5ixg6sieztgsdZ tmp]# chmod u=rw filedir  //将属主的权限改为读写
    //[root@iZ2zef4rb5ixg6sieztgsdZ tmp]# chmod u=6 filedir  //将属主的权限改为读写,效果一致
    [root@iZ2zef4rb5ixg6sieztgsdZ tmp]# ll
    drw-r-xr-x 2 root root 4096 Dec 28 16:25 filedir
    [root@iZ2zef4rb5ixg6sieztgsdZ tmp]# chmod g=  filedir  //将属组的权限取消
    //[root@iZ2zef4rb5ixg6sieztgsdZ tmp]# chmod g=0  filedir
    [root@iZ2zef4rb5ixg6sieztgsdZ tmp]# ll
    drw----r-x 2 root root 4096 Dec 28 16:25 filedir
    
    [root@iZ2zef4rb5ixg6sieztgsdZ tmp]# chmod o=wx filedir   //将其他人的权限改为写 执行
    //[root@iZ2zef4rb5ixg6sieztgsdZ tmp]# chmod o=3 filedir
    [root@iZ2zef4rb5ixg6sieztgsdZ tmp]# ll
    drw-----wx 2 root root 4096 Dec 28 16:25 filedir
    
  • 语法二:chown 用户名.组名  文件名  改变属主、属组(只改变文件夹上的权限不包括包含的内容)

    [root@iZ2zef4rb5ixg6sieztgsdZ tmp]# chown user02.user02 filedir //改变属主,属组
    [root@iZ2zef4rb5ixg6sieztgsdZ tmp]# ll
    drw-----wx 2 user02 user02 4096 Dec 28 16:25 filedir
    
    [root@iZ2zef4rb5ixg6sieztgsdZ tmp]# chown .user01 filedir//改变属组
    [root@iZ2zef4rb5ixg6sieztgsdZ tmp]# ll
    drw-----wx 2 user02 user01 4096 Dec 28 16:25 filedir
    
    [root@iZ2zef4rb5ixg6sieztgsdZ tmp]# chown user01 filedir //改变属主
    [root@iZ2zef4rb5ixg6sieztgsdZ tmp]# ll
    drw-----wx 2 user01 user01 4096 Dec 28 16:25 filedir
    
  • 语法二:chown  -R 用户名.组名  文件名  改变属主、属组(包含文件中的所有内容) -R是递归的意思是目录下的文件统一进行改变

    [root@iZ2zef4rb5ixg6sieztgsdZ tmp]# chown -R user02.user02 filedir
    [root@iZ2zef4rb5ixg6sieztgsdZ tmp]# ll
    drw-----wx 2 user02 user02 4096 Dec 28 17:02 filedir
    [root@iZ2zef4rb5ixg6sieztgsdZ tmp]# cd filedir
    [root@iZ2zef4rb5ixg6sieztgsdZ filedir]# ll
    total 0
    -rw-r--r-- 1 user02 user02 0 Dec 28 17:02 file1.txt
    
  • 语法三:setfacl  -m  对象:对象名:权限  文件名   设置文件访控

    [root@iZ2zef4rb5ixg6sieztgsdZ tmp]# setfacl -m g:user02:wx filedir
    [root@iZ2zef4rb5ixg6sieztgsdZ tmp]# ll
    drw--wx-wx+ 2 user02 user02 4096 Dec 28 17:02 filedir
    
    [root@iZ2zef4rb5ixg6sieztgsdZ tmp]# getfacl filedir  //查看文件中的ACL权限
    # file: filedir
    # owner: user02
    # group: user02
    user::rw-
    group::---
    group:user02:-wx  //添加新的访控名单
    mask::-wx
    other::-wx
    
  • 语法四:setfacl  -x  对象:对象名:权限  文件名   删除文件访控

    [root@iZ2zef4rb5ixg6sieztgsdZ tmp]# setfacl -m g:user02:wx filedir
    [root@iZ2zef4rb5ixg6sieztgsdZ tmp]# setfacl -x g:user02 filedir
    [root@iZ2zef4rb5ixg6sieztgsdZ tmp]# getfacl filedir
    # file: filedir
    # owner: user02
    # group: user02
    user::rw-
    group::---
    mask::---
    other::-wx
    

4.suid 

sudi是针对文件所设置的一个特别的权限

功能:使调用文件的用户临时具备属主的能力

[root@iZ2zef4rb5ixg6sieztgsdZ ~]# cat /root/demo.txt
aaaa
ffff
cccc
dddd
[root@iZ2zef4rb5ixg6sieztgsdZ ~]# su user02
[user02@iZ2zef4rb5ixg6sieztgsdZ root]$ cat /root/demo.txt
cat: /root/demo.txt: Permission denied  //没有权限
[root@iZ2zef4rb5ixg6sieztgsdZ ~]# ll /usr/bin/cat
-rwxr-xr-x 1 root root 46432 Jan 24  2022 /usr/bin/cat
[root@iZ2zef4rb5ixg6sieztgsdZ ~]# chmod u+s /usr/bin/cat //将cat设为有临时提权的能力
[root@iZ2zef4rb5ixg6sieztgsdZ ~]# lln/usr/bin/cat
bash: lln/usr/bin/cat: No such file or directory
[root@iZ2zef4rb5ixg6sieztgsdZ ~]# ll /usr/bin/cat
-rwsr-xr-x 1 root root 46432 Jan 24  2022 /usr/bin/cat
[root@iZ2zef4rb5ixg6sieztgsdZ ~]# su user02
[user02@iZ2zef4rb5ixg6sieztgsdZ root]$ cat /root/demo.txt   //user02用户也能查看到root文件下的txt文件
aaaa
ffff
cccc
dddd

5.chattr

a(append)允许在文件中进行追加操作
A这个属性不允许更新文件的访问时间
c(compressed)启动这个属性时,文件在磁盘上自动压缩
d(dump)不能使用dump命令备份文件
D设置了文件夹中的D属性时,更改会在同步保存在磁盘上
e(extent format)该文件使用磁盘上的块映射文件扩展
i(immutable)该文件使用磁盘时,我们不能更改、重命名、删除这个文件

6.规则

我们创建一个文件夹,权限永远是755,创建一个文件,权限永远是644,其实在系统中已经有创建文件夹以及文件的规则,都是为了保护文件而存在的,我们也可以修改规则(谁让我们是超级管理员呢)例如:umask  0000

[root@iZ2zef4rb5ixg6sieztgsdZ tmp]# mkdir 111
[root@iZ2zef4rb5ixg6sieztgsdZ tmp]# ll
total 16
drwxr-xr-x 2 root root 4096 Dec 28 18:10 111
[root@iZ2zef4rb5ixg6sieztgsdZ tmp]# touch 222
[root@iZ2zef4rb5ixg6sieztgsdZ tmp]# ll
total 16
drwxr-xr-x 2 root root 4096 Dec 28 18:10 111
-rw-r--r-- 1 root root    0 Dec 28 18:12 222
[root@iZ2zef4rb5ixg6sieztgsdZ ~]# umask
0022

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吃橘子的Crow

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值