云计算基础:第三章 Linux用户的权限

1.基本权限UGO

用户的权限可以通俗的理解为:QQ空间的红钻特权、某高仿传奇一刀999999、腾讯视频会员特权…

其实是指: 赋于某个用户或组 能够以何种方式 访问某个文件(图片文件,视频文件,普通文件)

  • 权限对象

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

  • 权限类型

    读:r=4
    写:w=2
    执行: x=1

  • 查看权限

[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 是文件的名和路径

  • 设置权限 :

    (1)更改权限

    更改权限的方法有两种:

    使用符号

    使用符号:u用户 g组  o其他  r读   w写  x执行
    语法: chmod   对象(u/g/o/a)赋值符(+/-/=)权限类型(r/w/x)    文件/目录
    
    #首先在tmp目录创建一个file1文件,并编写程序
    
    [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
    #    权限      属主 属组                 文件
    
    [root@localhost tmp]#vim    file1 
    echo    "hello 2020"
    read    -p     "请输入您的姓名:"     name
    echo     "哈哈 $name 是大笨蛋"
    #增加执行权限
    [root@localhost tmp]# chmod u+x file1 
    # 运行测试
    [root@localhost tmp]# ./file1 
    hello 2020
    请输入您的姓名:4567
    4567 是大笨蛋
    #成功
    #此时我们去除权限,再次测试
    [root@localhost tmp]# chmod u-x file1 
    [root@localhost tmp]# ./file1
    -bash: ./file1: 权限不够
    
    #另外尝试其他的权限命令
    [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执行

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

    (2)更改属主、属组

    常用的有chown命令,设置一个文件属于谁,属主。

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

    # 我们先创建一个组名为 yunjisuan的属组
    [root@localhost tmp]# groupadd yunjisuan
    #创建一个名为user100的用户,使其加入yunjisuan组
    [root@localhost tmp]#  useradd -G yunjisuan user100
    #创建一个名为babyjin的用户
    [root@localhost tmp]# useradd babyjin
    
    # 设置file1 属于 babyjin用户、yunjisuan组
    [root@localhost tmp]# chown babyjin.yunjisuan file1
    #测试
    [root@localhost tmp]# su babyjin
    [babyjin@localhost tmp]$ ll file1
    -rwxr-x---. 1 babyjin yunjisuan 134 7月  27 21:18 file1
    #babyjin用户可以对该文件进行读写执行,云计算属组中的用户可以对改文件进行读执行
    

2.基本权限ACL

  • 区别

ACL(access contral list)访问控制列表 文件权限管理: 设置不同用户,不同的基本权限(r、w、x)。对象数量不同。
UGO设置基本权限: 只能一个用户,一个组和其他人

  • 语法

    [root@localhost /]# setfacl  -m   u:user100:rwx       /tmp/file1
    #                     命令  设置  用户或组:用户名:权限    文件对象
    
  • 用法

3.特殊权限(了解)

  • 用法

-m 修改 modifiy

#首先准备一个名为test.txt的测试文件
[root@localhost ~]# touch /home/test.txt
[root@localhost ~]# ll /home/test.txt 
-rw-r--r--. 1 root root 0 7月  28 20:25 /home/test.txt

#创建名为alice和jack的用户
[root@localhost ~]# useradd jack
[root@localhost ~]# useradd alice

#查看一下该文件有哪些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--
#设置用户alice和jack的权限 
[root@localhost ~]# setfacl -m u:jack:- /home/test.txt 
[root@localhost ~]# setfacl -m u:alice:rw /home/test.txt 
#查看ACL
[root@localhost ~]# getfacl /home/test.txt 

#对应解释如下
# file: home/test.txt      #文件名
# owner: root			  #属主:root
# group: root   		  #属组:root
user::rw-                  #用户:属主:rwx
user:jack:---   		  #用户:jack:---
user:alice:rw-  		  #用户:alice:rw-
group::r--                 #组:属组:r--
mask::rw-                  #掩码::rw-
other::r-x                 #other:其他人:r-x


#分别使用jack,alice以及其他用户测试
[root@localhost ~]# su alice
[alice@localhost root]$ cat /home/test.txt 
alice可以看和编辑
jack什么也做不了
其他用户可以看和执行

[alice@localhost root]$ vim /home/test.txt 
[alice@localhost root]$ ./home/texst.txt
bash: ./home/texst.txt: 权限不够
[alice@localhost root]$ exit
exit
[root@localhost ~]# su jack
[jack@localhost root]$ cd /home/
[jack@localhost home]$ cat test.txt 
cat: test.txt: 权限不够
[jack@localhost home]$ vim test.txt 
[jack@localhost home]$ ./test.txt
bash: ./test.txt: 权限不够
[jack@localhost home]$ exit
exit
[root@localhost ~]# su kk4real
[kk4real@localhost root]$ cd /home/
[kk4real@localhost home]$ cat test.txt 
alice可以看和编辑
jack什么也做不了
其他用户可以看和执行

[kk4real@localhost home]$ vim test.txt 
[kk4real@localhost home]$ ./test.txt
./test.txt:行1: alice可以看和编辑: 未找到命令
./test.txt:行2: jack什么也做不了: 未找到命令
./test.txt:行3: 其他用户可以看和执行: 未找到命令
#成功实现使用ACL对不同用户的文件权限的修改。

*查看和删除

-x 删除 remove
-b 删除所有 remove all

#增加jishuzu对text.txt文件读取的权限
[root@localhost ~]# setfacl -m g:jishuzu:r /home/test.txt
#使用在jishuzu内的用户user200测试
[root@localhost ~]# su user200
[user200@localhost root]$ cd /home/
[user200@localhost home]$ cat test.txt 
alice可以看和编辑
jack什么也做不了
其他用户可以看和执行

[user200@localhost home]$ 
#删除技术组的所有权限,删除other的所有权限
[root@localhost ~]# setfacl -x g:jishuzu /home/test.txt
[root@localhost ~]# setfacl -m o::-  /home/test.txt 
#使用技术组中的user200进行测试
[root@localhost ~]# su  user200
[user200@localhost root]$ cat /home/test.txt 
cat: /home/test.txt: 权限不够
[user200@localhost root]$ exit
exit
#使用用户kk4real进行测试
[root@localhost ~]# su kk4real
[kk4real@localhost root]$ cat /home/test.txt 
cat: /home/test.txt: 权限不够
[kk4real@localhost root]$ 

#另外可以使用删除用户所有权限的命令
[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::---
#此时可以看到除了root以外都没有权限了

特殊权限

  • 特殊位suid

suid针对文件/程序时,具备临时获得属主的权限。

示例:设置suid,使普通用户通过suid临时提权,查看超管root用户的文件

1.为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
[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 - alice
[alice@localhost ~]$ cat /root/file1.txt
#结果,普通用户,看到了root的内容。这个行为很危险,请在试验后,将cat的suid权限除去。
[root@localhost ~]# chmod u-s /usr/bin/cat
[root@localhost ~]# ll  /usr/bin/cat
  • 文件属性chattr

用途:常用于锁定某个文件,拒绝修改。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XMViNMvR-1595948137332)(C:\Users\夜话\AppData\Roaming\Typora\typora-user-images\image-20200728223336982.png)]

使用案例:

#1 先创建新文件进行对比。查看默认权限。
[root@localhost ~]# touch file100
[root@localhost ~]# lsattr file100
-------------- file100
#2 加上不能更改,重命名,删除的属性。
[root@localhost ~]# chattr +i file100
#3.查看不同的属性
[root@lcoalhost ~]# lsattr file100
----i--------- file100
#4.尝试删除
[root@localhost ~]# rm -rf file100 
rm: cannot remove `file100': Operation not permitted
#5.将属性还原
[root@localhost ~]# chattr -i file100
  • 进程掩码umask

    新建文件、目录的默认权限会受到umask的影响,umask表示要减掉的权限

    #示例1: 在shell进程中创建文件,先查看当前用户的umask权限
    [root@localhost ~]# umask 			
    0022
    [root@localhost ~]# touch file800
    [root@localhost ~]# mkdir dir800
    [root@localhost ~]# ll -d dir800 file800 
    drwxr-xr-x. 2 root root 4096 3月  11 19:40 dir800
    -rw-r--r--. 1 root root    0 3月  11 19:40 file800
    
    #示例2:修改shell umask值(临时)
    [root@localhost ~]# umask 000
    [root@localhost ~]# mkdir dir900
    [root@localhost ~]# touch file900
    [root@localhost ~]# ll -d dir900 file900 
    drwxrwxrwx. 2 root root 4096 3月  11 19:44 dir900
    -rw-rw-rw-. 1 root root    0 3月  11 19:44 file900
    

猜你喜欢

转载自blog.csdn.net/weixin_44898311/article/details/107621822
今日推荐