Linux文件目录权限管理

常规权限:
r read 读取 4
w write 写入 2
x execute 执行 1

文件:
r 查看文件内容 (cat/ more/ less/ head/ tail/ grep)
w 编辑文件内容(vim)
x shell/python脚本

目录:
r 查看目录下的文件(ls / tmp)
w 修改目录下的文件(新建、删除、mv)
x 切换目录(cd)

查看文件权限

[root@tan ~]# ls -l /etc/fstab
-rw-r–r--. 1 root root 465 2月 26 00:51 /etc/fstab

查看目录权限

[root@tan ~]# ls -ld /etc/
drwxr-xr-x. 77 root root 8192 2月 27 14:37 /etc/

设置文件目录的权限

  1. chmod

chmod {augo} {±=} {rwx} 文件名称 {}表示或者意思

    a     all         所有 					 
	u     user     属主用户
    g     group   属组
    o     other	   其他

chmod a+x /tmp/1.txt
chmod u-x,o+r /tmp/2.txt

给1.txt所有添加X(1)权限
[root@tan ~]# chmod a+x /test/1.txt
[root@tan ~]# ls -l /test/1.txt
-rwxr-xr-x. 1 root root 0 2月 27 15:17 /test/1.txt

把2.txt属组和其他的r(4)权限删除
[root@tan ~]# chmod g-r,o-r /test/2.txt
[root@tan ~]# ls -l /test/2.txt
-rw-------. 1 root root 0 2月 27 15:17 /test/2.txt

把3.txt的属组权限改成6;
[root@tan ~]# chmod g=rw /test/3.txt
[root@tan ~]# ls -l /test/3.txt
-rw-rw-r–. 1 root root 0 2月 27 15:17 /test/3.txt

用数字修改权限
chmod nnn 文件名称

[root@tan ~]# chmod 600 /test/4.txt
[root@tan ~]# ls -l /test/4.txt
-rw-------. 1 root root 0 2月 27 15:17 /test/4.txt

  1. 修改文件的属主、属组

chowd 用户名称.用户组名称 文件名称

[root@tan ~]# chown user1.caiwu /test/1.txt

[root@tan ~]# chown user1 /test/2.txt

[root@tan ~]# chown root.caiwu /test/4.txt

仅修改属组:

chgrp 用户组名称 文件名称

[root@tan ~]# chgrp caiwu /test/3.txt

方法2) facl ---- 文件访问控制列表

设置权限:

针对单个用户设置权限

setfacl -m u:用户名:权限 文件名称

[root@tan ~]# setfacl -m u:user4:r /test/3.txt

facl查看权限:
[root@tan ~]# getfacl /test/3.txt
getfacl: Removing leading ‘/’ from absolute path names

file: test/3.txt

owner: user1

group: user3

user::rw-
user:user4:r-- 这个表是单个用户的权限
group::rwx
mask::rwx
other::r-x

针对单个用户组设置权限

setfacl -m g:用户组名称:权限 文件名称

删除facl权限

针对单个用户删除权限

setfacl -x u:用户名 文件名称

[root@tan ~]# setfacl -x u:user4 /test/3.txt

针对单个用户组删除权限

setfacl -x g:用户组名称 文件名称

特殊权限:
suid 4
sgid 2
sticky bit 1

  1. suid

作用:普通用户在执行命令期间,会临时获取到命令属主用户对操作系统的权限

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

设置suid权限

chmod u+s 文件名称

  1. sgid

针对目录设置
昨天:目录拥有sgid权限后,在目录下新创建的文件会继承目录的属组信息

设置sgid权限

chmod g+s 目录名称

[root@tan ~]# chmod g+s /linux/
[root@tan ~]# ls -ldh /linux/
drwxr-sr-x. 2 root caiwu 19 2月 27 16:47 /linux/

  1. sticky bit

针对目录设置
作用:
只有目录下文件的属主用户、目录属主用户及root可删除该文件

[root@tan ~]# ls -ldh /tmp/
drwxrwxrwt. 10 root root 250 2月 27 13:55 /tmp/

设置sticky bit权限

chmod o+t 目录名称

chmod, chown, chgrp, setfacl 共同选项: -R [递归]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值