文章目录
基本权限UGO
权限的三类对象:
- 属主:u
- 属组:g
- 其他人:o
- 特殊对象:所有人:a(u+g+o)
权限的三种类型
- 读: r = 4
- 写: w = 2
- 执行:x = 1
设置权限
更改权限chmod
- 使用符号
语法:chmod 对象(u/g/o/a)赋值符(+/-/=)权限类型(r/w/x) 文件/目录
chmod -R u+r 1.txt
其中-R是递归,文件夹下面所有文件都包括
chmod a=rx 1.txt
意思是给所有人(a)赋予r-x权限,不能用a=r-x
+是赋予,-是剥夺,=是覆盖 - 使用数字
chmod 671 file1.txt
代表file1.txt文件的权限是rw-rwx–x
[root@192 tmp]# ll file1
-rw-r--r--. 1 root root 0 1月 18 17:17 file1
-rw-r--r--.
共11个,第1个是类型。后9个是权限rwx(u)rwx(g)rwx(o)
1
链接
root
属主
root
属组
0
大小 单位字节
1月 18 17:17
创建时间
file1
文件名
更改属主、属组
chown
设置一个文件属于谁,属主change owner
chown 用户名.组名 文件
chown user01.hr /tmp/file1.txt
chgrp
设置一个文件的属组
chgrp hr /tmp/file1.txt
基本权限ACL
access(访问)control(控制)list(列表) 限制用户对文件的访问 ACL是UGO的补充
ACL文件权限管理:设置不同用户,不同的基本权限(r、w、x)。对象的数量不同
UGO设置基本权限:只能一个用户,一个组和其他人
setfacl(getfacl)
单独添加权限
语法setfacl -m u:user01:rw /home/test.txt
命令 设置 用户或组:用户名:权限 文件对象
[root@192 ~]# setfacl -m u:AAA:rw /home/test.txt
[root@192 ~]# setfacl -m u:BBB:- /home/test.txt
[root@192 ~]# setfacl -m g:hr:rx /home/test.txt
[root@192 ~]# getfacl /home/test.txt
getfacl: Removing leading '/' from absolute path names
# file: home/test.txt
# owner: root
# group: root
user::rw-
user:AAA:rw-
user:BBB:---
group::r--
group:hr:r-x
mask::rwx
other::r--
[root@192 ~]# ll /home/test.txt
-rw-rwxr--+ 1 root root 0 1月 19 11:39 /home/test.txt
//有了+号,代表除了能看到的权限还要其他的
[root@192 ~]# setfacl -m o::rw /home/test.txt
//注意o后面两个:
[root@192 ~]# ll /home/test.txt
-rw-rwxrw-+ 1 root root 0 1月 19 11:39 /home/test.txt
单独删除权限
setfacl -x u:AAA /home/test.txt
删除AAA的
setfacl -b /home/test.txt
删除test里面所有的
watch -n1 ' ls -l /tmp/file1.txt'
每隔一秒查看file1.txt的权限,用ctrl+c退出
特殊权限
- 有一个
rwxrwxrwt
特别的位置 - 如果一个文件很重要,那他万一被超管删除了怎么办。靠特殊的i权限
- 为什么系统创建的文件是644?因为有系统默认权限设计
特殊位suid
suid是针对文件/程序,所设置的一个特别的权限,使调用文件、程序的用户,临时具备属主的能力,个人感觉是针对调用的程序的
[root@192 ~]# ll /usr/bin/cat
-rwxr-xr-x. 1 root root 54080 11月 17 06:24 /usr/bin/cat
[root@192 ~]# su - user01
上一次登录:二 1月 19 16:45:00 CST 2021pts/3 上
[user01@192 ~]$ cat /root/file1.txt
cat: /root/file1.txt: 权限不够
[user01@192 ~]$ exit
登出
[root@192 ~]# chmod u+s /usr/bin/cat
//给命令cat所在的文件提权
[root@192 ~]# ll /usr/bin/cat
-rwsr-xr-x. 1 root root 54080 11月 17 06:24 /usr/bin/cat
[root@192 ~]# su - user01
上一次登录:二 1月 19 16:53:03 CST 2021pts/3 上
[user01@192 ~]$ cat /root/file1.txt
123
[user01@192 ~]$ ll /usr/bin/cat
-rwsr-xr-x. 1 root root 54080 11月 17 06:24 /usr/bin/cat
chmod u-s /usr/bin/cat
用完之后记得删除权限,否则影响后续操作
[root@192 ~]# chmod 7777 file100
[root@192 ~]# ll file100
-rwsrwsrwt. 1 root root 0 1月 19 17:42 file100
[root@192 ~]# chmod 4777 file100
[root@192 ~]# ll file100
-rwsrwxrwx. 1 root root 0 1月 19 17:42 file100
//chmod改权限的时候可以有4位,其中第一位不用记
[root@192 ~]# ll file100
-rw-r--r--. 1 root root 0 1月 19 17:42 file100
[root@192 ~]# chmod u+x file100
[root@192 ~]# ll file100
-rwxr--r--. 1 root root 0 1月 19 17:42 file100
[root@192 ~]# chmod u+s file100
[root@192 ~]# ll file100
-rwsr--r--. 1 root root 0 1月 19 17:42 file100
[root@192 ~]# chmod u-x file100
[root@192 ~]# ll file100
-rwSr--r--. 1 root root 0 1月 19 17:42 file100
//注意s/S的区别
文件属性chattr
lsattr file100
i | 任何用户包括超管都不能删除、修改、重命名 |
---|---|
a | 不能修改,不能删除(通常用于日志) |
[root@192 ~]# rm -rf file100
rm: 无法删除"file100": 不允许的操作
[root@192 ~]# lsattr file100
----i----------- file100
[root@192 ~]# chattr -i file100
[root@192 ~]# rm -rf file100
进程掩码umask
新建文件、目录的默认权限会受到umask的影响,umask表示要减掉的权限
系统的默认掩码是0022
0777-0022=0755 这就是为什么系统创建目录/文件夹的时候权限是755的原因(不是简单的减法,但是可以暂时理解)
0777-0022-0111=0644 系统为了保护自己,所以在创建的文件上。去掉了所有的执行
umask0000
umask是可以改变的