用户的权限
一、基本权限UGO:
- 1.权限对象: 属主: u 属组: g 其他人: o 所有人:a(u+g+o)
- 权限类型:读:r=4 写:w=2 执行: x=1
- 查看权限ls:
[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)使用符号:u用户 g组 o其他 r读 w写 x执行
语法: chmod 对象(u/g/o/a)赋值符(+/-/=)权限类型(r/w/x) 文件/目录
案例一:
了解普通用户的权限:
[root@localhost ~]# cd /tmp
[root@localhost ~]# touch file1
[root@localhost tmp]# ll file1
-rw-r--r--. 1 root root 0 4月 13 20:49 file1
权限 属主 属组 文件
编写程序:
[root@localhost tmp]#vim file1
echo "hello 2020"
read -p "请输入您的姓名:" name
echo "哈哈 $name 是大笨蛋"
增加执行权限:
[root@localhost tmp]# chmod u+x file1
运行测试,成功:
[root@localhost tmp]# ./file1
hello 2020
请输入您的姓名:aaa
aaa 是大笨蛋
去除权限,运行失败
[root@localhost tmp]# chmod u-x file1
[root@localhost tmp]# ./file1
-bash: ./file1: 权限不够
案例二:
[root@localhost tmp]# chmod a=rwx file1 //所有人等于读写执行
[root@localhost tmp]# chmod a=- file1 //所有人没有权限
[root@localhost tmp]# chmod ug=rw,o=r file1 //属主属组等于读写,其他人只读
[root@localhost tmp]# ll file1 //以长模式方式查看文件权限
-rw-rw-r-- 1 alice it 17 10-25 16:45 file1 //显示的结果
(2)使用数字
读4 写2 执行1
[root@localhost ~]# chmod 644 file1
[root@localhost ~]# ll file1
-rw-r--r-- 1 alice it 17 10-25 16:45 file1
5.更改属主、属组
(1)chown: 设置一个文件属于谁,属主
语法: chown 用户名.组名 文件
[root@localhost ~]# chown alice.hr file1
[root@localhost ~]# chown alice file1
[root@localhost ~]# chown .hr file1
(2)chgrp: 设置一个文件属于哪个组,属组
语法: chgrp 组名 文件 -R是递归的意思
[root@localhost ~]# chgrp it file1 //改文件属组(it)
[root@localhost ~]# chgrp -R it dir1 //改目录及文件属组
6.整理:
(1)修改文件的权限(完成授权一种方法)
命令:chmod(ch改变mod设置)
语法:chmod 授权 文件
示例:chmod u+x file1.txt
示例:chmod 764 file1.txt
(2)修改文件的属主和属组(完成授权另一种方法)
命令:chown(ch改变own所有者)
语法:chown 属主.属组 文件/文件夹
(3)修改文件的属主和属组(完成授权另一种方法)
命令:chown(ch改变own所有者)
语法:chown 属主.属组 文件/文件夹
语法:chown 属主 文件
语法:chown .属组 文件
二、基本权限 ACL
1.区别:
ACL文件权限管理: 设置不同用户,不同的基本权限(r、w、x)。对象数量不同。
UGO设置基本权限: 只能一个用户,一个组和其他人。
2.语法:
命令 设置 用户或组:用户名:权限 文件对象
setfacl -m u:alice:rw /home/test.txt
3.用法:
- 准备文件:
[root@qianfeng ~]# touch /home/test.txt //创建文件
[root@qianfeng ~]# ll /home/test.txt //查看文件基本信息
-rw-r--r-- 1 root root 0 10-26 13:59 /home/test.txt
- 查看文件的详细用户权限
[root@qianfeng ~]# getfacl /home/test.txt
- 设置用户alice,jack权限
[root@qianfeng ~]# setfacl -m u:alice:rw /home/test.txt
[root@qianfeng ~]# setfacl -m u:jack:- /home/test.txt
- 查看ACL
[root@qianfeng ~]# getfacl /home/test.txt
- 删除ACL
[root@qianfeng ~]# setfacl -m g:hr:r /home/test.txt //增加hr组对test.txt
文件读取的权限
[root@qianfeng ~]# setfacl -x g:hr /home/test.txt //删除hr组的权限
[root@qianfeng ~]# setfacl -b /home/test.txt //删除所有
三、特殊权限
特殊位 suid:suid针对文件/程序时,具备临时获得属主的权限。
[root@qianfeng ~]# ll /usr/bin/cat
-rwxr-xr-x. 1 root root 54080 8月 20 2019 /usr/bin/cat
[root@qianfeng ~]# chmod u+s /usr/bin/cat //增加suid权限
[root@qianfeng ~]# ll /usr/bin/cat
-rwsr-xr-x. 1 root root 54080 8月 20 2019 /usr/bin/cat
[alice@qianfeng ~]$ cat /root/file1.txt //普通用户可以看root的内容
[root@qianfeng ~]# chmod u-s /usr/bin/cat //去除suid权限
四、文件属性chattr
常用于锁定某个文件,拒绝修改。(超管无法删除,可追加)
1.先创建新文件进行对比。查看默认权限。
[root@qianfeng ~]# touch file100 //创建文件
[root@qianfeng ~]# lsattr file100 //使用 lsattr 命令来显示文件属性
-------------- file100
2.加上不能删除的属性。
[root@qianfeng ~]# chattr +i file100 //2 加上不能删除的属性。
3.查看不同属性
[root@qianfeng ~]# lsattr file100
----i--------- file100
4.尝试删除
[root@qianfeng ~]# rm -rf file100
5 将属性还原
[root@qianfeng ~]# chattr -i file100
四、进程掩码 umask
概述:新建文件、目录的默认权限会受到umask的影响,umask表示要减掉的权限。
示例1:观察系统默认掩码
在shell进程中创建文件,先查看当前用户的umask权限
[root@qianfeng ~]# umask
0022
[root@qianfeng ~]# touch file800
[root@qianfeng ~]# mkdir dir800
[root@qianfeng ~]# 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@qianfeng ~]# umask 000
[root@qianfeng ~]# mkdir dir900
[root@qianfeng ~]# touch file900
[root@qianfeng ~]# 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