5 Liunx系统中的权限管理

一 权限的查看及读取

1 权限查看

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

2 权限的读取注意:
"当文件权限列表开启,不要用ls -l 的方式来读取文件的权限"

"文件的属性被叫做文件的元数据(meta date)":为描述数据的数据
"一种元数据用1个byte来记录内容"

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

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

#对于每一位的解释#
[1]         #文件类型
            # - 普通文件
            # d 目录
            # l软连接 (ln -s westos testfile)
            # b 快设备 (硬盘设备)
            # c 字符设备
            # s socket套接字
            # p 管道 |

[2]         ##用户权限
            ## rw-|r--|r--
            # user group other
                   组成员
[3]         ##系统的selinux开启
            ## . 表示文件的安全上下文
            
[4]         ##对于文件: 1 文件内容被系统记录的次数(硬链接个数)文件要被删多少次才能被删掉
            ##对于目录:目录中子目录的个数

[5]         ##文件拥有者 谁的
                                  (拥有者优先级高于拥有组)
[6]         ##文件拥有组 哪个组的

[7]         ##对于文件:文件内容大小
            ##对于目录:目录中子文件的元数据大小

[8]         ##文件内容最后一次被修改的时间

[9]         ##文件名称

二 普通权限的类型及作用

#1 用户对文件的身份#


u:          #user 文件的拥有者,ls -l 看到的第五列信息
g:          #group 文件拥有组, ls -l 看到的第六列信息
o:          #other 既不是拥有者也不是拥有组成员的其他用户的通称
a:          #all所有人

#2 权限位#


rwx|r--|r--
 u   g   o

#3 用户身份匹配#


user>group>other

#4 权限类型#


-           #权限未开启

r           #可读
            #对于文件:可以读取文件内容
            #对于目录:可以ls列出目录中的文件
            
w           #可写
            #对于文件:可以更改文件内容
            #对于目录:可以在目录中新建或者删除文件
 
'x           #可执行
            #对于文件:'可以用文件名称调用文件内记录的程序(显示文件内的命令)
            #对于目录:'可以进入目录中 (可以切换到目录中)

三 设定普通权限的方法

chmod              ##设定文件权限

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

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

示例:
chmod u-rw /mnt/westos1
chmod u-rw /mnt/westosfile1
chmod u-rw,g+x,o+x,o+wx /mnt/westosfile2
chmod a-rwx /mnt/westosfile2
chmod u=rwx,g=rx,o=--- /mnt/westosdir/

#chomd 数学方式设定权限#
权限波尔只表示方式
rwx = 111
--- = 000

三位二进制可以表示的最大范围为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

chmod    600   /mnt/westosfile1
       rw--------

四 系统默认权限设定

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

#如何保留权力#

# umask表示系统保留权力
umask                #查看保留权力
umask 权限值          #临时设定系统预留权力

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

umask值越大系统安全性越高

#umask临时更改
umask 077

#永久更改
vim /etc/profile       ##系统环境下的配置文件

vim /etc/bashrc               ##shell本身的系统配置文件

74       if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
75          umask 002             #普通用户的umask  (uid>199 用户和用户组是一样的)
76       else
77          umask 022 -- 077      #root用户的umask
78       fi

vim /etc/profile               ##系统环境配置文件
59 if [ $UID -gt 199 ] && [ "` id -gn`" = "`id -un`" ]; then
60     umask 002                 #普通用户的umask
61 else      
62     umask 022 -- 007          #root用户的umask
63 fi

source /etc/bashrc               ##source作用时使我们更改的内容立即被系统识别(重新加在生效)
source /etc/profile
    ##两个文件的umask值必须一致,改完后用 source进行重读 这样可以将文件的权限永久固定下来

五 文件用户用户组管理

chown username file              ##更改用户拥有者

chgrp groupname file             ##更改文件拥有组
 
chown username:groupname file    ##同时更改文件的拥有者和拥有组

chown|chgrp -R user|group dir    ##更改目录本身及目录中内容的拥有者或者拥有组

六 特殊权限 重点!!!

chmod   1始权限 原 dir
chmod     o+t     dir
chmod   1777   dir
实验:
mkdir            /pub
chmod    777     /pub

su - westos ----> touch /pub/westosdirfile
exit
su - lee --------> touch /pub/leefile
rm -fr /pub/leefile        #可以删除
rm -fr /pub/westosdirfile  #不属于自己的文件不能删除
rm: cannot remove 'westosdirfile': Openration not permitted

课堂练习
[root@westos111111111 mnt]# mkdir /mnt/rttdir
[root@westos111111111 mnt]# chmod 777 rttdir
[root@westos111111111 mnt]# su - westos
Last login: Sun Jul 18 10:58:42 CST 2021 on pts/3
[westos@westos111111111 ~]$ touch /mnt/rttdir/rtt
[westos@westos111111111 ~]$ exit
logout
[root@westos111111111 mnt]# useradd lee
useradd: user 'lee' already exists
[root@westos111111111 mnt]# su - lee
[lee@westos111111111 ~]$ touch /mnt/rttdir/rttfile
[lee@westos111111111 ~]$ rm -fr /mnt/rttdir/rttfile
[lee@westos111111111 ~]$ rm -fr /mnt/rttdir/rtt         ##root用户将权限全部开启 任何人都可以执行任何操作
[lee@westos111111111 ~]$ su - root
Password: 
Last login: Sun Jul 18 10:04:48 CST 2021 on tty2
[root@westos111111111 ~]# chmod 1777 /mnt/rttdir        ##root用户给rttdir目录+t来限制权限
[root@westos111111111 ~]# su - westos
Last login: Sun Jul 18 12:12:55 CST 2021 on pts/2
[westos@westos111111111 ~]$ touch /mnt/rttdir/rtt
[westos@westos111111111 ~]$ su - root
Password: 
Last login: Sun Jul 18 12:16:45 CST 2021 on pts/2
[root@westos111111111 ~]# su - lee
Last login: Sun Jul 18 12:14:26 CST 2021 on pts/2
Last failed login: Sun Jul 18 12:19:23 CST 2021 on pts/2
There were 3 failed login attempts since the last successful login.
[lee@westos111111111 ~]$ touch /mnt/rttdir/rttfile
[lee@westos111111111 ~]$ rm -fr /mnt/rttdir/rttfile
[lee@westos111111111 ~]$ rm -fr /mnt/rttdir/rtt
rm: cannot remove '/mnt/rttdir/rtt': Operation not permitted     ##限制权限后,切换普通用户后,其他人不能开启不属于自己的文件

#sgid           强制位
#针对目录: 目录中新建的文件自动归属到目录的所属组中(不再归属当前执行者自己的组)

设定:
chmod 2源文件权限     dir
chmod g+s           dir

实验
group westoste

mkdir /mnt/public
chmod 777 /mnt/public

westos ---> touch /mnt/public/file   ##是谁建立的文件组就是谁的

chmod g+s /mnt/westosdir

westos ---> touch /mnt/pubilc/file1  ##file1自动复制了/mnt/public目录组

课堂实验
Every 1.0s: ls -Rl /mnt     westos111111111.westos.org: Sun Jul 18 13:16:19 2021   ##对文件权限信息进行监控

/mnt:
total 0
drwxrwsrwx. 2 root qq 29 Jul 18 13:14 rttdir

/mnt/rttdir:
total 0
-rw-rw-r--. 1 westos root 0 Jul 18 13:05 rtt
-rw-rw-r--. 1 lee    qq   0 Jul 18 13:14 rtt1

[root@westos111111111 ~]# groupadd qq
[root@westos111111111 ~]# chgrp qq /mnt/rttdir      ##root用户对rttdir目录组进行更改更改
[root@westos111111111 ~]# chmod g+s /mnt/rttdir     ##root用户对rttdir目录组设定+s权限
[root@westos111111111 ~]# su - lee                  ##切换普通用户
Last login: Sun Jul 18 12:19:52 CST 2021 on pts/2 
[lee@westos111111111 ~]$ touch /mnt/rttdir/rtt1     ##普通用户在rttdir目录中建立文件,其目录组归属于rttdir目录的目录组中

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

实验
su - westos
/bin/cat   ##程序(代码运行的状态)

watch -n 1 "ps ax -o user,group,comm | grep cat"
westos   westos   cat

用root用户身份
chmod g+s /bin/cat
su - westos
/bin/cat

ps ax -o user,group,comm | grep cat
westos   root   cat

课堂练习
Every 1.0s: ps ax -o co...  westos111111111.westos.org: Sun Jul 18 13:52:29 2021       ##对运行进程进行监控

cat             westos   root


root@westos111111111 mnt]# ls -l /bin/cat
-rwxr-xr-x. 1 root root 51856 Jan 11  2019 /bin/cat
[root@westos111111111 mnt]# chmod g+s /bin/cat         ##root用户对二进制的可执行文件的进程设定g+s权限
[root@westos111111111 mnt]# ls -l /bin/cat
-rwxr-sr-x. 1 root root 51856 Jan 11  2019 /bin/cat
[root@westos111111111 mnt]# su - westos
Last login: Sun Jul 18 13:42:45 CST 2021 on pts/3
[westos@westos111111111 ~]$ cat                        ##切换普通用户运行时,产生的进程的所有组自动归属root用户组,其权力得到了提升

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

chmod 4原属性  file
chmod u+s      file

实验:
su - westos
/bin/cat

ps ax -o user,group,comm | grep cat
westos   westos   cat

用root用户身份
chmod u+s /bin/watch
su - westos
/bin/cat

ps ax -o user,group,comm | grep cat
root   westos   cat

课堂实验
Every 1.0s: ps ax -o co...  westos111111111.westos.org: Sun Jul 18 14:15:08 2021

cat             root     root

[root@westos111111111 mnt]# chmod u+s /bin/cat
[root@westos111111111 mnt]# ls -l /bin/cat
-rwsr-sr-x. 1 root root 51856 Jan 11  2019 /bin/cat
[root@westos111111111 mnt]# su - westos
Last login: Sun Jul 18 13:46:27 CST 2021 on pts/3
[westos@westos111111111 ~]$ cat            ##切换普通用户运行时,产生进程的用户

七 acl权限列表

Aiccess Control Lists  #访问控制列表

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

#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列表功能开启

#acl列表权限读取
getfacl westosfile

显示内容分析

# file: westosfile         #文件名称
# owner: root              #文件拥有者
# group: root              #文件拥有组
user::rw-                  #文件拥有者权限
user:lee:rw--              #特殊指定用户权限
group::r--                 #文件拥有组权限
group::westos:---          #特殊的用户组的权限
mask::rw-                  #能够赋予特殊用户和特殊用户组的最大权限阀值
other::r--                 #其他人的权限

注意:
"当文件权限列表开启,不要用ls -l 的方式来读取文件的权限"

#acl列表的控制
setfacl -m u:lee:rw    westosfile      # -m 表示设定目录下的lee文件的用户有rw的权限
setfacl -m g:westos:rw westosfile      #设定目录下的lee文件的用户组有rw的权限
setfacl -m u::rwx      westosfile      #设定目录的所有用户有rwx的权限
setfacl -m g::0        westosfile      #设定目录的所有用户组没有权限
setfacl -x u:lee       westosfile      # -x表示删除 删除目录中的lee (不能直接删除所有人或所有组)
setfacl -b westosfile  westosfile      #关闭
 查看:getfacl westosfile  ##只能用getfacl查看
#acl 权限优先级

拥有者 > 特殊指定用户 > 权限多的组 > 权限少的组 > 其他

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

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

setfacl -m m:lee:r-x westosfile   ##指定mask权限为r-x,则其他权限也会随之改变
恢复:
setfacl -m m:权限   文件

#acl 列表的默认权限
setfacl -m u:lee:rwx /mnt/westosdir   ##只对于/mnt/westosdir目录本身生效
setfacl -Rm u:lee:rwx /mnt/westosdir  ##对于/mnt/westosdir目录和目录中已经存在的内容生效

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

setfacl  -m d:u:lee:rwx /mnt/westosdir/ ##针对于/mnt/westosdir目录中新建文件生效

八 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、付费专栏及课程。

余额充值