一,基本权限UGO
赋于某个用户或组 能够以何种方式 访问某个文件(图片文件,视频文件,普通文件)
1、权限设置对象:属主(u) 属组(g) 其他人(o) 所有人(a(u+g+o))
属主:文件的主人:u 属组:文件属于组员权限:g 其他人:除了主人和组员之外的用户:other
特殊对象:所有人(包含主人,组员,其他):all 全部
2、权限类型:读:r=4 写:w=2 执行:x=1
3、查看权限记录
[[email protected] ~]#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.了解普通文件的基本权限
[[email protected] ~]# cd /tmp
[[email protected] tmp]# touch file66
[[email protected] tmp]# ll file66
-rw-r--r--. 1 root root 0 7月 28 03:32 file66
权限 属主 属组 文件
2.编写程序(在创建的file66中编写一个程序做后面的运行测试)
echo "郑州欢迎你!"
read -p "请输入你的姓名:" name
echo "欢迎 $name 下次再来 "
3.增加执行权限:[[email protected] tmp]# chmod u+x file66 //属主增加执行
4.运行测试。成功
[[email protected] tmp]# ./file66
郑州欢迎你!
请输入你的姓名:马俊飞
欢迎 马俊飞 下次再来
5.去除权限。运行失败
[[email protected] tmp]# chmod u-x file66
[[email protected] tmp]# ./file66
-bash: ./file66: 权限不够
使用数字:
读:r=4
写:w=2
执行: x=1
[[email protected] tmp]# chmod 644 file66 //属主的权限可以读写,属组和 其他人只能读
[[email protected] 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 .组名 文件 //改属组
[[email protected] tmp]# ll file66
-rw-r--r--. 1 root root 99 7月 28 03:56 file66
属主root 数组root
使用chown 修改一个文件属于谁
[[email protected] tmp]# chown user01.zz file66
[[email protected] tmp]# ll file66
-rw-r--r--. 1 user01 zz 99 7月 28 03:56 file66
属主user01 属组zz
2.chgrp命令
chgrp: 设置一个文件属于哪个组,属组
语法: chgrp 组名 文件 -R是递归的意思
[[email protected] ~]# chgrp it file1 //改文件属组
[[email protected] ~]# chgrp -R it dir1 //改文件属组
案例
案例需求:文件file20.txt,属主是user11,读写执行 7(可以看内容,可以改内容,可以执行) 属组是mm(user22),读取 4(只能看,不能改,不能执行)其他人 没有权限 0(既不能看,又不能改和执行)
1.使用root账户,来到/tmp目录
[[email protected] tmp]# cd /tmp
2.创建文件file20.txt
[[email protected] tmp]# pwd
/tmp
[[email protected] tmp]# touch file20.txt
[[email protected] 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
[[email protected] tmp]# useradd user11
[[email protected] tmp]# useradd user22
[[email protected] tmp]# useradd user33
[[email protected] tmp]# groupadd mm
[email protected] tmp]# usermod -aG mm user22
[[email protected] tmp]# id user22
uid=1008(user22) gid=1008(user22) 组=1008(user22),1679(mm)
4.授予文件属主和属组,以及其他人的权限。
[[email protected] tmp]# chmod 740 file20.txt
[[email protected] tmp]# ll file20.txt
-rwxr-----. 1 root root 0 7月 28 04:58 file20.txt
5.修改文件属主和属组。chown
[[email protected] tmp]# chown user11.mm file20.txt
[[email protected] tmp]# ll file20.txt
-rwxr-----. 1 user11 mm 0 7月 28 04:58 file20.txt
属主:user11 属组:mm组员user22)
6.测试
使用user11,访问文件。写入文件,执行文件
[[email protected] tmp]# su - user100
[[email protected] ~]$ cd /tmp/
[[email protected] tmp]$ cat file20.txt
[[email protected] tmp]$ vim file20txt
[[email protected] tmp]$ ./file20.txt
123
456
789
[[email protected] tmp]$ exit
登出
使用mm成员,访问文件,不可写和执行
[[email protected] tmp]# su - user222
[[email protected] ~]$ cd /tmp/
[[email protected] tmp]$ cat file20.txt
[[email protected] tmp]$ vim file20.txt
写入失败,强制退出:q!
[[email protected] tmp]$ ./file20.txt
-bash: ./file20.txt: 权限不够
使用其他用户user33,访问文件失败。写入失败,执行失败。
[[email protected] tmp]# su - user33
[[email protected] ~]$ cd /tmp/
[[email protected] tmp]$ cat file20txt
cat: file20.txt: 权限不够
[[email protected] tmp]$ vim file20.txt
写入失败,强制退出:q!
[user33localhost tmp]$ ./file20.txt
-bash: ./file20.txt: 权限不够
[[email protected] tmp]$ exit
登出