Linux系统–用户的权限
一、基本权限UGO(常用)
权限对象:(每个文件都有)
属主: u (文件的属主)
属组: g (文件的分组)
其他人:o (other 除了主人和分组 其他的用户)
所有人:a(u+g+o) (a–all)
权限类型 (文件夹默认有执行权 文件默认没有执行权)
读:r=4 (读取 可以读文件 默认有此功能)【采用八进制】
写:w=2 (可以对文件编辑 默认有此功能)
执行: x=1 ( 可以编写程序 默认没有)
查看权限
[root@localhost ~]# ll /home/test.txt
-rw-rw-rw-. 1 root root 7 11月 19 15:17 /home/test.txt
分析:
-文件类型(是一个文件)
rw-主人的权限,属主(可读取、可写入、占位符)
r–属组的权限(默认只可读取)
r–其他人的权限(默认只可读取)
.权限的扩展(在ACL中添加用户,此处会变成+号)
1文件链接(第七章文件链接)
root文件的属主
root文件的属组
179大小
文件最后修改时间
/home/test.txt 文件名和路径
!!!
打开两个终端
终端一输入:
[root@localhost tmp]# watch -n1 'ls -l /tmp/file1'
会每隔一秒刷新文件file1d的权限和属组属主
终端二:
进行基本权限UGO操作 会在终端一上实时刷新
设置权限
更改权限
使用符号:
u:用户 g:组 o:其他人 a=u+g+o r:读 w:写 x:执行
语法 :chmod 对象(u/g/o/a)赋值符{+(增加) -(减去) =(覆盖)}权限类型(r/w/x)文件/目录
例:
1.
[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
权限 属主 属组 文件
2.编写程序:
[root@localhost tmp]#vim file1
echo “hello 2020”
read -p “请输入您的姓名:” name(-p 打印(可将双引号内容显示出)name(代称))
echo “哈哈 $name 是大笨蛋”(将name调出使用)
3.增加执行权限
[root@localhost tmp]# chmod u+x file1 (属主增加执行x)
4.运行测试
[root@localhost tmp]# ./file1 (需要路径)
hello 2020
请输入您的姓名:4567
4567 是大笨蛋
[root@localhost tmp]# bash file1 (不需要路径)
hello 2020
请输入你的姓名:
5.去除权限,则运行失败
[root@localhost tmp]# chmod u-x file1
[root@localhost tmp]# ./file1
-bash: ./file1: 权限不够
6.更多的修改权限练习
[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 //显示的结果
使用数字(修改权限)
4读 2写 1执行 (6:读和写 000 :清除全部权限)
[root@localhost ~]# chmod 644 file1(最多有四个数字)
[root@localhost ~]# ll file1
-rw-r--r-- 1 alice it 17 10-25 16:45 file1
更改属主、属组
chown命令:
语法 : chown 用户名.组名 文件
[root@localhost ~]# chown alice.hr file1 //改属主、属组(只有超管可修改)
[root@localhost ~]# chown alice file1 //只改属主(只有超管可修改)
[root@localhost ~]# chown .hr file1 //只改属组 (只有超管可修改)
[root@localhost ~]# chown -R .hr dir222
-R 针对目录中所有的文件。(-R可将文件内所有文件的属主、属组修改)
chgrp命令:
chgrp: 设置一个文件属于哪个组,属组
语法: chgrp 组名 文件 -R是递归的意思
[root@localhost ~]# chgrp hr file1 //改文件属组
[root@localhost ~]# chgrp -R it dir1 //改文件属组
案例:
用户:user100 user200(分到jishuzu中) user300
组 :jishuzu
修改file10.txt权限并查看
[root@localhost tmp]# chmod 700 file10.txt
[root@localhost tmp]# ll file10.txt
-rwx------. 1 root root 39 11月 19 14:33 file10.txt
将user100和jishuzu分别修改成文件的属主和属组
[root@localhost tmp]# chown user100.jishuzu file10.txt
[root@localhost tmp]# ll file10.txt
-rwx------. 1 user100 jishuzu 39 11月 19 14:33 file10.txt
分别切换user100 user200 user300使行rwx的权限 验证
二、基本权限ACL(用的较少)
区别:
ACL文件权限管理: 设置不同用户,不同的基本权限(r、w、x)。对象数量不同。
UGO设置基本权限: 只能一个用户,一个组和其他人
查看文件有哪些ACL权限
[root@localhost tmp]# getfacl file10.txt (getfacl : 获得文件访问控制列表)
# file: file10.txt
# owner: user100
# group: jishuzu
user::rwx
group::—
other::—
语法:setfacl -m(增加) u:user01:rw /home/test.txt
命令 设置 用户(u)或组(g):用户名:权限 文件对象
[root@qianfeng ~]# setfacl -m u:alice:rw /home/test.txt(赋予读写权限)(setfacl : 获得文件访问控制列表)
[root@qianfeng ~]# setfacl -m u:jack:- /home/test.txt(禁止权限)
[root@qianfeng ~]# setfacl -m o::rw /home/test.txt (对other的设置)
给file10.txt增加用户:
[root@localhost home]# setfacl -m u:user01:rw /tmp/file10.txt (赋予读写权限)(setfacl : 获得文件访问控制列表)
[root@localhost home]# getfacl /tmp/file10.txt
getfacl: Removing leading '/' from absolute path names 查看文件权限,删除了根路径
# file: tmp/file10.txt 文件名
# owner: user100 属主:root
# group: jishuzu 属组:root
user::rwx 用户:属主权限:rwx
user:user01:rw- 用户:user01权限:rw-
group::--- 组 : 属组权限:---
mask::rw- 掩码:rw-(指的是用户或群组能拥有的最大ACL权限,也就是说,给用户或群组设定的ACL权限不能超过mask规定的权限范围,超出部分做无效处理。
other::--- 其他人权限:---
给file10.txt增加组:
[root@localhost home]# setfacl -m g:hr:rw /tmp/file10.txt
[root@localhost home]# getfacl /tmp/file10.txt
getfacl: Removing leading '/' from absolute path names
# file: tmp/file10.txt
# owner: root
# group: jishuzu
user::rwx
user:user01:rw-
group::---
group:hr:rw-
mask::rw-
other::---
删除用户/组: //删除组hr的acl权限(-x删除某一条)
[root@localhost home]# setfacl -x u:user01 /tmp/file10.txt (不用加权限)
[root@localhost home]# getfacl /tmp/file10.txt
getfacl: Removing leading '/' from absolute path names
# file: tmp/file10.txt
# owner: root
# group: jishuzu
user::rwx
group::---
group:hr:rw-
mask::rw-
other::---
//删除所有(-b)acl后期附加权限:用户、组
[root@localhost home]# setfacl -b /tmp/file10.txt
[root@localhost home]# getfacl /tmp/file10.txt
getfacl: Removing leading '/' from absolute path names
# file: tmp/file10.txt
# owner: root
# group: jishuzu
user::rwx
group::---
other::---
三、特殊权限(了解)
4 --> suid–用户
2 --> sgid–组
1 --> stick–其他人
特殊位suid:(系统中passwd(修改密码,只能修改自己的)本身就含有suid)
针对文件/程序时,具备临时获得属主的权限。
只能作用于二进制程序上(例:cat),不能作用于脚本上,且设置在目录上无意义
案例:
设置suid,使普通用户通过suid临时提权,查看超管root用户的文件(普通用户无法使用cat)
a.为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(s--suid使用普通用户运行cat。暂时获得root权限)
[root@localhost ~]# ll /usr/bin/cat
-rwsr-xr-x. 1 root root 54080 8月 20 2019 /usr/bin/cat(用户的权限变成rws)
数字:文件属主的x权限,用s代替.表示被设置了SUID
[root@localhost ~]# chmod 4755 /bin/cat(在普通三位数字权限位之前,用4代表添加的SUID位)
[root@localhost ~]# ll /bin/cat
-rwsr-xr-x. 1 root root 54080 8月 20 2019 /bin/cat
如果属主位没有x权限,会显示为大写S,表示有故障(权限无效)
[root@localhost ~]# chmod 4000 /bin/cat
[root@localhost ~]# ll /bin/cat
---S------. 1 root root 54080 8月 20 2019 /bin/cat
b.使用普通用户运行cat,暂时获得(属主)root权限
[root@localhost ~]# su - user01
上一次登录:四 11月 19 19:16:41 CST 2020pts/0 上
[user01@localhost ~]$ cat /root/file1.txt
1123456
**普通用户看到了root的内容,此行为很危险 在试验后 将suid权限去除
[root@localhost ~]# chmod u-s /usr/bin/cat
查看
[root@localhost ~]# ll /usr/bin/cat
-rwxr-xr-x. 1 root root 54080 8月 20 2019 /usr/bin/cat
特殊位sgid:
作用在二进制程序上时,原理同suid,此用户将继承此程序的所属组权限
作用于目录上时,此文件夹下所有用户新建文件都自动继承此目录的用户组
案例:
a.对/tmp/dir111添加sgid
[root@localhost ~]# ll /tmp/dir111 -d
drwxrwxrwx. 2 root root 6 11月 19 20:08 /tmp/dir111
数字:
[root@localhost ~]# chmod 2777 /tmp/dir111
字母:
[root@localhost ~]# chmod g+s /tmp/dir111
[root@localhost ~]# ll /tmp/dir111 -d
drwxrwsrwx. 2 root root 6 11月 19 20:08 /tmp/dir111 (组的权限变成rws)
b.切换到普通用户user01,在dir111中创建一个文件和一个目录
[user01@localhost dir111]$ touch 1
[user01@localhost dir111]$ mkdir 2
[user01@localhost dir111]$ ll
总用量 0
-rw-rw-r--. 1 user01 root 0 11月 19 20:12 1
drwxrwsr-x. 2 user01 root 6 11月 19 20:12 2
结果显示:user01在dir111中创建的文件目录都自动继承了dir111的属组
新的目录的权限也继承了sgid
所以设定了sgid的目录中 所有的新建文件和目录都会自动属于root组(用另一个普通用户(非user01)创建dir111,效果会明显)
特殊位stick:
对于一个多人可写的目录,如果设置了sticky,则每个用户仅能删除和改名自己的文件或目录;
只能作用在目录上.普通文件设置无意义,且会被linux内核忽略
用户在设置Sticky权限的目录下新建的目录不会自动继承Sticky权限
root用户先创建一个777权限目录/app/tmp
普通用户user01和user02分别在其中创建几个文件和目录
这时候有个问题,就是目录中的任何用户都可以随意删除其他人的文件.
所以root要给/tmp这个文件夹设定一个Stick位
[root@localhost ~]#chmod 1777 /app/tmp
此时切换到user01和user02用户都只能修改自己创建的文件/目录
普通用户在设定了Stick位的目录下创建的子目录不会继承这个Stick权限,所以要注意设定好自己目录的权限.
SUID: user, 占据属主的执行权限位;
s: 属主拥有x权限
S:属主没有x权限
SGID: group, 占据group的执行权限位;
s: group拥有x权限
S:group没有x权限
Sticky: other, 占据ohter的执行权限位;
t: other拥有x权限
T:other没有x权限
文件属性chattr:
[root@localhost tmp]# touch file11
[root@localhost tmp]# lsattr file11 (查看文件属性)
---------------- file11
用Chattr添加上不能删除的属性i--(针对所有使用本文件的人 包括属组、属主、其他人 )
[root@localhost tmp]# chattr +i file11 (不能更改,重命名,删除)
[root@localhost tmp]# lsattr file11 (查看属性)
----i----------- file11
无法删除:[root@localhost tmp]# rm -rf file11
rm: 无法删除"file11": 不允许的操作
试验后,及时去除文件属性
[root@localhost tmp]# chattr -i file11
[root@localhost tmp]# lsattr file11
---------------- file11
用chattr添加上允许在文件中进行追加操作a
[root@localhost tmp]# chattr +a file2
[root@localhost tmp]# lsattr file2
-----a---------- file2
查看
[root@localhost tmp]# echo 123456 >> file2 (123456:追加到file2里;>>:不要显示在屏幕上)
[root@localhost tmp]# cat file2
danjdajsioj
123456
进程掩码 umask:
新建文件、目录的默认权限会受到umask的影响,umask表示要减掉的权限
新建的文件默认没有执行权x
查看umask:[root@localhost tmp]# umask
0022
修改umask:[root@localhost tmp]# umask 0444
查看umask:[root@localhost tmp]# umask
0444
新建文件和目录:
[root@localhost tmp]# touch file400
[root@localhost tmp]# mkdir dir400
查看权限:[root@localhost tmp]# ll -d file400 dir400
d-wx-wx-wx. 2 root root 6 11月 20 20:11 dir400 (目录权限333=777-444)
--w--w--w-. 1 root root 0 11月 20 20:11 file400 (文件权限222=777-444-111)