权限
权限(permission、privilege)
文章目录
5.1 用户捆绑权限
-
再来操作某个对象(文件或文件夹)
-
如:mkdir, cp, rm
[root@localhost ~]# su - jiangda
[jiangda@localhost ~]$ cd /
[jiangda@localhost /]$ mkdir dada
mkdir: 无法创建目录"dada": 权限不够
- **那系统是如何知道你没有权限的呢?如何检查出来的呢?**✨
- 用户启动程序,在进程的pcb里写入这个用户的uid
- 进程(在内存中)读取文件(在disk里)时,内核会检查有没有权限,内核会实现系统调用(操作系统内部的代码,实现某个功能)如果归root用户所有,root的uid为0。
- 内核还会检查有没有磁盘空间,有没有i-node和block
5.2 ll
查看文件的详细信息
ll
的 具体笔记记在第二节linux的基本指令中。
- 访问权限:
- r read 读权限:允许查看文件内容、显示目录列表
- w write 写权限:允许修改文件内容,允许在目录中新建、移动、删除文件或子目录
- x execute 执行权限,允许运行程序、切换目录
- 归属(所有权):
- 文件拥有者(owner):拥有该文件或目录的用户账号
- 属组(group):拥有该文件或目录的组账号
- 其他人(others):除了属主和属组的其他人
5.3 chmod命令
1、格式1:符号
(1)u=rwx
[root@localhost test]# mkdir changsha
[root@localhost test]# ll
总用量 0
drwxr-xr-x. 2 root root 6 2月 21 20:53 changsha
[root@localhost test]# chmod u=rw- changsha
[root@localhost test]# ll
总用量 0
drw-r-xr-x. 2 root root 6 2月 21 20:53 changsha
-
-R,递归修改指定目录下的文件/文件夹的权限(recursive)
[root@localhost test]# cd changsha/ [root@localhost changsha]# mkdir furong [root@localhost changsha]# chmod -R a=r-- changsha/ chmod: 无法访问"changsha/": 没有那个文件或目录 [root@localhost changsha]# cd .. [root@localhost test]# chmod -R a=r-- changsha/ [root@localhost test]# ll 总用量 0 dr--r--r--. 3 root root 20 2月 21 20:55 changsha [root@localhost test]# cd changsha/ [root@localhost changsha]# ll 总用量 0 dr--r--r--. 2 root root 6 2月 21 20:55 furong
(2)o-x或+x
-
./echo.sh
执行当前目录下的脚本文件,需要管理者权限,而bash不需要
[root@lb-1 test]# vim echo.sh [root@lb-1 test]# ll 总用量 4 -rw-r--r-- 1 root root 43 2月 21 20:48 echo.sh -rw-r--r-- 1 root root 0 2月 21 20:46 sc.txt [root@lb-1 test]# ./echo.sh -bash: ./echo.sh: 权限不够 [root@lb-1 test]# chmod +x echo.sh [root@lb-1 test]# ll 总用量 4 -rwxr-xr-x 1 root root 43 2月 21 20:48 echo.sh -rw-r--r-- 1 root root 0 2月 21 20:46 sc.txt [root@lb-1 test]# [root@lb-1 test]# ./echo.sh 执行当前目录下的脚本文件 sanchuang,hello,world [root@lb-1 test]# [root@lb-1 test]# chmod -x echo.sh [root@lb-1 test]# ll 总用量 4 -rw-r--r-- 1 root root 43 2月 21 20:48 echo.sh -rw-r--r-- 1 root root 0 2月 21 20:46 sc.txt [root@lb-1 test]# ./echo.sh -bash: ./echo.sh: 权限不够 [root@lb-1 test]# bash echo.sh sanchuang,hello,world [root@lb-1 test]#
2、格式2:数字
-
4+2+1=7
[root@localhost study]# mkdir changsha [root@localhost study]# ll 总用量 0 drwxr-xr-x. 2 root root 6 2月 22 10:15 changsha [root@localhost study]# chmod 77 changsha/ [root@localhost study]# ll 总用量 0 d---rwxrwx. 2 root root 6 2月 22 10:15 changsha [root@localhost study]# chmod 7 changsha/ [root@localhost study]# ll 总用量 0 d------rwx. 2 root root 6 2月 22 10:15 changsha [root@localhost study]# chmod 777 changsha/ [root@localhost study]# ll 总用量 0 drwxrwxrwx. 2 root root 6 2月 22 10:15 changsha
5.4 chown 更改归属的命令
-
必须是root
-
用户和组必须存在
-
格式:
chown 属主 文件 chown :属组 文件 chown 属主:属组 文件
[root@localhost /]# useradd heyx [root@localhost /]# cd /study [root@localhost study]# chown heyx:heyx changsha [root@localhost study]# ll 总用量 0 drwxrwxrwx. 2 heyx heyx 6 2月 22 10:15 changsha [root@localhost study]# chown jiangda changsha [root@localhost study]# ll 总用量 0 drwxrwxrwx. 2 jiangda heyx 6 2月 22 10:15 changsha
5.5 chgrp 改变文件群组
-
格式:
chgrp 属组 文件
-
必须是root或者是文件的所有者
-
必须是新租的成员
5.6 默认权限
1、在内核级别
- 文件的初始权限666
- 文件夹的初始权限777
2、umask
-
是一个掩码,掩码值到底是多少由/etc/profile文件决定。
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then umask 002 else umask 022 fi #[ int1 -gt int2] 判断int1是否大于int2 #反引号:优先执行里面的语句 #/usr/bin/id:id的路径 #-gn:组名 #-un:用户名
-
查看和修改默认权限值。
-
root用户
[root@localhost ~]# umask 0022 #默认减掉这个值,如某文件初始权限是666,减掉022就是644
- 第一个0:与特殊权限有关
- 后三位002与普通权限(rwx)有关
- 第一个0与用户(user)有关,表示从用户权限减0,也就是权限不变,所以文件的创建者的权限是默认权限(rw)
- 第二个0与组权限(group)有关,表示从组的权限减0.所以群组的权限也保持默认权限(rw)
- 最后一位2与系统中其他用户(others)有关,由于w=2,所以需要从其他用户默认权限(rw)减去2
- 创建文件的最终默认权限为
-rw-rw-r--
。
-
普通用户
[root@localhost ~]# su - jiangda 上一次登录:三 2月 22 10:14:04 CST 2023pts/0 上 [jiangda@localhost ~]$ umask 0002
5.8 chattr 设置文件的隐藏属性
1、i
immutable,不能修改文件里面的内容,连管理员都不能动。
设定文件不能被删除、改名、设定链接关系,同时不能写入或新增内容。i参数对于文件 系统的安全设置有很大帮助。
[root@localhost study]# mkdir liaopeng
[root@localhost study]# ll
总用量 0
drwxr-xr-x. 2 root root 6 2月 26 11:45 liaopeng
[root@localhost study]# chmod 777 liaopeng
[root@localhost study]# ll
总用量 0
drwxrwxrwx. 2 root root 6 2月 26 11:45 liaopeng
[root@localhost study]# chattr +i liaopeng
[root@localhost study]# ll
总用量 0
drwxrwxrwx. 2 root root 6 2月 26 11:45 liaopeng
[root@localhost study]# lsattr -d liaopeng/
----i----------- liaopeng/
[root@localhost study]# cd liaopeng/
[root@localhost liaopeng]# mkdir xiaopeng
mkdir: 无法创建目录"xiaopeng": 权限不够
-
取消i
[root@localhost study]# chattr -i liaopeng/ [root@localhost study]# cd liaopeng/ [root@localhost liaopeng]# mkdir sc [root@localhost liaopeng]# ll 总用量 0 drwxr-xr-x. 2 root root 6 2月 26 11:50 sc
2、-a
即append,设定该参数后,只能向文件中添加数据,而不能删除,多用于服务器日志文 件安全,只有root才能设定这个属性。
[root@localhost study]# chattr +a liaopeng
[root@localhost study]# lsattr -d liaopeng/
-----a---------- liaopeng/
5.9 SET位权限
1、主要用途
为可执行(有x权限的)文件设置,权限字符为“s”
- 其他用户执行该文件时,将拥有属主或属组用户的权限
2、set位权限类型:
- SUID:表示对属主用户增加SET位权限
- SGID:表示对属组内的用户增加SET位权限
3、应用举例
/usr/bin/passwd
:普通用户以root用户的身份,间接更新了shadow文件中自己的密码
[root@localhost bb]# chmod u+s '*_*'
[root@localhost bb]# ll
总用量 0
drws------. 2 root hello 6 2月 26 14:36 *_*
5.10 粘滞位权限
1、主要用途:
-
为公共目录(例如:权限为777的)设置,权限字符为“t”
因为本来某公共目录如果是777,就算里面的文件其他用户没有执行权限,其他用户也能删除。
-
用户不能删除该目录中其他用户的文件
2、/tmp
目录:
任何用户都可以在这个目录下新建文件和文件夹,临时存放下数据
[root@localhost bb]# ll -d /tmp
drwxrwxrwt. 18 root root 4096 2月 26 14:35 /tmp
5.11 设置SET位、粘滞位权限
- 使用权限字符
chmod ug+s 可执行文件
chmod 0+t 目录名
- 使用权限数字:
- cpu调度算法
ow文件中自己的密码
- cpu调度算法
[root@localhost bb]# chmod u+s '*_*'
[root@localhost bb]# ll
总用量 0
drws------. 2 root hello 6 2月 26 14:36 *_*
5.10 粘滞位权限
1、主要用途:
-
为公共目录(例如:权限为777的)设置,权限字符为“t”
因为本来某公共目录如果是777,就算里面的文件其他用户没有执行权限,其他用户也能删除。
-
用户不能删除该目录中其他用户的文件
2、/tmp
目录:
任何用户都可以在这个目录下新建文件和文件夹,临时存放下数据
[root@localhost bb]# ll -d /tmp
drwxrwxrwt. 18 root root 4096 2月 26 14:35 /tmp
5.11 设置SET位、粘滞位权限
- 使用权限字符
chmod ug+s 可执行文件
chmod 0+t 目录名
- 使用权限数字:
- cpu调度算法
- m为4时,对应SUID,2对应SGID,1对应粘滞位,可叠加