Linux系统中的权限管理

1.权限查看及读取

1.权限查看

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

2.权限的读取
文件的属性被叫做文件的元数据(meta data),一种元数据用1个byte来记录内容。
文件的权限针对三类对象进行定义
owner(u)拥有者
group(g)拥有组
other (o)其他人

每个文件针对每类访问者定义了三种主要权限:
r:Read(读) w:Write(写) x:eXecute(执行)
对于文件和目录来说,r,w,x有着不同的含义:

针对文件:r表示读取文件内容,w表示修改文件内容,x表示执行权限,对除二进制程序以外的文件没什么意义。
针对目录:r看目录下的文件列表,w删除和创建目录下的文件,x可以进入目录,能查看目录中文件的详细属性,能访问目录下的文件内容。

[root@westosaaaaaaaa mnt]# touch file1
[root@westosaaaaaaaa mnt]# mkdir dir1
[root@westosaaaaaaaa mnt]# ls -lR
.:
total 0
drwxr-xr-x. 2 root root 6 Jul 22 10:43 dir1
-rw-r--r--. 1 root root 0 Jul 22 10:43 file1

./dir1:
total 0
对以上显示内容,对应的解释如下:

在这里插入图片描述

1.文件类型

- 普通文件       d 目录    l 软连接	
b 快设备     c 字符设备     s socket套接字
 p 管道 |

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

2.设定普通权限的方法

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 ##用字副方式设定文件权限
u表示拥有者,g表示拥有组,o表示其他人,a表示所有人
示例:

chmod u-rw  /mnt/westos1
chmod u-rw  /mnt/westosfile1 
chmod u-rw,g+x,o+wx  /mnt/westosfile2 
chmod a-rwx  /mnt/westosfile3 
chmod u=rwx,g=rx,o=---  /mnt/westosfile4
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

示例:

[root@westosaaaaaaaa mnt]# chmod 641 file1
[root@westosaaaaaaaa mnt]# ls -l file1
-rw-r----x. 1 root root 0 Jul 22 10:43 file1
3.系统默认权限设定

系统本身存在的意义共享资源,从安全角度讲系统共享的资源越少,开放的权力越小系统安全性越高,既要保证系统安全,又要系统创造价值,于是把应该开放的权力默认开放,把不安全的权力默认保留。
umask表示系统保留权力
umask #查看保留权力
umask 权限值 #临时设定系统预留权力
文件默认权限 = 777-umask-111
目录默认权限 = 777-umask
umask值越大系统安全性越高
永久更改

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

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

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

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

示例:
同时修改file1文件的拥有者和拥有组为westos,修改dir1的拥有组为westos

[root@westosaaaaaaaa mnt]# chown westos:westos file1
[root@westosaaaaaaaa mnt]# chgrp -R westos dir1 
[root@westosaaaaaaaa mnt]# ls -l
total 0
drwxr-xr-x. 2 root   westos 6 Jul 22 10:43 dir1
-rw-r----x. 1 westos westos 0 Jul 22 10:43 file1
4.特殊权限###

Sticky 粘制位
#针对目录:如果一个目录设置sticky权限,尽管其他用户有写权限,也只能由文件所有者执行删除和移动等操作。
chmod 1+原始权限 dir
chmod o+t dir
实验:

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

如何解决:

logout(切回超级用户)
chmod 1777 /mnt/pub   

chmod ot /mnt/pub

su - westos
touch /mnt/pub/westosfile
logout
su - lee
touch /mnt/pub/leefile 
rm -fr /mnt/pub/leefile 
rm -fr /mnt/pub/westosfile    #不属于自己的文件不能删除

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

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

实验:

group westos
mkdir /mnt/public
chmod 777 /mnt/public
 touch /mnt/public/file       ##是谁建立的文件组就是谁的
chmod g+s /mnt/westosdir
touch /mnt/public/file1        ##file1自动复制了/mnt/public目录组

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

#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
5.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   file   #设定lee用户拥有rw权限
setfacl -m g:westos:rw file   #设定westos组拥有rw权限
setfacl -m u::rwx file  #设定拥有者rwx权限
setfacl -m g::0   file    #设定拥有组权限为0
setfacl -x u:lee  file   #删除列表中lee用户的权限
setfacl -b file            #关闭

#acl 列表的默认权限

setfacl -m u:lee:rwx /mnt/dir1  ##只对于/mnt/westosdir目录本身生效
 setfacl -Rm u:lee:rwx /mnt/dir1  ##对于/mnt/westosdir目录和目录中已经存在的内容生效
6.attr权限

#attr权限限制所有用户

i #不能作任何的更改
a #能添加不能删除

lsattr dir|file  ##查看attr权限     ( |表示或的意思 )
chattr +i|+a|-i|-a dir|file   ##设定attr权限
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值