<VI>权限管理


一、权限查看及读取

1.权限查看

[root@westos_student3 ~]# ls -l file    #查看文件file的权限
[root@westos_student3 ~]# ls -ld dir    #查看目录dir的权限

2.权限读取

文件的属性被叫做文件的元数据(meta data),一种元数据用 1 个 byte 来记录内容。

3.文件及目录权限读取

1)文件权限信息# - | rw-r–r-- | . | 1 | root | root | 0 | Apr 12 10:57 | westos
            [1]   [2]   [3] [4] [5]   [6]  [7]    [8]       [9]
2)目录权限信息# d | rw-r–r-- | . | 2 | root | root | 0 | Apr 12 10:57 | westosdir
            [1]   [2]   [3] [4] [5]   [6]  [7]    [8]       [9]

4.权限信息各位的解释

[1]文件的类型:
          -表示普通文件;
          d表示目录
          l表示软链接(快捷方式)
          b表示块设备(dev里只有sda)
          c字符设备
          s socket套接字(进入程序的一个接口)
          p 管道
[2] 用户权限:
          例如:rw-r–r-- ,rw-为用户权限(u);r–为用户组权限(g);r–为其他用户权限(o)
            u  g o
[3] 系统的 selinux 开启( .点是安全上下文的内容)
[4] 对于文件:文件内容被系统记录的次数(硬链接个数)
  对于目录:目录中子目录的个数

[5] 文件拥有者
[6] 文件拥有组
[7] 对于文件:文件内容大小
  对于目录:目录中子文件的元数据大小
[8] 文件内容被修改的时间(属性或内容等被修改时都会变)
[9] 文件名称

二、普通权限的类型及作用

1.用户对文件的身份

权限身份
uuser 文件的拥有者
ggroup 文件拥有组
oother 既不是拥有者也不是拥有组成员的其他用户的通称

2.普通权限的类型

权限类型解释
-权限未开启
r可读:对于文件:可以读取文件内容;对于目录:可以 ls 列出目录中的文件
w可写:对于文件:可以更改文件内容;对于目录:可以在目录中新建或者删除文件
x可执行:对于文件:可以用文件名称调用文件内记录的程序;对于目录:可以进入目录中

3.用户身份匹配

user>group>other

三、文件用户用户组管理

1.监控界面

[root@westos_student3 ~]# watch -n 1 'ls -Rl /mnt/ '  #监控/mnt目录下所有的文件及目录的权限

在这里插入图片描述

2.chown、chgrp操作界面

[root@westos_student3 ~]# chown lee file  #更改文件拥有者
[root@westos_student3 ~]# chgrp kiosk file #更改文件拥有组
[root@westos_student3 ~]# chown lee,kiosk file #同时更改文件的拥有者和拥有组
[root@westos_student3 ~]# chown lee:kiosk file #同时更改文件的拥有者和拥有组

操作效果图如下:
在这里插入图片描述

[root@westos_student3 ~]# chown -R lee dir  #更改目录本身及目录中文件的拥有者
[root@westos_student3 ~]# chgrp -R kiosk dir  #更改目录本身及目录中文件的者拥有组

在这里插入图片描述

四、设定普通权限的方法

1.监控界面

[root@westos_student3 ~]# watch -n 1 'ls -Rl /mnt/ '  #监控/mnt目录下所有的文件及目录的权限

2.chmod操作界面

1)方式1:加减操作

[root@westos_student3 ~]# chmod u+rw file  #把u的权限减去rw
[root@westos_student3 ~]# chmod u-r,g+rw,o+xr file #加减操作;把u,g,o的权限分别-r,+rw,+xr

在这里插入图片描述2)方式:等于操作

[root@westos_student3 ~]# chmod u=rwx,g=rxw,o=rxw  file  #把文件file的u,g,o权限分别都设为rwx
[root@westos_student3 ~]# chmod ugo-rwx file   #把文件file的u、g、o都减去rwx
[root@westos_student3 ~]# chmod -R ugo-rwx dir   #把目录及目录里的文件的u、g、o都减去rwx

在这里插入图片描述

3)方式3:复制操作

[root@westos_student3 ~]# chmod --reference=file1  file2  #把file1的权限复制给file2;即把文件file2的u,g,o权限变得跟file1一样

在这里插入图片描述4)方式4:指定数字方式

[root@westos_student3 ~]# chmod  777  file1   #把file1的权限设置成777.即u=rwx,g=rwx,o=rwx
[root@westos_student3 ~]# chmod  500  file1   #把file1的权限设置成500.即u=r-x,g=---,o=--
[root@westos_student3 ~]# chmod  -R  770 dir1   #把目录dir1和目录中的文件的权限设置成770.即u=rwx,g=rwx,o=---

在这里插入图片描述
注意:权限波尔指表示方式:rwx = 111;— = 000
             即r=4,w=2,x=1;
例如:
三位二进制可以表示的最大范围为 8 进至数:
                  rwx=111=7
                  rw-=110=6
                  r-x=101=5
                  r–=100=4=r
                  -wx=011=3
                  -w-=010=2=w
                  --x=001=1=x
                  —=000=0

五、系统默认权限设定

系统本身存在共享资源,从安全角度讲系统共享的资源越少,开放的权力越小系统安全性越高,既要保证系统安全,又要系统创造价值,于是把应该开放的权力默认开放,把不安全的权力默认保留。

1)如何保留权力

umask 表示系统保留权力
文件默认权限 = 777-umask-111
目录默认权限 = 777-umask
umask 值越大系统安全性越高

2)临时更改umask

[root@westos_student3 ~]# umask     #查看保留权力
[root@westos_student3 ~]# umask 023     #临时设定系统预留权力

在这里插入图片描述

2)永久更改umask
第一步:编辑shell系统配置文件,搜索umask,修改umask值
vim /etc/bashrc      #shell 系统配置文件
74 if [ $UID -gt 199 ] && [ “id -gn” = “id -un” ]; then
75 umask 002–>022    #普通用户的 umask
76 else
77 umask 022      #root 用户的 umask
78 fi

在这里插入图片描述

在这里插入图片描述

第二步:编辑系统环境配置文件,搜索umask,修改umask值
vim /etc/profile      #系统环境配置文件
59 if [ $UID -gt 199 ] && [ “id -gn” = “id -un” ]; then
60 umask 002–>022    #普通用户的 umask
61 else
62 umask 022     &emsp#root 用户的 umask
63 fi

在这里插入图片描述在这里插入图片描述

第三步:使更改的内容立即被系统识别
source /etc/bashrc ##source 作用时使我们更改的内容立即被系统识别
source /etc/profile

在这里插入图片描述

六、特殊权限

1.stickyid 粘制位

作用:针对目录: #如果一个目录 stickyid 开启,那么这个目录中的文件只能被文件所有人删除

[root@westos_student3 ~]# chmod 1777 dir  #777是目录原始权限,或chmod o+t dir

在这里插入图片描述

2.sgid 强制位

作用:
1)针对目录: 目录中新建的文件自动归属到目录的所属组中

[root@westos_student3 ~]# chmod 2777 dir  #777是目录原始权限,或chmod g+s dir  

在这里插入图片描述

2)只针对二进制的可执行文件(c 程序),当运行二进制可执行文件时都是用文件拥有组身份运行,和执行用户无关

[root@westos_student3 ~]# chmod 2777 dir  #777是目录原始权限,或chmod g+s dir  
[root@westos_student3 ~]# ps ax -o user,group,command | grep cat #查看进程/bin/cat的user,group,command;grep cat是指过滤cat
[root@westos_student3 ~]# ls -l /bin/cat  #查看进程文件的权限

另开一个shell

[kiosk@foundation34 Desktop]$ /bin/cat  #cat进程

在这里插入图片描述

3.suid 冒险位

作用:当运行二进制可执行文件时都是用文件拥有者身份运行,和执行用户无关,(只针对二进制的可执行文件(c 程序)

[root@westos_student3 ~]# chmod 4777 file  #777是目录原始权限,或,chmod u+s file 
[root@westos_student3 ~]# ps ax -o user,group,command | grep cat #查看进程/bin/cat的user,group,command;grep cat是指过滤cat
[root@westos_student3 ~]# chmod u+s file    #同时设置用户和用户组

在这里插入图片描述

七、acl 权限列表

1)功能:在列表中可以设定特殊用户对与特殊文件有特殊权限

[root@westos_student3 ~]# Aiccess Control Lists #访问控制列表

2)acl 列表开启标识
-rw-rw---- 1 root caiwu 0 Apr 18 09:03 westosfile
    ^
没有"+"代表 acl 列表未开启
-rw-rw----+ 1 root caiwu 0 Apr 18 09:03 westosfile
    ^
acl 列表功能开启
3)acl 列表权限读取及显示内容

[root@westos_student3 ~]# getfacl file  #acl 列表权限读取
[root@westos_student3 ~]# setfacl -m u:kiosk:rw /mnt/file1 #设定特殊用户对与特殊文件有特殊权限为rw
[root@westos_student3 ~]# setfacl -m g:kiosk:rw /mnt/file1 #设定特殊用户组对与特殊文件有特殊权限为rw
[root@westos_student3 ~]# setfacl -m u::0 file   #设置用户本身的u:---
[root@westos_student3 ~]# setfacl -m g::0 file   #设置用户本身的g:---
[root@westos_student3 ~]# setfacl -b file           #关闭

在这里插入图片描述

本身u被改变

在这里插入图片描述

[root@westos_student3 ~]# chmod g-x  dir    #改变用户组权限
[root@westos_student3 ~]# setfacl -m m:rwx   #恢复权限为rwx

恢复
在这里插入图片描述

[root@westos_student3 ~]# setfacl -m u:lee:rwx /mnt/dir               #只对于/mnt/dir 目录本身生效
[root@westos_student3 ~]# setfacl -Rm u:lee:rwx /mnt/dir              #对于/mnt/dir 目录和目录中已经存在的内容生效
[root@westos_student3 ~]# setfacl -m d:u:lee:rwx /mnt/dir/      #针对与/mnt/dir 目录中新建文件生效

在这里插入图片描述

八、attr 权限

[root@westos_student3 ~]# chattr +a ts/       #ts目录能添加不能删除,且attr 权限限制所有用户
[root@westos_student3 mnt]# lsattr -d ts/     #查看 attr 权限
[root@westos_student3 mnt]# chattr +i ts/     #ts目录不能作任何的更改

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值