Linux系统中的权限管理

Linux系统中的权限管理

一、权限查看及读取

1.权限查看

ls-l file查看文件权限
ls -ld dir查看目录权限

2.权限的读取
文件的属性被叫做文件的元数据(meta data)
一种元数据用一个byte来记录内容
注:文件的权限信息如下所示共有九种,除了第九位的文件名称之外,剩下八种每种都是一个元数据,每个元数据用一个字节记录,即八个字节。

文件权限信息:
-| rw-r–r-- | ’ | 1 | root | root | 0 | July 17 10:57 | westos
【1】 【2】 【3】【4】【5】 【6】 【7】 【8】 【9】

目录权限信息:
d | rw-r–r-- | ’ | 1 | root | root | 0 | July 17 10:57 | westos
【1】 【2】 【3】【4】【5】 【6】 【7】 【8】 【9】

对于每一位的解释
【1】文件类型
(1)- 普通文件
(2)d 目录
(3)l 软连接
(4)d 快设备
(5)c 字符设备
(6)s socket套接字
(7)p 管道
【2】用户权限

1rwx | r- - | r- -
2u    g    o

【3】系统的selinux开启
【4】对于文件:文件内容被系统记录的次数(硬链接个数);对于目录:目录中子目录的个数
【5】文件拥有者
【6】文件拥有组
【7】对于文件:文件内容大小;对于目录:目录中子文件元数据的大小
【8】文件内容被修改的时间
【9】文件名称

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

1.用户对文件的身份

uuser文件的拥有者,ls -l 看到的第五列信息
ggroup文件拥有者,ls -l 看到的第六列信息
oother 既不是拥有者也不是拥有组成员的其他用户的通称

2.权限位

rwx| r-- | r–
u     g    o

3.用户身份匹配
user > group > other
4.权限类型
【1】- :权限未开启
【2】r:可读
对于文件:可以读取文件内容
对于目录:可以ls 列出木中的文件
【3】w:可写
对于文件:可以更改文件内容
对于目录:可以在目录中新建或者删除文件
【4】x:可执行
对于文件:可以用文件名称调用文件内记录的程序

对于目录:可以进入目录中
用户涉及到的系统配置文件
/etc/passwd        ##用户身份信息文件
             ##用户名称:用户密码:用户id:用户主组id:用户说明:用户家目录:用户默认shell
/etc/group         ##组身份信息文件
            ##组名称:组密码:组id:组的附加成员
/etc/skel/.*        ##用户环境配置文件模板
/etc/shadow        ##用户认证信息文件
/home/username      ##用户家目录

三、设定普通权限的方法

【1】

chmod设定文件权限
chmod 复制权限 chmod --reference=/tmp /mnt/westosdir复制/tmp目录权限到/mnt /westosdir上
chmod -R --reference=/tmp /mnt/复制/tmp目录的权限到/mnt /westosdir及目录中的子文件上, -R代表递归操作

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

chmod 字符方式设定权限
chmod <a|u|g|o><+|-|=><r|w|x> file 用字符方式设定文件权限
eg:

1|[root@localhost mnt]# chmod u-rw /mnt/westos1
2 [root@localhost mnt]# chmod u-rw /mnt/westosfile1
3 [root@localhost mnt]# chmod u-rw,g+x,o+wx. /mnt/westosfile2
4 [root@localhost mnt]# chmod a-rwx /mnt/westosfile3
5 [root@localhost mnt]# chmod u=rwx,g=rx,o=--- /mnt/westosfile4
6 [root@localhost mnt]# chmod -R u=rwx, g=rx,o=--- /mnt/westosdir/

监控文件的权限和组权限
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

监控目录的权限和组权限
chmod 数字方式设定权限(最常用的模式)
权限波尔值表示方式
(1)rwx = 111
(2)- - - = 000
(3)三位二进制可以表示的最大范围为8进制数

1     rwx=111=7
2     rw-=110=6
3     r-x=101=5
4     r--=100=4=r
5     -wx=011=3
6     -w-010=2=w
7     --x=001=1=x
8     ---=000=0

在这里插入图片描述

4.系统默认权限设定

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

umask查看保留权利
umask 权限值临时设定系统预留权利

文件默认权限=777-umask-111
目录默认权限=777-umask

umask值越大系统安全性越高
【2】umask临时更改
umask 077
【3】永久更改

[root@node1 mnt]# vim /etc/bashrc     shell系统配置文件

74 if [ $UID -gt 199 ] && [ "id -gn = " id -un " ]; then
75     umask 002-->o22     普通用户umask
76 else
77     umask 022           root用户的umask
78fi
            
[root@node1 mnt]# vim /rtc/profile     系统环境配置文件

59 if [ $UID -gt 199 ] && [ " id -gn " = "id -un " ]; then 
60      umask 002-->022     普通用户的umask
61 else
62      umask o22           root用户的umask
63  fi      

[root@node1 mnt]# source /etc/bashrc          source作用是使我们更改的内容立即被识别
[root@node1 mnt]# source /etc/profile

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

5.文件用户用户组管理

chownusername file更改文件拥有者
chgrp groupname file更改文件拥有组
chown username:groupname file同时更改文件的拥有者和拥有组
chown|chgrp -R user group dir更改目录本身及目录中内容的拥有者或拥有组

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

chown -R admin /mnt/westdir                  更改目录本身及目录中内容的拥有者或拥有组

6.特殊权限

【1】stickyid 粘制位

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

设定方式:
(1)chmod 1原始权限 dir
(2)chmod o+t
在这里插入图片描述

1[root@node1 mnt]# mkdir /pub
2[root@node1 mnt]# chmod 777 /pub
3[xy@node1 mnt]$ su - xy
4[xy@node1 mnt]$ touch /pub/westosfile
5[root@node1 mnt]# exit
6[root@node1 mnt]# su - lee
7[lee@node1 mnt]$ touch /pub/leefile
8[lee@node1 mnt]$ rm -fr /pub/leefile   可以删除
9[lee@node1 mnt]$ rm -fr /pub/westosfile 不属于自己的文件也可以删除

在这里插入图片描述

如何解决此问题:
[root@node1 mnt]# chmod 1777 /pub
[root@node1 mnt]# chmod o+t /pub
以上两条命令都可以开启pub目录的t权限
[xy@node1 mnt]$ su - xy
[xy@node1 mnt]$ touch /pub/westosfile
[root@node1 mnt]# exit
[root@node1 mnt]# su - lee
[lee@node1 mnt]$ touch /pub/leefile
[lee@node1 mnt]$ rm -fr /pub/leefile   可以删除
[lee@node1 mnt]$ rm -fr /pub/westosfile  不属于自己的文件不能删除
rm:cannot remove 'westosfile' : Operation not permitted

在这里插入图片描述

【2】sgid 强制位

针对目录:目录中新建的文件自动归属到目录的所属组中
设定:
chmd 2原文件权限 dir
chmod g+s dir

实验:
对于文件:

1[root@node1 Desktop]# group westostsxt
2[root@node1 Desktop]# mkdir /mnt/westosdir
3[root@node1 Desktop]# chmod 777 /mnt/westosdir
4[root@node1 Desktop]# chgrp westostext /mnt/westosdir
5[root@node1 Desktop]# watch -n1 ls -lR /mnt
6[root@node1 Desktop]# touch /mnt/westosdir/file (root)   是谁建立的文件组就是谁的
7[root@node1 Desktop]# chmod g+s /mnt/westosdir
[8root@node1 Desktop]# touch /mnt/westosdir/file1(root)   file1自动复制了/mnt/westosdir目录组

在这里插入图片描述

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

9[root@node1 Desktop]# su - xy
10[xy@node1 ~]$ /bin/watch -n 1 date

11[root@node1 Desktop]# pa ax -o user,group,comm | grep watch
xy   xy    watch

用root用户身份:
12[root@node1 Desktop]# chmod g+s /bin/watch
13[root@node1 Desktop]# su - xy
14[xy@node1 ~]$ /bin/watch -n 1 date

15[root@node1 Desktop]# ps ax -o user,group,comm | grep watch
xy   root   watch

【3】suid 冒险位

只针对二进制的可执行文件(c程序)
当运行二进制可执行文件时都是用文件拥有者身份运行,和执行用户无关
设定:
chmod 4原属性 file
chmod u+s file
实验:

[root@node1 Desktop]# su - xy
[xy@node1 ~]$ /bin/watch -n 1 date
[root@node1 Desktop]# pa ax -o user,group,comm | grep watch
xy   xy    watch

用root用户身份:

[root@node1 Desktop]# chmod u+s /bin/watch
[root@node1 Desktop]# su - xy
[xy@node1 ~]$ /bin/watch -n 1 date
[root@node1 Desktop]# ps ax -o user,group,comm | grep watch
root   xy    watch

7.acl 权限列表

Aiccess Control Lists 访问控制列表

【1】功能:在列表中可以设定特殊用户与特殊文件有特殊权限
【2】acl 列表开启标识

-rw-rw---- 1 root caiiwu 0 Apr 18 09:03 westosfile
          ^
       没有“+”代表acl列表未开启
  
 rw-rw----+1 root caiiwu 0 Apr 18 09:03 westosfile  
          ^
       acl列表功能开启

在这里插入图片描述

【3】acl列表权限读取
getfacl westosfile
在这里插入图片描述

【4】显示内容分析

file:westosfile文件名称
owner:root文件拥有者
group:root文件拥有组
user::rw-文件拥有者权限
user:lee:rw-特殊指定用户权限
group::r- -文件拥有组权限
group:westos:- - -特殊指定的用户组的权限
mask::rw-能够赋予特殊用户和特殊用户组的最大权限阈值
other::r- -其他人权限
注意:当文件权限列表开启,不要用ls -l的方式来读取文件的权限

【5】acl列表的控制

acl列表的设定:
(1)setfacl -m u:lee:rw westosfile
(2)setfacl -m g:westos:rw wesrosfile
(3)setfacl -m u::rwx westosfile
(4)setfacl -m g::0 westosfile
删除列表中的lee
setfacl -x u:lee westosfile
关闭
setfacl -b westosfile

【6】acl权限优先级
拥有者 > 特殊指定用户 > 权限多的组 > 权限少的组 > 其他
【7】acl mask控制

mask是能够赋予指定用户权限的最大阀值

问题:
当设定完i毕文件的acl列表之后用chmod 缩小了文件拥有组的权利mask会发生变化

恢复:
setfacl -m m:权限 文件

在这里插入图片描述

【8】acl列表大的默认权限

setfacl -m u:lee:rwx /mnt/westosdir只对于/mnt/westosdir目录本身生效
setfacl -Rm u:lee:rwx /mnt/westosdir对于/mnt/westosdir目录和目录中已存在的内容生效

以上的命令只针对已存在在的文件生效,新建的文件是不会被设定的

setfacl - d:u:lee:rwx /mnt/wstosdir/只对与/mnt/westosdir目录中新建的文件生效-

8、attr权限

attr权限限制所有用户

i不能做任何更改
a能添加不能删除

lsattr dir |file 查看attr权限
chattr +i |+a | -i | -a  dir | file 设定attr权限

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值