文件目录权限管理

文件目录权限管理操作

一、常规权限

​ r 读取 4

​ w 写入 2

​ x 执行 1

针对文件表示含义:
	1、r     只能查看文件内容(cat/more/less/head/tail/grep)
	2、w		修改文件内容(vim)
	3、x 	针对shell, python脚本文件
针对目录表示含义:
	1、r		只能查看目录下的文件(ls 目录名称)
	2、w		修改该目录文件(touch, mv, rm, cp)
	3、x		可切换进该目录(cd 目录名称)
查看文件权限 
[root@localhost ~]# ls -l /etc/fstab 
-rw-r--r--. 1 root root 465 Dec 15 16:40 /etc/fstab

	九位权限,三位一组
		第一组:属主用户对文件的操作权限
		第二组:属组对文件的操作权限 
		第三组:其他用户
		
查看目录权限
[root@localhost ~]# ls -ld /etc/
drwxr-xr-x. 142 root root 8192 Dec 21 17:11 /etc/

二、修改权限的常用命令

1、修改属主、属组

# chown 用户名.用户组名  文件名称  

[root@localhost ~]# chown martin.caiwu /opt/test/1.txt

[root@localhost ~]# chown martin /opt/test/2.txt 

2、修改属组

# chgrp  用户组名  文件名称  

[root@localhost ~]# chgrp jishu /opt/test/4.txt 

3、修改权限位

# chmod  {augo}{+-=}{rwx}  文件名称
	u: 属主用户
	g: 属组 
	o: 其他用户
	a: 所有
	
[root@localhost ~]# chmod u+x /opt/test/1.jpg 
[root@localhost ~]# chmod g-r,o-r /opt/test/2.jpg
[root@localhost ~]# chmod a+x /opt/test/3.jpg

# chmod nnn  文件名称  
[root@localhost ~]# chmod 600 /opt/test/4.jpg
[root@localhost ~]# chmod 642 /opt/test/5.jpg 

三、facl 文件访问控制列表

​ 优势:

​ 针对单个用户、用户组设置

针对单个用户设置权限 
# setfacl -m   u:用户名:权限   文件名称  

[root@localhost ~]# setfacl -m u:userD:rwx /opt/test/file03

[root@localhost ~]# getfacl /opt/test/file03 
getfacl: Removing leading '/' from absolute path names
# file: opt/test/file03
# owner: userA
# group: userC
user::rw-
user:userD:rwx
group::r--
mask::rwx
other::r-x
针对单个用户组设置 

# setfacl -m g:用户组名:权限   文件名称  
删除facl权限 
[root@localhost ~]# setfacl -x u:userD /opt/test/file03 
[root@localhost ~]# getfacl /opt/test/file03
getfacl: Removing leading '/' from absolute path names
# file: opt/test/file03
# owner: userA
# group: userC
user::rw-
group::r--
mask::r--
other::r-x
chmod, chown, chgrp, setfacl共同选项:
	-R  递归修改 
	
[root@localhost ~]# chown -R martin.caiwu /opt/linux/
	

四、特殊权限

​ suid 4

​ sgid 2

​ sticky bit 1

1、suid

​ 针对可执行文件设置

​ 当普通用户运行文件期间,会临时获取到该文件属主对操作系统的操作权限

[root@localhost ~]# chmod u+s /usr/bin/passwd 

[root@localhost ~]# ls -l /usr/bin/passwd
-rwsr-xr-x. 1 root root 27832 Jun 10  2014 /usr/bin/passwd

2、sgid

​ 针对目录设置

​ 目录拥有sgid权限后,在该目录创建的文件会自动继承目录的属组

[root@localhost ~]# chmod g+s /opt/testdir/

[root@localhost ~]# ls -ldh /opt/testdir/
drwxr-srwx. 2 root caiwu 32 Dec 21 21:58 /opt/testdir/

3、sticky bit

​ 针对目录设置

​ 作用:

​ 防止普通用户互删文件

​ 只有文件属主、目录属主、root用户可删除该目录下的文件

[root@localhost ~]# chmod o+t /opt/bj/
[root@localhost ~]# 
[root@localhost ~]# ls -ldh /opt/bj/
drwxr-xrwt. 2 root root 6 Dec 21 22:06 /opt/bj/

五、文件属性

1、查看文件属性

[root@localhost ~]# lsattr /etc/fstab  
---------------- /etc/fstab

2、设置文件属性

# chattr +属性名称 文件名称  

常用属性: 

1) i    文件内容不允许修改,不允许删除

[root@localhost ~]# chattr +i /opt/file01
[root@localhost ~]# lsattr /opt/file01
----i----------- /opt/file01

2) a  仅允许向文件追加内容, 不允许删除 
[root@localhost ~]# rm -rf /opt/file02 
rm: cannot remove ‘/opt/file02’: Operation not permitted

[root@localhost ~]# echo "abc" >> /opt/file02 
[root@localhost ~]# echo "kkkk" >> /opt/file02 

六、umask

​ 权限的反掩码

查看root用户的umask值  
[root@localhost ~]# umask 
0022

[root@localhost ~]# umask 0000
[root@localhost ~]# umask
0000
[root@localhost ~]# touch AAA
[root@localhost ~]# ls -l AAA
-rw-rw-rw-. 1 root root 0 Dec 21 23:54 AAA
[root@localhost ~]# 
[root@localhost ~]# mkdir BBBB
[root@localhost ~]# ls -ldh BBBB/
drwxrwxrwx. 2 root root 6 Dec 21 23:54 BBBB/
[root@localhost ~]# 
[root@localhost ~]# umask 022
[root@localhost ~]# umask 
0022
查看普通用户的umask值 
[martin@localhost ~]$ umask 
0002
posted @ 2023-03-25 10:32  nhxuan  阅读(73)  评论(0)    收藏  举报