**
文件与目录权限管理
**
一.查看【文件/目录】信息
#ls -l或ll 文件名
#ls -d或ll -d 目录名
二.修改属主与属组
#chown 用户名 文件名/目录名 // 修改文件与目录的属主
#chown .组名 文件名/目录名 // 修改文件与目录的属组
#chown :组名 文件名/目录名 // 修改文件与目录的属组
#chown 用户名:组名 文件名/目录名 // 修改文件与目录的属主属组
三.权限管理
1.基本权限:ugo=主/组/其它人 rwx=421
2.查看基本权限:
#stat 文件名/目录名
#getfacl 文件名/目录名
#ll 文件名 // 查看文件权限
#ll -d 文件名 // 查看目录权限
四.修改权限
# chmod a/u/g/o/ +/- r/w/x 文件名/目录名
举例
# chmod g-r a
# chmod o+w a
# chmod u-w,g+w,o+x a
# chmod a-r a.txt a表示ugo
# chmod u+r+w-x a
少用:
# chmod +x a
# chmod -x a
五.隐藏权限
1.设置隐藏权限
#chattr +a 文件名/目录名 // 只能向文件中添加数据,而不能删除,多用于服务器日志文件安全,只有root可设定
#chattr +i 文件名/目录名 // 设定文件不能被删除、改名、设定链接关系,同时不能写入或新增内容,但可>>追加
#chattr -a 文件名/目录名
#chattr -i 文件名/目录名
2.显示隐藏权限
#lsattr 文件名/目录名
六.高级权限(查看命令的绝对路径:which 命令名称)
1.设置Suid // 只对命令/可执行文件有效
#which 命令名称 //查看命令的完整路径
#chmod u+s+x 命令/可执行文件 // 设置此权限后,普通用户在执行命令和可执行文件时拥有root身份
2.设置Sgid
#chmod g+s 文件名/目录名 //设置此权限后,在此目录下创建的所有文件或目录都会继承此目录的家目录
3.设置sticky
#chmod o+t 目录名 //设置此权限后,在此目录下任何用户只删除自已创建的文件,其它人的不能删除,只对目录有效
同样也可使用数字方式表示
SUID=4 ----->>>用户的x位
SGID=2 ----->>>组的x为
STICKY=1 ----->>>other的x位
七.设置facl权限
#getfacl 文件/目录 //查看文件或目录的ACL权限
#setfacl -m u:username:mode FILE/DIR //设定某用户的某权限 mode代表权限,若不给某用户任何权限 setfacl -m u:username:000 FILE/DIR
#setfacl -m g:groupname:mode FILE/DIR //设定某组的某权限
#setfacl -b FILE/DIR //删除所有用户的ACL权限
#setfacl -x u:username FILE/DIR //取消某用户的某权限
#setfacl -x g:groupname FILE/DIR //取消某组的某权限
FACL 文件系统的权限设置
FACL 文件系统的权限设置(file access contrl list) 文件访问控制列表
针对Unix系统权限机制的不足,一个名为POSIX ACL的全新权限机制诞生了,目的就是为了给各Unix系统之间制定一个兼容的ACL标准,使得各操作系统之间使用统一的接口。ACL为现有权限机制的延伸,在现有机制的三个基本设定(owner、group、other)的基础上加入了对某指定使用者或群组的存取权限设定。
在Linux Kernel 2.6上已经正式支持POSIX ACL,常用的档案系统(如:ext2,ext3,xfs,jfs以及ReiserFS)都能使用ACL。当然,在编译kernel时需要启动ACL。
相关的kernel option:
CONFIG_FS_POSIX_ACL
CONFIG_EXT3_POSIX_ACL
CONFIG_EXT2_POSIX_ACL
rules:
user:(uid/name):(perms) 指定某位使用者的权限
group:(gid/name):(perms) 指定某一群组的权限
other::(perms) 指定其它使用者的权限
mask::(perms) 设定有效的权限屏蔽
ACL可以对某个文件设置该文件具体的某些用户的权限,意思就是通过ACL可以对一个文件权限做扩展,可以不同的用户对某个文件有不同的权限。
option:
-m 新增或修改ACL中的规则
-x 移出ACL中的规则
getfacl <文件名> 获取文件的访问控制信息
setfacl设置文件的acl -m 修改文件的acl -x 取消对文件的设置
setfacl –m u:用户名:权限 文件名
setfacl –m g:组名:权限 文件名
要设定预设型ACL只需在每个规则前加上"default:" 。
例如: setfacl -m default:user::rw /home/alex
或简写为: setfacl -m d:u::rw /home/alex
[root@haha ~]# getfacl hello_world 查看文件的acl权限
# file: hello_world
# owner: root
# group: root
user::rw-
group::r--
other::r--
[root@haha ~]# setfacl -m student:rwx hello_world 让用户student拥有rwx权限
[root@haha ~]# getfacl hello_world
# file: hello_world
# owner: root
# group: root
user::rw-
user:student:rwx
group::r--
mask::rwx
other::r--
[root@haha ~]# setfacl -m g:student:rx hello_world 让组student拥有rwx权限
[root@haha ~]# getfacl hello_world
# file: hello_world
# owner: root
# group: root
user::rw-
user:student:rwx
group::r--
group:student:r-x
mask::rwx
other::r--
[root@haha ~]# ll hello_world
-rw-rwxr—+ 1 root root 0 07-21 13:48 hello_world
[root@haha ~]# setfacl -x student hello_world 解除student用户对文件的acl权限
[root@haha ~]# setfacl -x g:student hello_world 解除student组对文件的权限
(撤消ACL操作: 对用户直接加用户名字就可以了 对组,在前面加g:组名)
[root@haha ~]# getfacl hello_world
# file: hello_world
# owner: root
# group: root
user::rw-
group::r--
mask::r--
other::r--
[root@haha ~]# getfacl hello_world
# file: hello_world
# owner: root
# group: root
user::rw-
user:student:rwx
group::r--
group:student:rwx
mask::rwx
other::r--
[root@haha ~]# setfacl -b hello_world 删除所有扩展的acl规则
[root@haha ~]# getfacl hello_world
# file: hello_world
# owner: root
# group: root
user::rw-
group::r--
other::r--
[root@haha lianxi]# getfacl a
# file: a
# owner: root
# group: root
user::rw-
user:wl:rwx
group::r--
group:wl:rwx
mask::rwx
other::r--
收回所有用户和所有组的写的权限
[root@haha lianxi]# setfacl -m m::r-x a
[root@haha lianxi]# getfacl a
# file: a
# owner: root
# group: root
user::rw-
user:wl:rwx #effective:r-x
group::r--
group:wl:rwx #effective:r-x
mask::r-x
other::r--
让子目录下的文件和文件夹继承
[root@haha lianxi]# setfacl -m d:u:wl:rwx,g:wl:rwx b (其中d表示defaults)
[root@haha lianxi]# getfacl b
# file: b
# owner: root
# group: root
user::rwx
group::r-x
group:wl:rwx
mask::rwx
other::r-x
default:user::rwx
default:user:wl:rwx
default:group::r-x
default:mask::rwx
default:other::r-x
[root@haha lianxi]# cd b/
[root@haha b]# mkdir c;touch d
[root@haha b]# ls
c d
[root@haha b]# getfacl c
# file: c
# owner: root
# group: root
user::rwx
user:wl:rwx
group::r-x
mask::rwx
other::r-x
default:user::rwx
default:user:wl:rwx
default:group::r-x
default:mask::rwx
default:other::r-x
[root@haha b]# getfacl d 可能由于文件本身的默认权限666问题,文件没有继承x权限
# file: d
# owner: root
# group: root
user::rw-
user:wl:rwx #effective:rw-
group::r-x #effective:r--
mask::rw-
other::r--
getfacl a |setfacl —set-file=- b
这是个很有意思的命令,它可以让一个文件的权限直接复制改成那一个文件的权限
[root@haha lianxi]# ll
总计 4
-rw-rwxr--+ 1 root root 0 07-21 20:34 a
-rw-r--r-- 1 root root 0 07-21 20:34 b
[root@haha lianxi]# getfacl a
# file: a
# owner: root
# group: root
user::rw-
user:wl:rwx
group::r--
group:wl:rwx
mask::rwx
other::r--
[root@haha lianxi]# getfacl a |setfacl --set-file=- b
[root@haha lianxi]# ll
总计 8
-rw-rwxr--+ 1 root root 0 07-21 20:34 a
-rw-rwxr--+ 1 root root 0 07-21 20:34 b
[root@haha lianxi]# getfacl b
# file: b
# owner: root
# group: root
user::rw-
user:wl:rwx
group::r--
group:wl:rwx
mask::rwx
other::r--