Linux系统用户权限

Linux系统用户权限:

掌握其中的逻辑关系


UGO:

一、设置权限的两个基本元素

1、权限对象

属主:u
属组:g
其他人:o
所有人:a

2、权限类型

读:r=4 (可读权限,用户只具有查看的权限)

写: w=2(可写权限,用户可以对文件进行编辑等权限)

执行:x=1(可执行权限,用户可以对文件有执行的权限,能够直接运行文件)

二、查看权限记录

通过命令ls -l 文件/目录或者ll -d文件/目录可以查看文件/目录的权限。

[root@localhost ~]#ls  -l  /root/1.txt
-rw-r--r--. 1  root root  179  525  14:27  /root/1.txt

各个部分代表的含义:

项目Value
-文件类型
r w -主人的权限属主
r - -属组的权限
r - -其他人的权限
.权限的扩展
1文件链接
root文件的属主
root文件的属组
179大小
5月 25 14:27文件最后的修改时间
/root/1.txt文件的名和路径

三、设置权限

1、更改权限

使用符号

使用符号:u用户、g组 、o其他 、r读 、w写 、x执行
语法: chmod 对象(u/g/o/a)赋值符(+/-/=)权限类型(r/w/x) 文件/目录

示例:
1.了解普通文件的基本权限

[root@localhost tmp]# ll file1 
-rw-r--r--. 1 root root 0 413 20:49 file1
权限         属主 属组              文件

2.编写程序

[root@localhost tmp]#vim    file1 
echo    "hello 2020"
read    -p     "请输入您的姓名:"     name
echo     "哈哈 $name 是大笨蛋"

3.增加执行权限

[root@localhost tmp]# chmod u+x file1

4、运行测试。成功

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

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 主、组等于读写,其他人只读
使用数字

4读 2写 1执行

[root@localhost ~]# chmod 644 file1
[root@localhost ~]# ll file1
-rw-r--r-- 1 alice it 17 10-25 16:45 file1

2、更改属主/组

1、chown: 设置一个文件属于谁,属主

语法: chown 用户名 . 组名 文件

[root@localhost ~]# chown alice.hr file1  改属主、属组
[root@localhost  ~]# chown alice file1    只改属主
[root@localhost ~]# chown .hr file1       只改属组

:chmod -R 针对目录中所有的文件。

2、chgrp:设置一个文件属于哪个组,属组(比较鸡肋)

语法:chgrp 组名 文件 -R是递归的意思

[root@localhost ~]# chgrp it file1     改文件属组
[root@localhost ~]# chgrp -R it dir1   改文件属组

ACL

一、 ACL的概念及其作用

传统的权限设置仅有三种身份(ower、group、other),搭配三种权限(read、write、execute),并没有办法单纯针对某一个用户或某一个组来设置特定的权限需求。为了解决这一问题,就提出了ACL的办法。

二、命令使用

ACL的设置主要是通过以下两条命令实现的:

getfacl:用于取得某个文件/目录的ACL设置

setfacl:用于设置某个目录/文件的ACL规定

1、getfacl命令

getfacl命令大多只是用来查询文件/目录的ACL设置的,其他使用较少。

2、setfacl命令

setfacl    -m         u:alice:rw              /home/test.txt
命令       设置      用户或组:用户名:权限            文件对象

参数介绍:

  • -m:修改文件当前的ACL,不可与-x参数合用

  • -x:从文件的ACL中单独移除一条权限

  • -b:移除文件的全部ACL

  • -k:删除默认的ACL

  • -d:为目录设置默认的ACL(该参数只对目录有效)

  • -R:递归设置ACL,其子目录也将会被设置

用法:

1、准备文件

[root@localhost ~]# touch /home/test.txt
[root@localhost ~]# ll /home/test.txt 
-rw-r--r-- 1 root root 0 10-26 13:59 /home/test.txt

2、查看文件有哪些ACL权限。

[root@localhost ~]# getfacl /home/test.txt

3、设置用户alice,jack权限

[root@localhost ~]#useradd  alice  jack
[root@localhost ~]# setfacl -m u:alice:rw /home/test.txt
[root@localhost ~]# setfacl -m u:jack:- /home/test.txt

4、请思考命令中的o是什么作用

[root@localhost ~]# setfacl -m o::rw /home/test.txt

5、查看ACL

[root@localhost ~]# getfacl /home/test.txt

6、设置ACL

设置(增加hr组对test.txt文件读取的权限)

[root@localhost ~]# setfacl -m g:hr:r /home/test.txt

7、删除

[root@localhost ~]# setfacl -x g:hr /home/test.txt      //删除部分
[root@localhost ~]# setfacl -b /home/test.txt          //删除所有

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值