目录
一,基本权限UGO
赋于某个用户或组 能够以何种方式 访问某个文件(图片文件,视频文件,普通文件)
1、权限设置对象:属主(u) 属组(g) 其他人(o) 所有人(a(u+g+o))
属主:文件的主人:u 属组:文件属于组员权限:g 其他人:除了主人和组员之外的用户:other
特殊对象:所有人(包含主人,组员,其他):all 全部
2、权限类型:读:r=4 写:w=2 执行:x=1
3、查看权限记录
[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 文件的名和路径
4.设置权限
4.1更改权限
使用符号
语法: chmod 对象(u/g/o/a)赋值符(+/-/=)权限类型(r/w/x) 文件/目录
1.了解普通文件的基本权限
[root@localhost ~]# cd /tmp
[root@localhost tmp]# touch file66
[root@localhost tmp]# ll file66
-rw-r--r--. 1 root root 0 7月 28 03:32 file66
权限 属主 属组 文件
2.编写程序(在创建的file66中编写一个程序做后面的运行测试)
echo "郑州欢迎你!"
read -p "请输入你的姓名:" name
echo "欢迎 $name 下次再来 "
3.增加执行权限:[root@localhost tmp]# chmod u+x file66 //属主增加执行
4.运行测试。成功
[root@localhost tmp]# ./file66
郑州欢迎你!
请输入你的姓名:***
欢迎 *** 下次再来
5.去除权限。运行失败
[root@localhost tmp]# chmod u-x file66
[root@localhost tmp]# ./file66
-bash: ./file66: 权限不够
使用数字:
读:r=4
写:w=2
执行: x=1
[root@localhost tmp]# chmod 644 file66 //属主的权限可以读写,属组和 其他人只能读
[root@localhost tmp]# ll file66
-rw-r--r--. 1 root root 99 7月 28 03:56 file66
4.2更改属主、属组(chown命令和chgrp命令)
1.chown命令:
chown: 设置一个文件属于谁,属主
语法: chown 用户名.组名 文件 //改属主、属组
chown 用户名 文件 //改属主
chown .组名 文件 //改属组
[root@localhost tmp]# ll file66
-rw-r--r--. 1 root root 99 7月 28 03:56 file66
属主root 数组root
使用chown 修改一个文件属于谁
[root@localhost tmp]# chown user01.zz file66
[root@localhost tmp]# ll file66
-rw-r--r--. 1 user01 zz 99 7月 28 03:56 file66
属主user01 属组zz
2.chgrp命令
chgrp: 设置一个文件属于哪个组,属组
语法: chgrp 组名 文件 -R是递归的意思
[root@localhost ~]# chgrp it file1 //改文件属组
[root@localhost ~]# chgrp -R it dir1 //改文件属组
案例
案例需求:文件file20.txt,属主是user11,读写执行 7(可以看内容,可以改内容,可以执行) 属组是mm(user22),读取 4(只能看,不能改,不能执行)其他人 没有权限 0(既不能看,又不能改和执行)
1.使用root账户,来到/tmp目录
[root@localhost tmp]# cd /tmp
2.创建文件file20.txt
[root@localhost tmp]# pwd
/tmp
[root@localhost tmp]# touch file20.txt
[root@localhost tmp]# ll file20.txt
-rw-r--r--. 1 root root 0 7月 28 04:58 file20.txt
主人:读写(rw) 组:读(r) 其他人:读(r)
3.准备测试账号
创建用户 ( user11 ,user22 ,user33 ),创建组(mm) ,将用户user22追加到组mm
[root@localhost tmp]# useradd user11
[root@localhost tmp]# useradd user22
[root@localhost tmp]# useradd user33
[root@localhost tmp]# groupadd mm
oot@localhost tmp]# usermod -aG mm user22
[root@localhost tmp]# id user22
uid=1008(user22) gid=1008(user22) 组=1008(user22),1679(mm)
4.授予文件属主和属组,以及其他人的权限。
[root@localhost tmp]# chmod 740 file20.txt
[root@localhost tmp]# ll file20.txt
-rwxr-----. 1 root root 0 7月 28 04:58 file20.txt
5.修改文件属主和属组。chown
[root@localhost tmp]# chown user11.mm file20.txt
[root@localhost tmp]# ll file20.txt
-rwxr-----. 1 user11 mm 0 7月 28 04:58 file20.txt
属主:user11 属组:mm组员user22)
6.测试
使用user11,访问文件。写入文件,执行文件
[root@localhost tmp]# su - user100
[user11@localhost ~]$ cd /tmp/
[user11@localhost tmp]$ cat file20.txt
[user11@localhost tmp]$ vim file20txt
[user11@localhost tmp]$ ./file20.txt
123
456
789
[user11@localhost tmp]$ exit
登出
使用mm成员,访问文件,不可写和执行
[root@localhost tmp]# su - user222
[user22@localhost ~]$ cd /tmp/
[user22@localhost tmp]$ cat file20.txt
[user22@localhost tmp]$ vim file20.txt
写入失败,强制退出:q!
[user22@localhost tmp]$ ./file20.txt
-bash: ./file20.txt: 权限不够
使用其他用户user33,访问文件失败。写入失败,执行失败。
[root@localhost tmp]# su - user33
[user300@localhost ~]$ cd /tmp/
[user33@localhost tmp]$ cat file20txt
cat: file20.txt: 权限不够
[user33@localhost tmp]$ vim file20.txt
写入失败,强制退出:q!
[user33localhost tmp]$ ./file20.txt
-bash: ./file20.txt: 权限不够
[user33@localhost tmp]$ exit
登出
二.基本权限 ACL
ACL和UGO区别
ACL文件权限管理: 设置不同用户,不同的基本权限(r、w、x)。对象数量不同。
UGO设置基本权限: 只能一个用户,一个组和其他人
ACL语法:setfacl -m u:user01:rw /home/test.txt
命令 设置 用户或组:用户名:权限 文件对象
用法:1.准备文件/home/test.txt 准备用户a1 和用户b1两个用户。
[root@localhost ~]# touch /home/test.txt //
root@localhost ~]# useradd a1
[root@localhost ~]# useradd b1
2. 查看文件有哪些ACL权限。
[root@localhost ~]# getfacl /home/test.txt
getfacl: Removing leading '/' from absolute path names
# file: home/test.txt
# owner: root
# group: root
user::rw-
group::r--
other::r--
owner: root 属主:root
group: root 属组:root
user::rw 用户:属主:rw
group::r 组:属组:r
other::r other:其他人:r
3.设置用户a1,b1权限
[root@localhost ~]# setfacl -m u:a1:rw /home/test.txt //设置用户a1权限
[root@localhost ~]# setfacl -m u:b1:- /home/test.txt //设置用户b1权限
[root@localhost ~]# setfacl -m o::rw /home/test.txt //设置其他用户other权限
[root@localhost ~]# getfacl /home/test.txt
getfacl: Removing leading '/' from absolute path names
# file: home/test.txt
# owner: root
# group: root
user::rw-
user:a1:rw-
user:b1:---
group::r--
mask::rw-
other::rw-
# file: home/test.txt 文件名
# owner: root 属主:root
# group: root 属组:root
user::rw 用户:属主:rw
user:a1:rw- 用户:a1:rw-
user:b1:--- 用户:b1:---
group::r-- 组:属组:r--
mask::rw- 掩码::rw-
other::rw- other:其他人:rw-
设置组的权限:增加hr组对test.txt文件读取的权限
首先创建组hr:[root@localhost ~]# groupadd hr
增加hr组对test.txt文件读取的权限:
[root@localhost ~]# setfacl -m g:hr:r /home/test.txt //增加hr组对test.txt文件读取的权限:
[root@localhost ~]# getfacl /home/test.txt
getfacl: Removing leading '/' from absolute path names
# file: home/test.txt
# owner: root
# group: root
user::rw-
user:a1:rw-
user:b1:---
group::r--
group:hr:r-- 增加了hr组对test.txt文件读取的权限
mask::rw-
other::rw-
删除ACL
删除部分:删除组hr的读取的权限
[root@localhost ~]# setfacl -x g:hr /home/test.txt //删除组hr的读取的权限
[root@localhost ~]# getfacl /home/test.txt
getfacl: Removing leading '/' from absolute path names
# file: home/test.txt
# owner: root
# group: root
user::rw-
user:a1:rw-
user:b1:---
group::r-- hr没有了读取的权限
mask::rw-
other::rw-
删除所有:
[root@localhost ~]# setfacl -b /home/test.txt //删出以上创建的所有用户和组的权限
[root@localhost ~]# getfacl /home/test.txt
getfacl: Removing leading '/' from absolute path names
# file: home/test.txt
# owner: root
# group: root
user::rw-
group::r--
other::rw-
三.特殊权限
特殊位 suid:高级权限的类型,suid针对文件/程序时,具备临时获得属主的权限。
设置suid,使普通用户通过suid临时提权,查看超管root用户的文件
1.为cat程序添加上suid权限。
[root@localhost ~]# chmod u+s /usr/bin/cat //给文件设置suid
[root@localhost ~]# ll /usr/bin/cat
-rwsr-xr-x. 1 root root 54080 8月 20 2019 /usr/bin/cat
2.使用普通用户运行cat。暂时获得root权限
[root@localhost ~]# su - a1
上一次登录:三 7月 29 02:42:10 CST 2020pts/3 上
[a1@localhost ~]$ cat /root/file1.txt
r123345
23432 //,普通用户,看到了root的内容
3254
这个行为很危险,请在试验后,将cat的suid权限除去。(要切换到到root)
[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
文件属性chattr
用途:常用于锁定某个文件,拒绝修改。
案例;1 先创建新文件进行对比。查看默认权限。
[root@localhost ~]# touch file100
[root@localhost ~]# lsattr file100
---------------- file100
2 加上不能删除的属性(i)
[root@localhost ~]# chattr +i file100
[root@localhost ~]# lsattr file100
----i----------- file100 //i不能更改,重命名,删除
3. 尝试删除
[root@localhost ~]# rm -rf file100
rm: 无法删除"file100": 不允许的操作
4 将属性还原。
[root@localhost ~]# chattr -i file100
再次尝试删除
[root@localhost ~]# rm -rf file100
[root@localhost ~]# cat file100
cat: file100: 没有那个文件或目录 //已经删除了文件file100
进程掩码 umask
新建文件、目录的默认权限会受到umask的影响,umask表示要减掉的权限
示例:观察系统默认掩码 ,在shell进程中创建文件,先查看当前用户的umask权限
[root@localhost ~]# umask
0022 //系统原来的进程掩码
[root@localhost ~]# touch file111
[root@localhost ~]# mkdir dir111
[root@localhost ~]# ll -d dir111 file111
drwxr-xr-x. 2 root root 6 7月 29 03:23 dir111
-rw-r--r--. 1 root root 0 7月 29 03:23 file111
修改shell umask值(临时)
[root@localhost ~]# umask 000
[root@localhost ~]# mkdir 99
[root@localhost ~]# touch file99
[root@localhost ~]# ll -d 99 file99
drwxrwxrwx. 2 root root 6 7月 29 03:26 99
-rw-rw-rw-. 1 root root 0 7月 29 03:26 file99