文件权限以及文件权限的更改,特殊权限(强制位,冒险位和粘制位 )

1.文件权限存在的意义
 系统最底层安全设定方法之一,保证文件可以被可用的用户做相应操作
2.文件权限的查看
ls -l 	file	   # 查看文件权限
ls -ld	dir	   # 查看目录权限
ll 	file	
ll  -d	dir

注:
ls -l = ll  
ls -ld = ll -d   
3.文件权限的读取
ls -l file

 -    rw-rw-r--   1     kiosk   kiosk    0    Oct 2 17:05   file 
[1]    [2]       [3]     [4]     [5]    [6]       [7]        [8]

 注释: 
[1]文件类型    
	 -           # 普通文件    
	 d           # 目录    
	 s           # socket套接字,程序对外开放的接口,可以通过接口进入程序    
	 l           # 软链接,就是快捷方式    
	 p           # 管道    
	 c           # 字符设备,显示字符的设备即终端    
	 b           # 块设备,可以进行终极存储,类似U盘
	 
[2]读写权限 
rw-     rw-     r--  
@        $       *    

        @	        # 文件拥有者对文件能做的动作    
 	$	        # 文件所在组的组成员能对文件做的动作    
 	*	        # 其他人对与文件能做的动作 
[3]文件内容被系统记录的次数 

	对文件:文件硬链接个数(文件内容被记录的次数)	
	对目录:目录中子目录的个
 	
[4]文件拥有者 

[5]文件所在组 

[6]文件大小 

	  对文件:文件大小	
	  对目录:目录中子文件元数据(matedate可以理解为文件的属性)大小

[7]文件最后一次被更改的时间 

[8]文件名称

在这里插入图片描述

4.改变文件的所有人和所有组
(1)chown       # 修改用户所有人,也可以所有人和所在组一起修改,只有超级用户才可以执行

chown   	username	file|dir     # 修改文件和或目录的所有人
chown   	user.group	file|dir     # 修改文件或目录的所有人和所在组
chown	-R	user.group	dir          # 递归修改目录和目录里文件的所有人和所在组

(2)chgrp     # 修改文件或目录的所在组

chgrp		group		file|dir     # 修改目录或文件的所在组
chgrp	-R	group		dir          # 递归修改目录以及目录里文件所在组
5.改变文件的权限
 (1)识别权限 

 rwx	                    r-x	           	r-x 
用户权限(u)              组成员权限(g)	    其他用户权限(o)   

(2)对权限的理解

r	
	对文件:是否可以查看文件中的内容  	
	对目录:是否可以查看目录中有什么子文件或者子目录 
w	
	对文件:是否可以改变文件里面记录的字符	
	对目录:是否可以对目录中子目录或子文件的元数据进行更改
x	
	对文件:是否可以通过文件名称调用文件内记录的程序	
	对目录:是否可以进入目录
(3)更改方式
a.chmod	<u|g|o><+|-|=><r|w|x>	file|dir

	chmod 	u+x	/mnt/file1
	chmod 	g-r	/mnt/file2
	chmod	ug-r	/mnt/file3
	chmod	u-r,g+x	/mnt/file4
	chmod	-r	/mnt/file5
	chmod	o=r-x	/mnt/file6	
	
b.chmod  [权限数字]  [文件名] 

	u=rwx=7	  g=rwx=7    o=rwx=7         777 
	u=rw-=6   g=r--=4    o=r--=4         644 
	r = 4     w = 2      x = 1    - = 0 
	7=rwx,
	6=rw-,
	5=r-x,
	4=r--,
	3=-wx,
	2=-w-,
	1=--x,
	0=---

[root@desktop Desktop]# ll file
	-rw-r--r-- 1 root student 16900 Sep 26 09:12 file
[root@desktop Desktop]# chmod 777 file
[root@desktop Desktop]# ll file
	-rwxrwxrwx 1 root student 16900 Sep 26 09:12 file
[root@desktop Desktop]# chmod ug-w file
[root@desktop Desktop]# ll file
	-r-xr-xrwx 1 root student 16900 Sep 26 09:12 file

在这里插入图片描述

注:系统预留权限为022,即新建目录的默认权限为777-220=755;当新建文件时,文件系统又会保留111的权限,即新建文件的权限为755-111=644

[root@localhost Desktop]# mkdir /mnt/test
[root@localhost Desktop]# ll -d /mnt/test
	drwxr-xr-x 2 root root 6 Oct  1 22:47 /mnt/test
[root@localhost Desktop]# touch /mnt/test/file
[root@localhost Desktop]# ll  /mnt/test/file
	 -rw-r--r-- 1 root root 0 Oct  1 22:47 /mnt/test/file 

(4)文件的默认权限

umask             #系统建立文件是默认保留的权力

[root@desktop Desktop]# umask   # 查看系统的保留的默认权限
	0022   
a.临时修改	
[root@desktop Desktop]# umask 077      # 临时修改umask值
[root@desktop Desktop]# umask
0077 

b.永久修改默认权限 
[root@desktop Desktop]# vim /etc/bashrc       #shell配置文件
	70     if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then 
	71        umask 002     # 普通用户的更改 
	72     else 
	73        umask 022     # 超级用户的更改 
	74     fi 
[root@desktop Desktop]# vim /etc/profile      #系统配置文件
	59 if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then 
	60     umask 002    # 普通用户的更改 
	61 else 
	62     umask 022    # 超级用户的更改  
	63 fi 
	

# 修改完后需要刷新配置,才会生效

[root@desktop Desktop]#source /etc/bashrc           # 刷新bash配置 
[root@desktop Desktop]#source /etc/profile            # 刷新系统配置
6.特殊权限
(1)stickyid	粘制位

o+t(t=1)
	
a.作用:只针对目录生效,当一个目录上有sticky权限时,在这个目录中的文件只能被文件的所有者删除 

b.设定方式:

chmod  o+t 	dir	
chmod 1xxx 	dir

  [root@desktop Desktop]# mkdir /westos
  [root@desktop Desktop]# ls -ld /westos/
 	drwx------ 2 root root 6 Sep 28 11:52 /westos/
  [root@desktop Desktop]# chmod 777 /westos/    # 给westos目录赋予最大权限
  [root@desktop Desktop]# ls -ld /westos/
	drwxrwxrwx 2 root root 6 Sep 28 11:52 /westos/
  [root@desktop Desktop]# su - student
  	Last login: Fri Sep 28 11:52:20 EDT 2018 on pts/0
  [student@desktop ~]$ cd /westos/
  [student@desktop westos]$ ls 
  [student@desktop westos]$ touch studentfile    # 以student用户身份在westos目录下建立文件
  [student@desktop westos]$ ls
  	studentfile
  [student@desktop westos]$ logout
  [root@desktop Desktop]# su - haha
 	 Last login: Fri Sep 28 11:49:21 EDT 2018 on pts/0
  [haha@desktop ~]$ cd /westos/
  [haha@desktop westos]$ lsstudentfile
  [haha@desktop westos]$ touch westosfile    # 以haha用户身份在westos目录下建立文件
  [haha@desktop westos]$ ls /westos/studentfile  westosfile
  [haha@desktop westos]$ rm -fr studentfile  # 无论在什么用户下都可以删除不属于自己建立的文件 
  [haha@desktop westos]$ ls  
 	 westosfile
  [haha@desktop westos]$ rm -fr westosfile 
  [haha@desktop westos]$ ls
  [haha@desktop westos]$ logout
  [root@desktop Desktop]# chmod 1777 /westos/  # 给目录赋予t权限
  [root@desktop Desktop]# su - student
 	 Last login: Fri Sep 28 11:53:02 EDT 2018 on pts/0
  [student@desktop ~]$ cd /westos/
  [student@desktop westos]$ ls
  [student@desktop westos]$ touch studentfile
  [student@desktop westos]$ ls
	studentfile
  [student@desktop westos]$ logout
  [root@desktop Desktop]# su - haha
  	Last login: Fri Sep 28 11:53:58 EDT 2018 on pts/0
  [haha@desktop ~]$ cd /westos/
  [haha@desktop westos]$ ls
 	studentfile
  [haha@desktop westos]$ touch westosfile
  [haha@desktop westos]$ ls
  	studentfile  westosfile
  [haha@desktop westos]$ rm -fr studentfile     # 其他用户不能删除不属于自己建立的文件
  	rm: cannot remove ‘studentfile’: Operation not permitted
  [haha@desktop westos]$ rm -fr westosfile   # 可以删除自己建立的文件
  [haha@desktop westos]$ ls
  	studentfile

(2)sgid          # 强制位

g+s(s=2)	

a.作用
	
对文件:	
	只针对与二进制可执行文件		
	当文件上有sgid时任何人执行此文件产成的进程都属于文件的的组	
对目录:		
	当目录上有sgid权限时任何人在此目录中建立的文件都属于目录的所有组 
	
b.设定方式	
chmod g+s  file|dir	
chmod 2xxx file|dir

[root@localhost ~]# touch file
[root@localhost ~]# ll file
	-rw-r--r-- 1 root root 0 Oct  1 23:50 file
[root@localhost ~]# su - student
	Last login: Mon Oct  1 23:29:19 EDT 2018 on pts/1
[student@localhost ~]$ touch file1    
[student@localhost ~]$ ll file1     # 谁在执行这个进程,进程就属于谁,即studet用户建立的文件属于student用户
	-rw-rw-r-- 1 student student 0 Oct  1 23:51 file1
[student@localhost ~]$ logout
[root@localhost ~]# chgrp student /bin/touch
[root@localhost ~]# chmod g+s /bin/touch   # 对touch这个进程加s的权限,使得所有用户执行的进程都属于student用户,这叫权力下放
[root@localhost ~]# ls -l /bin/touch 
	-rwxr-sr-x. 1 root student 62432 Jan 24  2014 /bin/touch
[root@localhost ~]# touch file3
[root@localhost ~]# ll file3
	-rw-r--r-- 1 root student 0 Oct  1 23:56 file3
[root@localhost ~]# su - tom
	Last login: Mon Oct  1 23:34:38 EDT 2018 on pts/1
[tom@localhost ~]$ touch file5
[tom@localhost ~]$ ll file5
	-rw-rw-r-- 1 tom student 0 Oct  1 23:58 file5

(3)suid	冒险位 
u+s(s=4)	

a.作用
	只针对与2进制可执行文件	
	当文件上有suid时任何人执行这个文件中记的程序产生的进程都属于文件的所有人
	
b.设定方式
	
chmod +s  file	
chmod 4xxx file

[root@localhost Desktop]# ls -l /bin/watch
-rwxr-xr-x. 1 root root 24704 Feb 27  2014 /bin/watch

# 重新打开一个shell登录student用户
[root@localhost Desktop]# su - student
Last login: Mon Oct  1 23:57:43 EDT 2018 on pts/1
[student@localhost ~]$ watch -n 1 date

# 在root用户上
[root@localhost Desktop]# ps ax -o comm,user,group | grep watch
	watchdog/0      root     root
	abrt-watch-log  root     root
	abrt-watch-log  root     root
	watch           student  student
[root@localhost Desktop]# chmod g+s /bin/watch     # 给watch赋予g+s权限
[root@localhost Desktop]# ls -l /bin/watch        # 可以看到g上多了一个s权限
	-rwxr-sr-x. 1 root root 24704 Feb 27  2014 /bin/watch 

# 在student用户上
[student@localhost ~]$ watch -n 1 date

# 在root用户上
[root@localhost Desktop]# ps ax -o comm,user,group | grep watch     # 此时watch的所有人改变为student
	watchdog/0      root     root
	abrt-watch-log  root     root
	abrt-watch-log  root     root
	watch           student  root
[root@localhost Desktop]# chmod u+s /bin/watch     # 给watch赋予u+s权限
[root@localhost Desktop]# ls -l /bin/watch       # 可以看到u上多了一个s权限
	-rwsr-sr-x. 1 root root 24704 Feb 27  2014 /bin/watch

# 在student用户上
[student@localhost ~]$ watch -n 1 date

# 在root用户上
[root@localhost Desktop]# ps ax -o comm,user,group | grep watch   # 
	watchdog/0      root     root
	abrt-watch-log  root     root
	abrt-watch-log  root     root
	watch           root     root

在这里插入图片描述

sudo原理
# suid+sgid的结合使用

# 练习(特殊权限的结合使用):
	实现当目录的所在组对文件没有w权限时,不能删除目录里的文件

[root@localhost Desktop]# useradd -s /sbin/nologin norm   # 建立一个系统用户
[root@localhost Desktop]# ls -l /bin/rm
	-rwxr-xr-x. 1 root root 62808 Jan 24  2014 /bin/rm
[root@localhost Desktop]# chown norm.norm /bin/rm   # 使rm命令属于系统用户
[root@localhost Desktop]# ls -l /bin/rm
	-rwxr-xr-x. 1 norm norm 62808 Jan 24  2014 /bin/rm
[root@localhost Desktop]# chmod 6755 /bin/rm   #给rm命令赋予冒险位和强制位权限
[root@localhost Desktop]# ls -l /bin/rm
	-rwsr-sr-x. 1 norm norm 62808 Jan 24  2014 /bin/rm
[root@localhost Desktop]# chmod 777 /mnt
[root@localhost Desktop]# ls -ld /mnt
	drwxrwxrwx. 2 root root 6 Oct  2 02:39 /mnt
[root@localhost Desktop]# chmod 775 /mnt
[root@localhost Desktop]# ls -ld /mnt
	drwxr-xr-x. 2 root root 17 Oct  2 03:11 /mnt
[root@localhost Desktop]# touch /mnt/file
[root@localhost Desktop]# ls /mntfile
[root@localhost Desktop]# rm -fr /mnt/file
	rm: cannot remove ‘/mnt/file’: Permission denied 

# 做完实验记得还原环境
[root@localhost Desktop]# chown root.root /bin/rm
[root@localhost Desktop]# ls -l /bin/rm
	-rwxr-xr-x. 1 root root 62808 Jan 24  2014 /bin/rm
7.acl权限列表
(1)作用	
	让特定的用户对特定的文件拥有特定权限

注:当acl权限列表开启时,默认权限不生效,关闭时,默认权限生效

(2)acl列表查看

ls -l file                 # 查看权限列表	
-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访问控制列表的管理	  

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@localhost Desktop]# setfacl -m u:student:rw /mnt/file
[root@localhost Desktop]# ls -lR /mnt  # /mnt目录对root用户的权限为rw,对student组的权限为rw,对其他人的权限为r,但当acl权限开启时,用ls -lR看到的这些权限都会失效	
	-rw-rw-r--+ 1 root student 0 Oct  2 03:11 file
[root@localhost Desktop]# getfacl /mnt/file 	
	getfacl: Removing leading '/' from absolute path names	
	# file: mnt/file	
	# owner: root	
	# group: student	
	user::rw-	
	user:student:rw-	
	group::r--	
	mask::rw- 	
	other::r--
	[root@localhost Desktop]# echo hello > /mnt/file
[root@localhost Desktop]# cat /mnt/file	
	hello
[root@localhost Desktop]# chmod 664 /mnt/file
[root@localhost Desktop]# su - tom
[tom@localhost ~]$ echo world > /mnt/file	
	-bash: /mnt/file: Permission denied
[tom@desktop ~]$ logout 
[root@localhost Desktop]# setfacl -m u:tom:0 /mnt/file  # 设定tom用户对/mnt/file的权限为0,不用写成000
[root@localhost Desktop]# getfacl /mnt/file

在这里插入图片描述

[root@localhost Desktop]# setfacl -x u:student /mnt/file  #从acl列表中删除student用户的权限
[root@localhost Desktop]# getfacl /mnt/file		

在这里插入图片描述

[root@localhost Desktop]# setfacl -m g:shengchan:rw /mnt/file  # 设定shengchan组对/mnt/file拥有rw权限
[root@localhost Desktop]# getfacl /mnt/file

在这里插入图片描述

[root@localhost Desktop]# setfacl -x u:tom /mnt/file  # 从acl列表删除tom用户对/mnt/file文件的权限
[root@localhost Desktop]# getfacl /mnt/file

在这里插入图片描述

# 恢复环境

[root@localhost Desktop]# setfacl -x g:shengchan /mnt/file
[root@localhost Desktop]# setfacl -b /mnt/file  # 关闭/mnt/file的acl列表	
	-rw-rw-r-- 1 root root 0 Oct  2 03:27 file

(4)mask值  
	在权限列表中mask表示能生效的权力值,当用chmod减小开启acl的文件权限时mask值会发生改变 
chmod g-w westos  

[root@desktop Desktop]# ls -lR /mnt
	-rw-rwxr--+ 1 root root 6 Oct  3 10:49 file
[root@desktop Desktop]# getfacl /mnt/file	

在这里插入图片描述

[root@localhost Desktop]# chmod g-rwx /mnt/file
[root@localhost Desktop]# ls -lR /mnt
-rw----r--+ 1 root root 6 Oct  3 10:49 file
[root@localhost Desktop]# getfacl /mnt/file
	getfacl: Removing leading '/' from absolute path names
	# file: mnt/file# owner: root
	# group: rootuser::rw-user:student:rwx     #effective:---  # student用户的有效权限变为---
	group::r--			           #effective:---  # group的有效权限变为---
	mask::---               # mask值变为空
	other::r-- 
[root@localhost Desktop]# setfacl -m m:rwx /mnt/file   # 恢复mask值,m:rwx中m代表mask
[root@localhost Desktop]# getfacl /mnt/file	
	getfacl: Removing leading '/' from absolute path names	
	# file: mnt/file	
	# owner: root	
	# group: root	
	user::rw-	
	user:student:rwx	
	group::---	
	mask::rwx	
	other::---		

(5)acl的默认权限设定

	acl默认权限只针对目录设定"acl权限只针对设定完成之后新建立的文件或目录生效,而已经存在的文件是不会继承默认权限" 
	
	设定方式:setfacl -m d:u:student:rwx /mnt/westos

[root@localhost Desktop]# mkdir /mnt/test
[root@localhost Desktop]# chmod 775 /mnt/test
[root@localhost Desktop]# touch /mnt/test/file
[root@localhost Desktop]# setfacl -m u:student:rwx /mnt/test
[root@localhost Desktop]# ls -lR /mnt	
-rw-rwx---+ 1 root root  0 Oct  2 03:27 file	
drwxrwxr-x+ 2 root root 17 Oct  2 04:07 test
[root@localhost Desktop]# getfacl /mnt/test	
getfacl: Removing leading '/' from absolute path names	
# file: mnt/test	
# owner: root	
# group: root	
user::rwx
user:student:rwx
group::rwx	
mask::rwx	
other::r-x
[root@localhost Desktop]# su - student 
[student@localhost ~]$ touch /mnt/test/file1
[student@desktop ~]$ ls -lR /mnt

在这里插入图片描述

[root@localhost Desktop]# mkdir /mnt/test/linux
[root@localhost Desktop]# cd /mnt/test/
[root@localhost test]# ls	file  linux
[root@localhost test]# setfacl -m d:u:student:rwx /mnt/test
[root@localhost test]# mkdir /mnt/test/hello
[root@localhost test]# getfacl /mnt/test/hello/	
	getfacl: Removing leading '/' from absolute path names	
	# file: mnt/test/hello/	
	# owner: root	
	# group: root
	
	# 默认权限	
	user::rwx	
	user:student:rwx	
	group::rwx	mask::rwx	
	other::r-x
	
	# hello目录继承了test的权限,但继承权限的前提是,默认权限必须存在,继承的权限才生效	
	default:user::rwx   	
	default:user:student:rwx	
	default:group::rwx	
	default:mask::rwx	
	default:other::r-x
	[root@localhost test]# setfacl -x u:student /mnt/test

提问:其他用户对文件不可写但wq!保存成功是否说明它对文件可写?

 [root@localhost mnt]# ls -li /mnt/file    
 [root@localhost test]# setfacl -b /mnt/test/file*
 [root@localhost test]# cd /mnt
 [root@localhost mnt]# ls	test
 [root@desktop test]# chmod 777 /mnt/test/
 [root@localhost mnt]# cd /test
 [root@localhost test]# rm -fr *
 [root@localhost test]# ls
 [root@localhost test]# touch file{1..3}
 [root@localhost test]# ls	
	 file1  file2  file3
 [root@localhost test]# echo hello > /mnt/test/file1
 [root@localhost test]# ll
	 total 4-rw-rw-r-- 1 root root 6 Oct  2 04:46 file1
	 -rw-rw-r-- 1 root root 0 Oct  2 04:44 file2
	 -rw-rw-r-- 1 root root 0 Oct  2 04:44 file3
 [root@localhost test]# cat file1
	 hello
 [root@localhost test]# chmod 700 /mnt/test/*
 [root@localhost test]# ll
	 total 4
	 -rwx------ 1 root root 6 Oct  2 04:46 file1
	 -rwx------ 1 root root 0 Oct  2 04:44 file2
	 -rwx------ 1 root root 0 Oct  2 04:44 file3
 [root@localhost test]# su - student
 [student@localhost ~]$ cd /mnt/test
 [student@localhost test]$ ll
	 total 4
	 -rwx------ 1 root root 6 Oct  2 04:46 file1
	 -rwx------ 1 root root 0 Oct  2 04:44 file2
	 -rwx------ 1 root root 0 Oct  2 04:44 file3
 [root@desktop test]# su - student
	 Last login: Wed Oct  3 11:35:46 EDT 2018 on pts/0
 [student@localhost ~]$ cd /mnt/test/
 [student@localhost test]$ ls
	file1  file2  file3
 [student@localhost test]$ touch file4
 [student@localhost test]$ ll     # 谁建立的文件,文件就属于谁
	 total 4-rwx------ 1 root    root    6 Oct  3 11:28 file1
	 -rwx------ 1 root    root    0 Oct  3 11:27 file2
	 -rwx------ 1 root    root    0 Oct  3 11:27 file3
	 -rw-rw-r-- 1 student student 0 Oct  3 11:36 file4
 [student@desktop test]$ ls -li /mnt/test/file1   # 记录file1的id号
	 20215088 -rwx------ 1 root root 6 Oct  3 11:41 /mnt/test/file1
 [student@localhost test]$ vim file1   #wq!强制保存退出
	 world
 [student@localhost test]$ ll
	 total 4
	 -rwx------ 1 student student 6 Oct  3 11:36 file1
	 -rwx------ 1 root    root    0 Oct  3 11:27 file2
	 -rwx------ 1 root    root    0 Oct  3 11:27 file3
	 -rw-rw-r-- 1 student student 0 Oct  3 11:36 file4						
[student@desktop test]$ ls -li /mnt/test/file1  # 发现wq!强制退出去后,并不是改变了原文件的内容,而是直接改变了文件本身,删除了原来的文件,重新生成了一个名字与原来文件相同的文件,即文件名相同但这是两个不同的文件,也就说明了用户对文件不可写! 
	 20215092 -rwx------ 1 student student 6 Oct  3 11:42 /mnt/test/file1

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/wzt888_/article/details/82908013