基本权限UGO
权限对象:
- 属主:文件的主人:u
- 属组:文件属于组员权限:g
- 其他人:除了主人和组员之外的用户:other
- 特殊对象:所有人(包含主人,组员,其他):all 全部
权限类型:
- 读:r=4
- 写:w=2
- 执行: x=1
查看权限
[root@localhost ~]#ls -l /root/1.txt
-rw-r--r--. 1 root root 179 5月 25 14:27 /root/1.txt
- -文件类型
- rw-主人的权限,属主
- r--属组的权限,
- r--其他人的权限
- .权限的扩展
- 1文件链接(第七章文件链接)
- root文件的属主
- root文件的属组
- 179大小
- 5月 25 14:27文件最后的修改时间
- /root/1.txt 文件的名和路径
权限命令
修改权限
[root@localhost tmp]# chmod a=rwx file1 //所有人等于读写执行
[root@localhost tmp]# chmod a=- file1 //所有人没有权限
[root@localhost tmp]# chmod ug=rw,o=r file1 //属主属组等于读写,其他人只读
使用数字
4读 2写 1执行
[root@localhost ~]# chmod 644 file1
[root@localhost ~]# ll file1
-rw-r--r-- 1 alice it 17 10-25 16:45 file1
设置属主属组
[root@localhost ~]#chown alice.hr file1 //改属主、属组
[root@localhost ~]#chown alice file1 //只改属主
[root@localhost ~]#chown .hr file1 //只改属组
- -R 针对目录中所有的文件。
- chgrp: 设置一个文件属于哪个组,属组
[root@localhost ~]# chgrp it file1 //改文件属组
[root@localhost ~]# chgrp -R it dir1 //改文件属组,-R是递归的意思
基本权限 ACL
配置ACL权限
[root@localhost ~]# setfacl -m u:alice:rw /home/test.txt
[root@localhost ~]# setfacl -m u:jack:- /home/test.txt
[root@localhost ~]# setfacl -m o::rw /home/test.txt
查看ACL权限
[root@localhost ~]# getfacl /home/test.txt
getfacl: Removing leading '/' from absolute path names
查看文件权限,删除了根路径。
# file: home/test.txt 文件名
# owner: root 属主:root
# group: root 属组:root
user::rwx 用户:属主:rwx
user:alice:rw- 用户:alice:rw-
user:jack:--- 用户:jack:---
group::rwx 组:属组:rwx
mask::rwx 掩码::rwx
other::rwx other:其他人:rwx
删除ACL权限
[root@localhost ~]# setfacl -x g:hr /home/test.txt //删除组hr的acl权限
[root@localhost ~]# setfacl -b /home/test.txt //删除所有acl权限
特殊权限
特殊位 suid
suid针对文件/程序时,具备临时获得属主的权限。
- sgid:针对目录授权,可以使目录下新建文件,继承目录的属组权限。
- stick:针对目录设置,目录内的文件,仅属主能删除
chmod o+t /tmp/1.txt //切换用户,只能删除自己的文件
[root@localhost ~]# ll /root/file1.txt
-rw-r--r-- 1 root root 4 7月 27 14:14 /root/file1.txt
[root@localhost ~]#su - alice
[alice@localhost ~]$ cat /root/file1.txt
cat: /root/file1.txt: 权限不够
分析:root运行是超管的权限,普通用户运行时是普通用户的权限。
root /usr/bin/cat (root) /root/file1.txt OK
alice /usr/bin/cat (alice) /root/file1.txt
为cat程序添加上suid权限。
[root@localhost ~]# ll /usr/bin/cat
-rwxr-xr-x. 1 root root 54080 8月 20 2019 /usr/bin/cat
[root@localhost ~]# chmod u+s /usr/bin/cat
[root@localhost ~]# ll /usr/bin/cat
-rwsr-xr-x. 1 root root 54080 8月 20 2019 /usr/bin/cat
结果,普通用户,看到了root的内容。这个行为很危险
删除suid特殊权限
[root@localhost ~]# chmod u-s /usr/bin/cat
文件属性chattr
用途:常用于锁定某个文件,拒绝修改。
[root@localhost ~]# touch file100
[root@localhost ~]# lsattr file100
-------------- file100
[root@localhost ~]# chattr +i file100 //不能更改,重命名,删除
[root@localhost ~]# lsattr file100
----i--------- file100
进程掩码 umask
示例1: 在shell进程中创建文件,先查看当前用户的umask权限
[root@localhost ~]# umask
0022
[root@localhost ~]# touch file800
[root@localhost ~]# mkdir dir800
[root@localhost ~]# ll -d dir800 file800
drwxr-xr-x. 2 root root 4096 3月 11 19:40 dir800
-rw-r--r--. 1 root root 0 3月 11 19:40 file800
示例2:修改shell umask值(临时)
[root@localhost ~]# umask 000
[root@localhost ~]# mkdir dir900
[root@localhost ~]# touch file900
[root@localhost ~]# ll -d dir900 file900
drwxrwxrwx. 2 root root 4096 3月 11 19:44 dir900
-rw-rw-rw-. 1 root root 0 3月 11 19:44 file900