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

智能推荐

MQ的概念和RabbitMQ知识点(无代码)-程序员宅基地

文章浏览阅读1.2w次,点赞7次,收藏76次。MQ全称是MessageQueue(消息队列),是保存消息在传输过程中的一种容器,既是存储消息的一种中间件。多是应用在分布式系统中进行通信的第三方中间件,如下图所示,发送方成为生产者,接收方称为消费者。............_mq

如何做好Bug分析-程序员宅基地

文章浏览阅读1.5k次,点赞47次,收藏18次。Bug分析是QA的一项主要技能,需要针对项目中遇到的经典问题进行分类分析, 直达问题本质。 并且能够给团队其他项目或者成员起到典型的借鉴作用。 当然也有一些非常经典的问题可以进行技术深挖, 以供参考。 个人认为比较典型的「Bug分析」是stackoverflow, 当然, 一个完善的bug分析库, 可以进行问题分类总结。 对于测试新人是有很大的帮助的。本质上, 在测试领域很多问题是可重现可整理可规避的。另外, bug分析本身是为了拓宽每个人的认知边界, 缩小团队间的乔哈里窗以达到最佳的合作状态。一个「好的B

H5020NL PULSE 50PIN千兆四口网络变压器 HQST H85001S建议IC配置型号_4口网络变压器-程序员宅基地

文章浏览阅读800次。HQST导读:PULSE普思是网络通讯行业中龙头企业之一,其中网络变压器产品大都由国内代工厂代为生产,H5020NLHX5020NL千兆四口网络变压器是普思公司经典老牌产品,相对整个市场用量不是很大,集中生产约一月20万颗左右……PULSE普思是网络通讯行业中龙头企业之一,其中网络变压器产品大都由国内代工厂代为生产,H5020NLHX5020NL千兆四口网络变压器是普思公司经典老牌产品,相对整个市场用量不是很大,集中生产约一月20万颗左右,……PULSE H5020NL千兆网络变压器对应HQS._4口网络变压器

D20 EME 支持2k MAC地址表-程序员宅基地

文章浏览阅读242次,点赞3次,收藏9次。交换机,壳体采用镀锌钢板,结构紧凑,支持八个百兆端口,可配置一至四个百兆光纤端口。两路冗余电源设计,支持4pin可插拔端子,交直流通用,同时提供电源防接保护及过压、欠压保护,极大提升产品工作的稳定性。2.支持两路冗余电源设计,4pin可插拔端子,支持12~36V宽电压输入,交直流通用,同时提供电源防反接保护及过压、欠压保护,极大提升产品工作的稳定性。4.-40℃~75℃工作温度,-40~85℃存储温度,在极端气象条件下也能安全运行。8.支持IEEE802.3,IEEE802.3u,IEEE802.3x。

阿昌教你如何使用通义灵码-程序员宅基地

文章浏览阅读946次。Hi,我是阿昌,今天教你如何使用通义灵码。_通义灵码

老版本NDK下载列表(Android官网)_ndk 老颁布-程序员宅基地

文章浏览阅读2.3w次。我们在开发或编译旧版本NDK项目时,需要使用一些老版本的NDK,在这里提供了旧版NDK的列表及下载链接_ndk 老颁布

随便推点

网关、安全网关?与防火墙的区别(2),网络安全多线程断点续传-程序员宅基地

文章浏览阅读640次,点赞6次,收藏18次。网关是一个大的概念,没有特指是什么设备,很多设备都可以做网关,普通的PC机也能做,常用的网关设备是路由器。网关的作用主要是用来连接两个不同的网络,比如可以连接两个IP地址不相同的网络,或连接两个操作系统不同的网络,如WINDOWS与LINUX互连,或连接两个网络协议不同的网络,如TCP/IP与IPX.或拓扑结构不同的网络,如以太网和令牌环网。总之网关是一种中间媒介。而防火墙也可以做网关,但它的主要做用只是用来防病毒或防黑客,网关只算是防火墙的一个功能。网关与防火墙的区别。

解决:ModuleNotFoundError: No module named ‘pymysql’_modulenotfounderror: no module named 'pymysql-程序员宅基地

文章浏览阅读4.1k次,点赞42次,收藏34次。背景在使用之前的代码时,报错: Traceback (most recent call last): File "xxx", line xx, in import pymysql ModuleNotFoundError: No module named 'pymysql'翻译:```追溯(最近一次通话):文件“xxx”,第xx行,在导入pymysqlModuleNotFoundError:没有名为“pymysql”的模块```原因 ......_modulenotfounderror: no module named 'pymysql

android读取生成excel,Android创建与读取Excel-程序员宅基地

文章浏览阅读275次。1 import java.io.File;23 import java.io.IOException;45 import java.util.Locale;6789 import jxl.CellView;1011 import jxl.Workbook;1213 import jxl.WorkbookSettings;1415 import jxl.format.UnderlineStyle;..._android excel生成读取类

VS2015离线安装 安装包损坏或丢失_vs2015离线版csdn-程序员宅基地

文章浏览阅读4.3w次,点赞16次,收藏126次。1、去微软官网下载完成ISO镜像,最好不要在线安装,打开官方链接 https://www.visualstudio.com/zh-cn/downloads/download-visual-studio-vs.aspx按下图操作:2、用虚拟光驱加载,或者直接右键解压。在安装前,先安装两个证书。亲测,安装后,减少了很多“安装包损坏或丢失”的现象。两证书下载地址链接: https:/..._vs2015离线版csdn

解决vue中安装postcss-pxtorem插件,报错“ Error: PostCSS plugin postcss-pxtorem requires PostCSS 8.”_error: postcss plugin postcss-import requires post-程序员宅基地

文章浏览阅读2k次,点赞4次,收藏3次。目前 postcss-pxtorem 版本最高6.0.0,报这个错是因为插件版本太高,降成5.1.1可解决这个报错解决方法:分两步1.执行npm uninstall post-pxtorem2.执行npm i [email protected]_error: postcss plugin postcss-import requires postcss 8.

Linux-ARM开发_linux arm开发-程序员宅基地

文章浏览阅读787次。Linux-ARM开发_linux arm开发