理解Linux下文件安全权限的设定与更改

如何查看及读取权限信息

查看文件属性

• 文件属性的查看方式 ls -l wang

在这里插入图片描述

 • 目录属性的查看方式
   ls –ld wangdir

对于属性各字段的理解
文件属性字段的理解目录属性字段的理解
1 类型1 类型
2 文件权限2目录权限
3 SELinux Context3 SELinux Context
4文件硬链接个数4目录中子目录的个数
5文件拥有者5文件拥有者
6文件拥有组6文件拥有组
7文件大小7目录中子文件或者子目录源数据大小
8文件最后一次被修改的时间8目录的内容最后一次被修改的时间
9文件名称9目录名称

文件的拥有者及拥有组

Linux 是个多用户多任务的系统 , 常常会有多人同时使用同一主机来进行工 作 , 为了考虑每个人的隐私权以及每个人喜好的工作环境 , 对用户进行分类

用户对于文件的 身份划分
文件拥有者
(user)
文件所属组
(group)
其他人
(other)
更改文件拥有者及拥有组的方法
• 文件拥有者及拥有组只有超级用户root可以修改
• 更改方式如下 chown

 chown		用户名     文件
 chgrp		组名称     文件
 chown -R   用户名     目录
 chgrp -R    组名称     目录
 chown       用户名 : 组名称		文件 | 目录

实验操作:
监控代码: watch -n 1 ls -lR /mnt

文件权限的理解

1.对权限的理解
r
对文件:是否可以查看文件中的内容   --->cat file
对目录:是否可以查看目录中有什么子文件或者子目录 ---> ls dir
w
对文件:是否可以改变文件里面记录的字符
对目录:是否可以对目录中子目录名字和子文件的元数据进行更改
x
对文件:是否可以通过文件名称调用文件内记录的程序
对目录:是否可以进入目录
2.更改方式
 更改方式一共有三种:字符,数字和复制
 chmod	<u|g|o><+|-|=><r|w|x>	file|dir
 chmod 	u+x	/mnt/westos1
 chmod 	g-r	/mnt/westos2
 chmod	ug-r	/mnt/westos1
 chmod	u-r,g+x	/mnt/westos2
 chmod	-r	/mnt/westosdir/test1
 chmod	o=r-x	/mnt/westosdir/test2
 rwx
210
r=4
w=2
x=1

r-x|r--|--x
5   4   1

chmod 541 /mnt/file1
7=rwx
6=rw-
5=r-x
4=r--
3=-wx
2=-w-
1=--x
0=---

示例:
eg:
监控命令:

[root@workstation Desktop]# watch -n 1 ls -lR /mnt/

监控,观察每次操作一次时操作后的变化,更加深刻的理解。

实验部分代码:
新建文件、目录作为实验素材:

[root@workstation Desktop]# cd /mnt
[root@workstation mnt]# touch westos1
[root@workstation mnt]# touch westos2
[root@workstation mnt]# mkdir westosdir
[root@workstation mnt]# touch westosdir/test{1..2}
[root@workstation mnt]# chmod 777 file

复制:

  [root@workstation mnt]# chmod --reference=file file1

字符:
ugo + - = “+”添加权限 “-”删除权限 “=”把权限设定为(不管原来权限是什么)

[root@workstation mnt]# chmod o-w westos1
[root@workstation mnt]# chmod ug-r westos2
[root@workstation mnt]# chmod ugo=rwx {westos1,westos2}

可以用“,”隔开同时更改不同的文件。

注意:
[root@workstation westosdir]# chmod +r westos1 所有加r
+x 所有+x
+w 只对user+w,对其他的不起作用
(-r,-w,-x同理)
可以用a+rwx(all)
a+r,a+x等于+r,+x
a+w,给所有的都加w,跟+w不同

数字:

[root@workstation mnt]# chmod 000 {test1,test2,test3}
[root@workstation mnt]# cd westosdir
[root@workstation westosdir]# chmod 111 test1
[root@workstation test]# chmod 421 test2
[root@workstation test]# chmod 754 test3

在这里插入图片描述

umask的修改与设定

注意:
   系统会预留022(可用umask修改),对目录和文件
   对文件,不同文件系统保留的权限不同,一般是011
   umask 保留权限最大值
   在shell改是临时的

代码实现:

[root@workstation test]# umask
0022
[root@workstation test]# umask 077
[root@workstation test]# umask
0077
[root@workstation test]# touch test1
[root@workstation test]# mkdir linuxdir
[root@workstation test]# 

实验效果对比:
新shell

[root@workstation Desktop]# cd /mnt/test
[root@workstation test]# umask
0022
[root@workstation test]# touch test2

永久性的设立:

注意:shell已经调用过了,不会再调用,新开shell会自行调用,不能关了重启的,用source重新调用一遍。
[root@workstation test]# touch test2
[root@workstation test]# vim /etc/bashrc
[root@workstation test]# vim /etc/profile
[root@workstation test]# source /etc/bashrc
[root@workstation test]# umask
0022
[root@workstation test]# source /etc/profile
[root@workstation test]# umask
0077

在这里插入图片描述

新打开shell调用的文件和用source命令调用文件,文件的加载顺序不同。(若两个文    件值不同,生效后调用的文件)
/etc/bashrc中,-gt是大于的意思,"id -gn"="id -un"是当前id的uname=gname
第一行是普通用户,第二行是超级用户。
注意:766不能减111,因为6是4+2,没有1

特殊权限的设定

1.sticky ##粘制位
作用:
只针对目录生效,当一个目 录上有sticky权限时
在这个目录中的文件只能被文件的所有者删除

设定方式:
chmod o+t 	dir
chmod 1xxx 	dir

实例演练:
在这里插入图片描述

2.sgid 强制位
作用
对文件:	只针对与二进制可执行文件
		当文件上有sgid时任何人执行此文件产成的进程都属于文件的的组
对目录:
		当目录上有sgid权限时任何人在此目录中建立的文件都属于目录的所 有组

设定方式
chmod g+s  file|dir
chmod 2xxx file|dir
3.suid 冒险位
只针对与2进制可执行文件
当文件上有suid时任何人执行这个文件中记的程序产生的进程都属于文件的所有人

设定方式
chmod u+s  file
chmod 4xxx file

强制位和冒险位实例:
在这里插入图片描述
监控:
两个实验:
一、观察程序cat的进程,用student用户执行/bin/cat,观察进程的拥有者,用u+s,g+s更改
/bin/cat后,再用student执行,观察进程的拥有者
[root@workstation Desktop]# watch -n 1 ‘ps ax -o user,group,comm | grep cat’ (grep cat是为了把cat这个命令的进程抓取出来,否则会显示多个进程)
二、监控目录/mnt/观察student用户建立文件的所有者和所有组,用g+s改变目录后,用student建立文件观察文件拥有组。(对于目录而言,只能用g+s,u+s不起作用)
eg:
在这里插入图片描述
强制位添加t后普通用户之间只能删除属于自己的文件,不能删除属于其他用户建立的文件。
实例示例:
在这里插入图片描述

注意:
原来普通用户可以删除别的用户的文件,o+t后,普通用户只能删除自己拥有的文件,不能删除别的用户拥有的文件。

acl权限列表

1.作用
让特定的用户对特定的文件拥有特定权限
2.acl列表查看
-rw-rwxr--+ 1 root root 0 Jul 21 15:45 file
	  ^	
	acl开启

getfacl file	##查看acl开启的文件的权限
# file: file	##文件名称
# owner: root	##文件拥有者
# group: root	##文件拥有组
user::rw-	##文件拥有人的权限
user:kiosk:rwx	##指定用户的权限
group::r--	##文件拥有组的权力
mask::rwx	##能赋予用户的最大权力伐值
other::r--	##其他人的权限
3.acl列表的管理
getfacl  file
setfacl  -m u:username:rwx file	##设定username对file拥有rwx权限
setfacl  -m g:group:rwx	file	##设定group组成员对file拥有rwx权限
setfacl  -x u:username	file	##从acl列表中删除username
setfacl  -b file		##关闭file上的acl列表

先监控
[root@workstation Desktop]# watch -n 1 ‘ls -l /mnt/file;getfacl /mnt/file’
setfacl -m/-x/-b
代码部分:

[root@workstation mnt]# setfacl -b /mnt/file
[root@workstation mnt]# setfacl -m u:student:777 /mnt/file
setfacl: Option -m: Invalid argument near character 12
[root@workstation mnt]# setfacl -m u:student:rw /mnt/file
[root@workstation mnt]# setfacl -x u:student /mnt/file
[root@workstation mnt]# setfacl -m u:student:rw- /mnt/file
[root@workstation mnt]# setfacl -x u:student /mnt/file
[root@workstation mnt]# setfacl -m u:student:rx /mnt/file

在这里插入图片描述

在这里插入图片描述

图片中+号表示功能列表位开启

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值