Linux云计算-第三章 文件权限管理

文章详细介绍了Linux系统中的文件权限机制,包括UGO(用户、组、其他)权限模型,读取、写入、执行权限的数值表示,以及如何使用chmod和chown命令来修改文件的权限和属主属组。通过实例展示了如何增减权限和切换属主属组,强调了权限设置对文件访问控制的重要性。

Linux用户权限解析

UGO权限概述

  • QQ空间的红钻特权        ​​​​​
  • 文件权限设置真实的样子
  • 赋予某个用户或组,能够以何种方式访问某个文件(图片文件,视频文件,普通文件)

权限对象

  • 权限对象
  • 所属主:U
  • 所属组:g
  • 其他人:o
  •  所有人:a(u+g+o)

 权限类型

  • 读取权限:r=4
  • 写入权限:w=2
  • 执行权限:x=1

文件的所有者、所属组

  • 查看权限
  •  查看权限记录
    • [root@www ~]# ls -l /root/1.txt -rw-r--r--. 1 root root 458 6月  15 15:11 /root/1.txt
    • 其中:-表示文件类型            rw-主人的权限,属主             r--属组的权限                r--其他人的权限权限的扩展     1  文件链接        root 文件的属主              root文件的属组              458大小          6月 15    15:11 文件最后的修改时间   /root/1.txt   文件的名称和路径

 设置权限

  • 更改权限
  • 使用符号
  • 语法
  • 使用符号:u用户  g组   o 其他    r 读  w 写  x  执行语法:chmod  对象(u/g/o/a)赋值符(+/-/=)权限类型(r/w/x)  文件/目录
  • 当修改目录的权限时,需要加参数-R、以及使用绝对路径

举例:

   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 2023"read    -p     "请输入您的姓名:"     nameecho     "哈哈 $name 是大笨蛋"

 3.增加执行权限

 [root@localhost tmp]# chmod u+x file1                 //属主增加执行

 4.运行测试——成功

 [root@localhost tmp]# ./file1 hello 2020请输入您的姓名:45674567 是大笨蛋

 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 //显示的结果

7.使用数字

• 4读   2写   1执行[root@localhost ~]# chmod 644 file1[root@localhost ~]# ll file1-rw-r--r-- 1 alice it 17 10-25 16:45 file1

 更改属主、属组

• chown命令

• chown:设置一个文件属于谁,属主语法:chown  用户名|组名  文件

• [root@localhost ~]# chown alice.hr file1

•  //改属主、属组

• [root@localhost  ~]# chown alice file1

• //只改属主

• [root@localhost ~]# chown .hr file1

•  //只改属组

• 选项

• -R 针对目录中所有的文件。进行统一授权

• chgrp命令

• chgrp: 设置一个文件属于哪个组,属组

• 语法:   chgrp   组名   文件      -R是递归的意思[root@localhost ~]# chgrp it file1 //改文件属组[root@localhost ~]# chgrp -R it dir1 //改文件属组

举例

  • 1.用户权限意义

• 设定用户账户对文件(图片、视频、程序)的访问能力(看一看、写一写、执行)

  • 2.权限的对象

• 属主:文件的主人:u

• 属组:文件属于组员权限:g

• 其他人:除了主人和组员之外的用户:other

• 特殊对象:所有人(包括主人、组员、其他人):all

  • 3.权限的类型

• 读取:r=4

• 写入:w=2

• 执行:x=1

  • 4.修改文件的权限(完成授权一种方式)

• 命令:chmod (ch改变mod设置)

• 语法:chmod 授权  文件

• 示例:chmod  u+x  file1.txt

• 示例:chmod  764 file1.txt(属主读写执行,属组读写,其他人读)

• 案例:给一个文件增加执行权限,用于执行

  • 5.修改文件的属主和属组(完成授权另一种方法)

• 命令:chown(ch改变own所有者)

• 语法:chown 属主。属组  文件/文件夹

• 语法:chown  属主文件

• 语法:chown  属组文件

• 示例:chown   user01.hr  file1.txt

• 示例:chown  -R  user01.hr  dir1/

  • 6.案例需求

• 文件file10.txt

• 属主是user100,读写执行-7(可以看内容,可以改内容,可以执行)

• 属组是jishuzu(user200),读取 -4(只能看,不能改,不能执行)

• 其他人  没有权限-0(既不能看,也不能改和执行)

  • 7.操作

• 1.使用root账户,来到/tmp目录

• [root@localhost tmp]# cd /tmp

• [root@localhost tmp]# pwd

• 2.创建文件file10.txt

• [root@localhost tmp]# touch file10.txt

• [root@localhost tmp]# ll file10.txt

• -rw-r--r--. 1 root root 0 3月  19 16:20 file10.txt

•  主人:读写

•  组:读

•  其他人:读

• 3.准备测试账号

•  [root@localhost tmp]# useradd user100

•  [root@localhost tmp]# useradd user200

•  [root@localhost tmp]# useradd user300

•  [root@localhost tmp]# groupadd jishuzu

•  [root@localhost tmp]# usermod -aG jishuzu   user200

•  [root@localhost tmp]# id user200

•  uid=1013(user200) gid=1016(user200) 组=1016(user200),1018(jishuzu)

• 4.授予文件属主和属组,以及其他人的权限

•  [root@localhost tmp]# chmod 740 file10.txt

•  [root@localhost tmp]# ll file10.txt

•  -rwxr-----  . 1 root root 0 3月  19 16:20 file10.txt

•  主人:  读写执行

•  组:    读

•  其他人:没有权限

• 5.修改文件属主和属组。chown

• [root@localhost tmp]# chown user100.jishuzu  file10.txt

• [root@localhost tmp]# ll file10.txt

• -rwxr-----. 1 user100 jishuzu 0 3月  19 16:20 file10.txt

• 属主:user100

• 属组:jishuzu(组员user200)

• 6.测试

• 使用user100,访问文件,写入文件,执行文件

• [root@localhost tmp]# su - user100

• [user100@localhost ~]$ cd /tmp/

• [user100@localhost tmp]$ cat file10.txt

• [user100@localhost tmp]$ vim file10.txt

• [user100@localhost tmp]$ ./file10.txt

• 123

• 456

• 789

• [user100@localhost tmp]$ exit

• 登出

• 使用jishuzu成员,访问文件,不可写和执行

• [root@localhost tmp]# su - user200

• [user200@localhost ~]$ cd /tmp/

• [user200@localhost tmp]$ cat file10.txt

• echo 123

• echo 456

• echo 789

• [user200@localhost tmp]$ vim file10.txt

• 写入失败,强制退出:q!

• [user200@localhost tmp]$ ./file10.txt

• -bash: ./file10.txt: 权限不够

• [user200@localhost tmp]$ exit

• 登出

• 使用其他用户user300,访问文件失败,写入失败,执行失败

• [root@localhost tmp]# su - user300

• [user300@localhost ~]$ cd /tmp/

• [user300@localhost tmp]$ cat file10.txt

• cat: file10.txt: 权限不够

• [user300@localhost tmp]$ vim file10.txt

• 写入失败,强制退出:q!

• [user300@localhost tmp]$ ./file10.txt

• [user300@localhost tmp]$ exit

• 登出

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嗨丶王哪跑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值