RH124(6)----Linux系统中的权限管理_stripped, too many notes-程序员宅基地

一、权限查看及读取

1、权限查看

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

[root@localhost mnt]# ls -l westosfile
-rw-r--r--. 1 root root 0 Dec 16 20:31 westosfile
[root@localhost mnt]# ls -ld westosdir/
drwxr-xr-x. 2 root root 6 Dec 16 20:32 westosdir/

2、权限的读取

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

#文件权限信息#
“-”| 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 软连接
         #b 快设备
         #c 字符设备
         #s socket套接字
         #p 管道
[2]      ##用户权限
         ##rw -|r–|r–
         # u — g-- o
[3]      ##系统的selinux开启
[4]      ##对于文件:文件内容被系统记录的次数(硬链接个数)
         ##对于目录:目录中子目录的个数
[5]      ##文件拥有者
[6]      ##文件拥有组
[7]      ##对于文件:文件内容大小
         ##对于目录:目录中子文件的元数据大小
[8]      ##文件内容被修改的时间
[9]      ##文件名称
硬连接  一个节点对应多个数据区域
软连接 多个节点对应一个数据区域
[root@localhost mnt]# echo hello westos > westosfile
[root@localhost mnt]# file westosfile 
westosfile: ASCII text
[root@localhost mnt]# ls -l westosfile
-rw-r--r--. 1 root root 13 Dec 16 20:48 westosfile
[root@localhost mnt]# ln -s /mnt/westosfile /home/westos/Desktop/
[root@localhost mnt]# ls -l /home/westos/Desktop/westosfile 
lrwxrwxrwx. 1 root root 15 Dec 16 20:48 /home/westos/Desktop/westosfile -> /mnt/westosfile
[root@localhost mnt]# ls -l /dev/sr0
brw-rw----+ 1 root cdrom 11, 0 Dec 15 20:54 /dev/sr0
[root@localhost mnt]# ls -l /dev/pts/0
crw--w----. 1 westos tty 136, 0 Dec 16 20:09 /dev/pts/0

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

1、用户对文件的身份

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

[root@localhost mnt]# ls -l westos1
-rw-r--r--. 1 westos westos 0 Dec 17 09:50 westos1

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 代表第归操作]
[root@localhost mnt]# chmod --reference=/mnt/westos1  /mnt/westos2
[root@localhost mnt]# chmod --reference=/mnt/westosdir /mnt/westos3

在这里插入图片描述

#chmod 字符方式设定权限
chmod <a|u|g|o><+|-|=><r|w|x> file ##用字副方式设定文件权限
a---all  u---user g---group  o---other
[root@localhost mnt]# chmod u-rw /mnt/westos1
[root@localhost mnt]# chmod u-rw /mnt/westosfile1
[root@localhost mnt]# chmod u-rw,g+x,o+wx /mnt/westosfile2
[root@localhost mnt]# chmod a-rwx /mnt/westosfile3
[root@localhost mnt]# chmod u=rwx,g=rx,o=--- /mnt/westosfile4
[root@localhost mnt]# chmod -R u=rwx,g=rx,o=--- /mnt/westosdir/
#chmod 数字方式设定权限#
权限波尔指表示方式
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-------
布尔值 750-111=640  0-1=0
[root@localhost mnt]# chmod 640 westos3
[root@localhost mnt]# chmod 755 westos2
[root@localhost mnt]# chmod 742 westos1
[root@localhost mnt]# chmod -R 777 westosdir

在这里插入图片描述

四、系统默认权限设定

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

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

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

umask值越大系统安全性越高

umask临时更改
umask 077

[root@localhost mnt]# umask 077
[root@localhost mnt]# umask
0077
[root@localhost mnt]# touch westosfile1
[root@localhost mnt]# mkdir westosdir1

在这里插入图片描述

永久更改

[root@localhost mnt]# vim /etc/bashrc ##shell系统配置文件
74 if [ $UID -gt 199 ] && [ “id -gn” = “id -un” ]; then
75     umask 002  #普通用户的umask
76 else
77     umask 022 ---->077  #root用户的umask
78 fi

[root@localhost mnt]# vim /etc/profile ##系统环境配置文件
59 if [ $UID -gt 199 ] && [ “id -gn” = “id -un” ]; then
60     umask 002 #普通用户的umask
61 else
62     umask 022 --->077 #root用户的umask
63 fi
[root@localhost mnt]# source /etc/bashrc  ##source作用时使我们更改的内容立即被系统识别
[root@localhost mnt]# source /etc/profile 

五、文件用户用户组管理

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

开启根目录权限查看监控,并在/mnt/下建立文件夹和文件
更改拥有者或者拥有组
在这里插入图片描述

[root@localhost mnt]# watch -n 1 "ls -lR /mnt/"
[root@localhost mnt]# chown lee /mnt/westos1
[root@localhost mnt]# chown lee.westos /mnt/westos2
[root@localhost mnt]# chown lee:westos /mnt/westos3
[root@localhost mnt]# chgrp lee /mnt/westosdir
[root@localhost mnt]# chgrp -R lee /mnt/westosdir
[root@localhost mnt]# chown -R lee /mnt/westosdir
[root@localhost mnt]# chown -R root.root /mnt/westosdir
[root@localhost mnt]# chown -R root.root /mnt/

六、特殊权限

stickyid 粘制位

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

chmod 1原始权限 dir
chmod o+t dir

实验:
mkdir /mnt/pub
chmod 777 /mnt/pub

su - westos ----> touch /mnt/pub/westosfile
su - lee ----> touch /mnt//pub/leefile
rm -fr /mnt/pub/leefile #可以删除
rm -fr /mnt/pub/leefile #不属于自己的文件也可以删除

chmod 1777 /mnt/pub
chmod o+t /mnt/pub
以上两条命令都可以开启mnt/pub目录的t权限

[root@localhost mnt]# su - lee
[lee@localhost mnt]$ touch /mnt/pub/leefile
[lee@localhost mnt]$ logout
[root@localhost mnt]# su - linux
[linux@localhost mnt]$ touch /mnt/pub/linuxfile
[linux@localhost mnt]$ logout
[root@localhost linux]# chmod o+t /mnt/pub
[root@localhost linux]# su - lee
[lee@localhost ~]$ rm -fr /mnt/pub/leefile
[lee@localhost ~]$ rm -fr /mnt/pub/linuxfile
rm: cannot remove '/mnt/pub/linuxfile': Operation not permitted
[root@localhost home]# chmod 777 /mnt/pub
[root@localhost home]# chmod 1777 /mnt/pub

sgid 强制位

#针对目录: 目录中新建的文件自动归属到目录的所属组中

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

[linux@localhost ~]$ id linux
uid=1003(linux) gid=1003(linux) groups=1003(linux),8000(shengchan)
[linux@localhost ~]$ touch /mnt/sc/file  ##是谁建立的文件组就是谁的
[linux@localhost ~]$ logout
[root@localhost mnt]# chmod g+s /mnt/sc
[root@localhost mnt]# su - linux
[linux@localhost ~]$ touch /mnt/sc/file1   ##file1自动复制了/mnt/sc目录组

在这里插入图片描述

[root@localhost mnt]# id linux
uid=1003(linux) gid=1003(linux) groups=1003(linux),8000(shengchan)
[linux@localhost ~]$ /bin/cat

[root@localhost mnt]# ls -l /bin/cat
-rwxr-xr-x. 1 root root 51856 Jan 11  2019 /bin/cat
[root@localhost mnt]# ps ax -o user,group,command | grep cat
gdm      gdm      /usr/libexec/gsd-print-notifications
westos   westos   /usr/libexec/gsd-print-notifications
westos   westos   /usr/bin/gnome-software --gapplication-service
linux    linux    /bin/cat
root     root     grep --color=auto cat
[root@localhost mnt]# chmod g+s /bin/cat
[root@localhost mnt]# ls -l /bin/cat
-rwxr-sr-x. 1 root root 51856 Jan 11  2019 /bin/cat
[root@localhost mnt]# ps ax -o user,group,command | grep cat
gdm      gdm      /usr/libexec/gsd-print-notifications
westos   westos   /usr/libexec/gsd-print-notifications
westos   westos   /usr/bin/gnome-software --gapplication-service
linux    root     /bin/cat
root     root     grep --color=auto cat
[root@localhost mnt]# ls -l /bin/cat
-rwxr-sr-x. 1 root root 51856 Jan 11  2019 /bin/cat

suid 冒险位

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

chmod 4原属性 file
chmod u+s file
在这里插入图片描述
超级用户不能删根

[root@localhost mnt]# useradd -s /sbin/nologin -M norm
[root@localhost mnt]# file /bin/rm
/bin/rm: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=943105ab9f2599b6725b5b90a4c05b549ce50beb, stripped, too many notes (256)
[root@localhost mnt]# chown norm.norm /bin/rm
[root@localhost mnt]# chmod ug+s /bin/rm 
[root@localhost mnt]# rm -fr /mnt/
rm: cannot remove '/mnt/.fiel1.swp': Permission denied
rm: cannot remove '/mnt/.file.swp': Permission denied
rm: cannot remove '/mnt/sc': Permission denied
[root@localhost mnt]# chmod 755 /bin/rm ##还原

七、acl权限列表

Aiccess Control Lists #访问控制列表

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

acl列表开启标识

没有"+“代表acl列表未开启
drwxrwxrwx+ 2 root root 6 Dec 17 15:57 /mnt/westos
没有”+"代表acl列表未开启 “+”代表acl列表功能开启
在这里插入图片描述

acl列表权限读取

getfacl +文件名

显示内容分析
#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:westos:rwx /mnt/westos#设定
setfacl -m g:westos:rwx /mnt/westos
setfacl -m u::rwx /mnt/westos
setfacl -m g::0 /mnt/westos
setfacl -x u:westos /mnt/westos##删除列表中的westos
setfacl -b /mnt/westos #关闭

[root@localhost mnt]# mkdir westos
[root@localhost mnt]# chmod 777 /mnt/westos
[root@localhost mnt]# setfacl -m u:westos:0 /mnt/westos
[root@localhost mnt]# getfacl /mnt/westos
getfacl: Removing leading '/' from absolute path names
# file: mnt/westos
# owner: westos
# group: root
user::rwx
user:westos:---
group::rwx
mask::rwx
other::rwx
[root@localhost mnt]# su - westos
[westos@localhost ~]$ cd /mnt/westos
-bash: cd: /mnt/westos: Permission denied
[root@localhost mnt]# ls -ld /mnt/westos
drwxrwxrwx+ 2 root root 6 Dec 17 15:57 /mnt/westos

acl 权限优先级

拥有者 > 特殊指定用户 > 权限多的组 >权限少的组 > 其他
设置拥有者和特殊拥有者同一个,给特殊的满权限,拥有者0权限,切换到该用户在/mnt/guo/目录下建立文件,成功说明特殊的优先级别高,反之拥有者优先级高

acl mask 控制

mask是能够赋予指定用户权限的最大阀值
问题:当设定完毕文件的acl列表之后用chmod缩小了文件拥有组的权力,mask会发生变化
恢复: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权限

[root@localhost mnt]# mkdir westosdir
[root@localhost mnt]# chattr +a westosdir
[root@localhost mnt]# lsattr westosdir -d
-----a-------------- westosdir
[root@localhost mnt]# touch westosdir/file
[root@localhost mnt]# rm -fr westosdir/file
rm: cannot remove 'westosdir/file': Operation not permitted
[root@localhost mnt]# chattr -a westosdir
[root@localhost mnt]# rm -fr westosdir/file
[root@localhost mnt]# chattr +i westosdir/
[root@localhost mnt]# lsattr westosdir -d
----i--------------- westosdir
[root@localhost mnt]# touch westosdir/file
touch: setting times of 'westosdir/file': No such file or directory
[root@localhost mnt]# rm -fr westosdir/file
rm: cannot remove 'westosdir/file': Operation not permitted
[root@localhost mnt]# chattr -i westosdir/
[root@localhost mnt]# lsattr westosdir -d
-------------------- westosdir
[root@localhost mnt]# rm -fr westosdir/file
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_45777669/article/details/111301974

智能推荐

python色卡识别_用Python帮小姐姐选口红,人人都是李佳琦-程序员宅基地

文章浏览阅读502次。原标题:用Python帮小姐姐选口红,人人都是李佳琦 对于李佳琦,想必知道他的女生要远远多于男生,李佳琦最早由于直播向广大的网友们推荐口红,逐渐走红网络,被大家称作“口红一哥”。不可否认的是,李佳琦的直播能力确实很强,他能够抓住绝大多数人的心理,让大家喜欢看他的直播,看他直播推荐的口红适不适合自己,色号适合什么样子的妆容。为了提升效率,让自己的家人或者女友能够快速的挑选出合适自己妆容的口红色号,今..._获取口红品牌 及色号,色值api

linux awk命令NR详解,linux awk命令详解-程序员宅基地

文章浏览阅读3.6k次。简介awk命令的名称是取自三位创始人Alfred Aho 、Peter Weinberger 和 Brian Kernighan姓名的首字母,awk有自己的程序设计语言,设计简短的程序,读入文件,数据排序,处理数据,生成报表等功能。awk 通常用于文本处理和报表生成,最基本功能是在文件或者字符串中基于指定规则浏览和抽取信息,awk抽取信息后,才能进行其他文本操作。awk 通常以文件的一行为处理单位..._linux awk nr

android 网络连接失败!failed to connect to /192.168.1.186(port 8080)_failed to connect to 192.168.88.218:80-程序员宅基地

文章浏览阅读1.3w次,点赞5次,收藏2次。在网上找了一个小时,一直没有头绪,因为上个星期还是好好的,最后看到一个大神的解答,只需要将防火墙关闭就好了.原本向测试功能的,却卡在了登录上.以此记录.另外好像还有种错误是电脑与手机连接的WiFi不同,也可以看看...._failed to connect to 192.168.88.218:80

matlab 多径衰落,利用MATLAB仿真多径衰落信道.doc-程序员宅基地

文章浏览阅读1.9k次。利用MATLAB仿真多种多径衰落信道摘要:移动信道的多径传播引起的瑞利衰落,时延扩展以及伴随接收过程的多普勒频移使接受信号受到严重的衰落,阴影效应会是接受的的信号过弱而造成通信的中断:在信道中存在噪声和干扰,也会是接收信号失真而造成误码,所以通过仿真找到衰落的原因并采取一些信号处理技术来改善信号接收质量显得很重要,这里利用MATLAB对多径衰落信道的波形做一比较。一,多径衰落信道的特点关于多径衰落..._matlab多径衰落工具箱

python对json的操作及实例解析_import json灰色-程序员宅基地

文章浏览阅读1w次,点赞2次,收藏17次。Json简介:Json,全名 JavaScript Object Notation,是一种轻量级的数据交换格式。它基于 ECMAScript (w3c制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。(来自百度百科)python关于json文_import json灰色

mysql实现MHA高可用详细步骤_mysql mha超详细教程-程序员宅基地

文章浏览阅读1.1k次,点赞6次,收藏3次。一、工作原理MHA工作原理总结为以下几条:(1) 从宕机崩溃的 master 保存二进制日志事件(binlog events);(2) 识别含有最新更新的 slave ;(3) 应用差异的中继日志(relay log) 到其他 slave ;(4) 应用从 master 保存的二进制日志事件(binlog events);(5) 通过Manager控制器提升一个 slave 为新 m..._mysql mha超详细教程

随便推点

Linux环境下主从搭建心得(高手勿喷)_linux的java主从策略是什么-程序员宅基地

文章浏览阅读194次。一 java环境安装:1 安装JDK 参考链接地址:https://blog.csdn.net/qq_42815754/article/details/82968464注:有网情况下直接 yum 一键安装:yum -y list java(1)首先执行以下命令查看可安装的jdk版本(2)选择自己需要的jdk版本进行安装,比如这里安装1.8,执行以下命令:yum install -y java-1.8.0-openjdk-devel.x86_64(3)安装完之后,查看安装的jdk 版本,输入以下指令_linux的java主从策略是什么

ACM第四题_acm竞赛题 i 'm from mars-程序员宅基地

文章浏览阅读104次。定义int 类型,由while实现A,B的连续输入,输出A+B的值按Ctrl Z结束循环。#include&amp;lt;iostream&amp;gt;using namespace std;int main(){ int A,B; while(cin&amp;gt;&amp;gt;A&amp;gt;&amp;gt;B) { cout&amp;lt;&amp;lt;A+B&amp;lt;&_acm竞赛题 i 'm from mars

TextView.SetLinkMovementMethod后拦截所有点击事件的原因以及解决方法-程序员宅基地

文章浏览阅读5.2k次。在需要给TextView的某句话添加点击事件的时候,我们一般会使用ClickableSpan来进行富文本编辑。与此同时我们还需要配合 textView.setMovementMethod(LinkMovementMethod.getInstance());方法才能使点击处理生效。但与此同时还会有一个问题:如果我们给父布局添加一个点击事件,需要在点击非链接的时候触发(例如RectclerV..._linkmovementmethod

JAVA实现压缩解压文件_java 解压zip-程序员宅基地

文章浏览阅读1.1w次,点赞6次,收藏31次。JAVA实现压缩解压文件_java 解压zip

JDK8 新特性-Map对key和value分别排序实现_java comparingbykey-程序员宅基地

文章浏览阅读1.3w次,点赞7次,收藏21次。在Java 8 中使用Stream 例子对一个 Map 进行按照keys或者values排序.1. 快速入门 在java 8中按照此步骤对map进行排序.将 Map 转换为 Stream 对其进行排序 Collect and return a new LinkedHashMap (保持顺序)Map result = map.entrySet().stream() .sort..._java comparingbykey

GDKOI2021普及Day1总结-程序员宅基地

文章浏览阅读497次。第一次参加GDKOI,考完感觉还可以,结果发现还是不行,有一些地方细节打错,有些失分严重,总结出以下几点:1.大模拟一定要注意,细节打挂就是没分,像T1就是一道大模拟题,马上切了,后面就没想着检查以下,导致有些地方挂掉了,用民间数据一测,才85分。2.十年OI一场空,不开longlonglong longlonglong见祖宗。今天的T2本来想用暴力水点分的,结果没想到longlong→intlong long\to intlonglong→int,40→040\to040→0。3.代码实现能力太差,_gdkoi

推荐文章

热门文章

相关标签