• Linux 权限相关例题练习


    目录

    一、前期准备工作:

    1)新建redhat用户

    2)新建testdir目录及其file1

    二、例题详解

    1、当用户redhat对/testdir目录无写权限时,该目录下的只读文件file1是否可修改和删除?

    2、复制/etc/fstab文件到/var/tmp下,设置文件所有者为redhat,有读写权限,所属组为cxk组,有读写权限,其他人无权限​编辑

    3、要求在/tmp/testdir里创建的新文件自动属于g1组,组g2的成员如alice能对这些新文件有读写权限,组g3的成员如tom只能对新文件有读权限,其他用户(不属于g1,g2,g3)不能访问这个文件夹

    4.让普通用户有能力使用cat文件阅读/etc/shadow文件

    5.创建/tmp/aaaa/目录,该目录中的文件只能文件拥有者可以删除


    一、前期准备工作:

    1)新建redhat用户

    1. [root@localhost ~]# useradd redhat
    2. [root@localhost ~]# echo 123 | passwd --stdin redhat
    3. Changing password for user redhat.
    4. passwd: all authentication tokens updated successfully.

    2)新建testdir目录及其file1

    1. [redhat@localhost ~]$ mkdir testdir
    2. [redhat@localhost ~]$ chmod u="r-x" testdir
    3. [redhat@localhost ~]$ cd testdir
    4. [redhat@localhost testdir]$ touch file1
    5. [redhat@localhost testdir]$ ll
    6. total 0
    7. -rw-r--r--. 1 redhat redhat 0 Sep 25 21:46 file1
    8. [redhat@localhost testdir]$ chmod u="r--" file1
    9. [redhat@localhost testdir]$ ll
    10. total 0
    11. -r--r--r--. 1 redhat redhat 0 Sep 25 21:46 file1

    二、例题详解

    1、当用户redhat对/testdir目录无写权限时,该目录下的只读文件file1是否可修改和删除?

    修改:(尝试修改文件的属主)————失败

    1. [redhat@localhost testdir]$ chown root file1
    2. chown: changing ownership of 'file1': Operation not permitted

    删除:——失败

    1. [redhat@localhost testdir]$ ll
    2. total 0
    3. -r--r--r--. 1 redhat redhat 0 Sep 25 21:52 file1
    4. [redhat@localhost testdir]$ rm -f file1
    5. rm: cannot remove 'file1': Permission denied

    因此, 当用户redhat对/testdir目录无写权限时,该目录下的只读文件file1不可修改和删除

    若对目录有写权限和执行权限,则对file1不能修改但可以删除

    2、复制/etc/fstab文件到/var/tmp下,设置文件所有者为redhat,有读写权限,所属组为cxk组,有读写权限,其他人无权限

    1. [redhat@localhost testdir]$ cp /etc/fstab /var/tmp
    2. [redhat@localhost testdir]$ ll /var/tmp
    3. total 4
    4. -rw-r--r--. 1 redhat redhat 579 Sep 25 21:59 fstab
    5. ...
    6. # 设置文件所有者
    7. [redhat@localhost /]$ sudo chown redhat:cxk /var/tmp/fstab

    由于Redhat无法成功创建cxk组,在网上查找资料后,要加上sudo命令,但Redhat也没有权限进行sudo,故给Redhat加上sudo权限

    1. [redhat@localhost tmp]$ groupadd -g 1004 cxk
    2. groupadd: Permission denied. # 无法成功创建cxk
    3. groupadd: cannot lock /etc/group; try again later.
    4. [redhat@localhost /]$ sudo groupadd cxk
    5. [sudo] password for redhat:
    6. redhat is not in the sudoers file. This incident will be reported. # 无使用sudo权限

     修改 /etc/sudoers 配置文件,使Redhat有权限使用sudo命令

    1. [redhat@localhost /]$ su
    2. Password:
    3. [root@localhost /]# vi /etc/sudoers
    4. [root@localhost /]# exit
    5. exit
    6. [redhat@localhost /]$

    此时可成功创建cxk

    1. [redhat@localhost /]$ sudo groupadd cxk
    2. [redhat@localhost /]$ cat /etc/group
    3. ...
    4. cxk:x:1112:

     设置文件所有者:

    此时redhat、cxk分别对fstab有读写操作和读操作

    修改如下:

    设置redhat有读写权限,cxk组有读写权限,其他人无权限

    1. [redhat@localhost tmp]$ chmod g+w fstab
    2. [redhat@localhost tmp]$ chmod o-r fstab

    3、要求在/tmp/testdir里创建的新文件自动属于g1组,组g2的成员如alice能对这些新文件有读写权限,组g3的成员如tom只能对新文件有读权限,其他用户(不属于g1,g2,g3)不能访问这个文件夹

    1. #创建g1,g2,g3组
    2. [redhat@localhost /]$ sudo groupadd g1
    3. [redhat@localhost /]$ sudo groupadd g2
    4. [redhat@localhost /]$ sudo groupadd g3
    5. [redhat@localhost testdir]$ sudo useradd -G g2 alice
    6. [redhat@localhost testdir]$ sudo useradd -G g3 tom
    7. #创建testdir目录
    8. [redhat@localhost tmp]$ mkdir testdir
    9. #再在testdir中创建data
    10. [redhat@localhost testdir]$ mkdir data
    11. [redhat@localhost testdir]$ sudo chgrp g1 data
    12. [redhat@localhost testdir]$ chmod g+s data
    13. [redhat@localhost testdir]$ setfacl -m g:g2:rw data
    14. [redhat@localhost testdir]$ setfacl -m g:g3:r data
    15. [redhat@localhost testdir]$ chmod o=- data
    16. [redhat@localhost testdir]$ getfacl data #查看data相关信息

     

    4.让普通用户有能力使用cat文件阅读/etc/shadow文件

    普通用户在未提权前无法查看 /etc/shadow文件

    1. [redhat@localhost var]$ cat /etc/shadow
    2. cat: /etc/shadow: Permission denied

    5.创建/tmp/aaaa/目录,该目录中的文件只能文件拥有者可以删除

    1. # 创建目录
    2. [redhat@localhost tmp]$ mkdir aaaa
    3. # 给满权限
    4. [redhat@localhost tmp]$ chmod 777 aaaa/
    5. [redhat@localhost tmp]$ ll aaaa/ -d
    6. drwxrwxrwx. 2 redhat redhat 6 Sep 26 09:49 aaaa/
    7. # 设置Sticky Bit权限
    8. [redhat@localhost tmp]$ chmod a=rwxrwxrwt aaaa/
    9. [redhat@localhost tmp]$ ll aaaa/ -d
    10. drwxrwxrwt. 2 redhat redhat 6 Sep 26 09:49 aaaa/

    1. # 在aaaa中创建a文件
    2. [redhat@localhost tmp]$ cd aaaa
    3. [redhat@localhost aaaa]$ touch a
    4. # 其他用户zx对该文件进行删除操作,删除失败
    5. [zx@localhost ~]$ cd /tmp/aaaa
    6. [zx@localhost aaaa]$ ll
    7. total 0
    8. -rw-r--r--. 1 redhat redhat 0 Sep 26 09:54 a
    9. [zx@localhost aaaa]$ rm -rf a
    10. rm: cannot remove 'a': Operation not permitted
    11. # 原用户redhat进行操作,删除成功!
    12. [redhat@localhost aaaa]$ rm -f a
    13. [redhat@localhost aaaa]$ ll
    14. total 0

  • 相关阅读:
    C++11闭包函数的几种实现方法
    软考-系统架构师-计算机与网络基础知识-数据库系统基础知识
    信道复用技术
    9、学习MySQL DELETE 语句
    EN 13859-2防水用柔性薄板—CE认证
    STM32笔记-AD模数转换
    微服务实战系列之Sentinel
    Redis——Java客户端配置
    vue3.2封装一个listTable组件
    Pytest自动化测试实战之执行参数
  • 原文地址:https://blog.csdn.net/weixin_68256171/article/details/133281036