• Linux权限总结


    放弃不难,但坚持很酷,加油!希望此文对您有所帮助!

    shell运行原理—外壳程序

    在这里插入图片描述

    Linux权限的概念

    Linux下有两种用户:超级用户(root)、普通用户。
    超级用户:可以在linux系统做任何事情,不受限制。
    普通用户:在linux下做有限的事情
    超级用户的命令提示符是“#“,普通用户的命令提示符是”$"。
    命令:su [用户名]
    功能:切换用户。
    例如,要从root用户切换到普通用户user,则使用su user。要从普通用户user切换到root用户则使用su root(root可以管理),此时系统会提示输入root用户的口令。

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    如何修改文件权限?

    改人
    在这里插入图片描述

    改属性 (ugo ± ,八进制方案)
    在这里插入图片描述
    在这里插入图片描述

    常见权限问题(面试题)

    1.目录权限

    目录权限(在目录下操作,看的是此目录的相关权限)
    进入一个目录,需要什么权限? x权限(易错)
    查看目录下的文件列表:r
    要在目录下创建文件或者目录:在这里插入图片描述
    在这里插入图片描述

    可执行权限:如果目录没有可执行权限,则无法cd到目录中。
    可读权限:如果目录没有可读权限,则无法使用ls等命令查看目录中的文件内容。
    可写权限:如果目录没有可写权限,则无法在目录中创建文件,也无法在目录中删除文件。

    2.umask

    umask:权限掩码
    凡是在umask中出现的权限,都不应该在最终权限中出现!
    在这里插入图片描述

    为何我们创建一个目录或者文件,默认权限是你所看到的样子?(如上图所示)
    1.目录起始权限是777,普通文件起始权限是666
    2.最终权限 = 起始权限&(~umask)
    在这里插入图片描述
    虽然说默认的umask是0002,但是我们可以自己手动改,得到我们所需要的。
    比如如果我们想让权限都为空,可以设置为umask 0777

    在这里插入图片描述

    3.写权限的作用

    [jyf@VM-12-14-centos ~]$ cd lesson4
    [jyf@VM-12-14-centos lesson4]$ ll
    total 8
    drwxrwxr-x 2 jyf jyf 4096 Sep 16 20:18 dir
    d--------- 2 jyf jyf 4096 Sep 18 19:12 director
    -rw-rw-r-- 1 jyf jyf    0 Sep 16 20:18 test.c
    ---------- 1 jyf jyf    0 Sep 18 19:11 test.cpp
    [jyf@VM-12-14-centos lesson4]$ umask
    0002
    [jyf@VM-12-14-centos lesson4]$ chmod u-w dir //将目录dir的拥有者的写权限去掉
    [jyf@VM-12-14-centos lesson4]$ ll
    total 8
    dr-xrwxr-x 2 jyf jyf 4096 Sep 16 20:18 dir
    d--------- 2 jyf jyf 4096 Sep 18 19:12 director
    -rw-rw-r-- 1 jyf jyf    0 Sep 16 20:18 test.c
    ---------- 1 jyf jyf    0 Sep 18 19:11 test.cpp
    [jyf@VM-12-14-centos lesson4]$ cd dir
    [jyf@VM-12-14-centos dir]$ touch other
    touch: cannot touch ‘other’: Permission denied //文件所在的目录没有写权限,无法创建文件
    [jyf@VM-12-14-centos dir]$ cd ..
    [jyf@VM-12-14-centos lesson4]$ chmod u+w dir
    [jyf@VM-12-14-centos lesson4]$ ll
    total 8
    drwxrwxr-x 2 jyf jyf 4096 Sep 16 20:18 dir
    d--------- 2 jyf jyf 4096 Sep 18 19:12 director
    -rw-rw-r-- 1 jyf jyf    0 Sep 16 20:18 test.c
    ---------- 1 jyf jyf    0 Sep 18 19:11 test.cpp
    [jyf@VM-12-14-centos lesson4]$ cd dir
    [jyf@VM-12-14-centos dir]$ touch test.c
    [jyf@VM-12-14-centos dir]$ ll
    total 0
    -rw-rw-r-- 1 jyf jyf 0 Sep 27 19:43 test.c
    [jyf@VM-12-14-centos dir]$ touch file.txt
    [jyf@VM-12-14-centos dir]$ ll
    total 0
    -rw-rw-r-- 1 jyf jyf 0 Sep 27 19:51 file.txt
    -rw-rw-r-- 1 jyf jyf 0 Sep 27 19:43 test.c
    [jyf@VM-12-14-centos dir]$ cd ..
    [jyf@VM-12-14-centos lesson4]$ chmod u-w dir
    [jyf@VM-12-14-centos lesson4]$ cd dir
    [jyf@VM-12-14-centos dir]$ rm test.c
    rm: cannot remove ‘test.c’: Permission denied //文件所在的目录没有写权限,无法删除文件
    [jyf@VM-12-14-centos dir]$ rm -f test.c
    rm: cannot remove ‘test.c’: Permission denied
    [jyf@VM-12-14-centos dir]$ cd ..
    [jyf@VM-12-14-centos lesson4]$ rm dir
    rm: cannot remove ‘dir’: Is a directory
    [jyf@VM-12-14-centos lesson4]$ rm -rf dir //目录没有写权限,无法删除目录
    rm: cannot remove ‘dir/test.c’: Permission denied
    rm: cannot remove ‘dir/file.txt’: Permission denied
    [jyf@VM-12-14-centos lesson4]$ ll
    total 8
    dr-xrwxr-x 2 jyf jyf 4096 Sep 27 19:51 dir
    d--------- 2 jyf jyf 4096 Sep 18 19:12 director
    -rw-rw-r-- 1 jyf jyf    0 Sep 16 20:18 test.c
    ---------- 1 jyf jyf    0 Sep 18 19:11 test.cpp
    [jyf@VM-12-14-centos lesson4]$ 
    
    

    4.如何创建一个共享目录(粘滞位)

    可执行权限:如果目录没有可执行权限,则无法cd到目录中。
    可读权限:如果目录没有可读权限,则无法使用ls等命令查看目录中的文件内容。
    可写权限:如果目录没有可写权限,则无法在目录中创建文件,也无法在目录中删除文件。

    于是问题来了,换句话说,就是只要用户拥有目录的写权限,用户就可以删除目录中的文件,而不论这个用户是否有这个文件的写权限。
    这好像不太科学啊,就像我张三创建的一个文件,凭什么可以被你李四删除?我们用下面的过程印证一下。

    [jyf@VM-12-14-centos lesson4]$ cd /
    [jyf@VM-12-14-centos /]$ ll
    total 76
    lrwxrwxrwx.   1 root root     7 Mar  7  2019 bin -> usr/bin
    dr-xr-xr-x.   5 root root  4096 Jul 28 11:37 boot
    drwxr-xr-x    2 root root  4096 Nov  5  2019 data
    drwxr-xr-x   19 root root  3020 Aug 26 08:05 dev
    drwxr-xr-x.  95 root root 12288 Sep 27 22:23 etc
    drwxr-xr-x.   5 root root  4096 Sep 27 20:57 home
    lrwxrwxrwx.   1 root root     7 Mar  7  2019 lib -> usr/lib
    lrwxrwxrwx.   1 root root     9 Mar  7  2019 lib64 -> usr/lib64
    drwx------.   2 root root 16384 Mar  7  2019 lost+found
    drwxr-xr-x.   2 root root  4096 Apr 11  2018 media
    drwxr-xr-x.   2 root root  4096 Apr 11  2018 mnt
    drwxrwxrwx    2 root root  4096 Sep 27 22:34 mytemp
    drwxr-xr-x.   4 root root  4096 Aug 26 08:03 opt
    dr-xr-xr-x  102 root root     0 Aug 26 08:05 proc
    dr-xr-x---.   7 root root  4096 Sep 27 22:24 root
    drwxr-xr-x   25 root root   880 Aug 26 08:06 run
    lrwxrwxrwx.   1 root root     8 Mar  7  2019 sbin -> usr/sbin
    drwxr-xr-x.   2 root root  4096 Apr 11  2018 srv
    dr-xr-xr-x   13 root root     0 Sep 27 20:28 sys
    drwxrwxrwt.   8 root root  4096 Sep 28 03:24 tmp
    drwxr-xr-x.  14 root root  4096 Jan  8  2021 usr
    drwxr-xr-x.  20 root root  4096 Jan  8  2021 var
    [jyf@VM-12-14-centos /]$ cd mytemp
    [jyf@VM-12-14-centos mytemp]$ ll
    total 0
    -rwx------ 1 jyf jyf 0 Sep 27 22:31 jyf1
    -rwx------ 1 jyf jyf 0 Sep 27 22:31 jyf2
    -rwx------ 1 jyf jyf 0 Sep 27 22:31 jyf3
    -rw-rw-r-- 1 yfj yfj 0 Sep 27 22:34 yfj1
    -rw-rw-r-- 1 yfj yfj 0 Sep 27 22:34 yfj2
    -rw-rw-r-- 1 yfj yfj 0 Sep 27 22:34 yfj3
    [jyf@VM-12-14-centos mytemp]$ su
    Password: 
    [root@VM-12-14-centos mytemp]# su yfj
    [yfj@VM-12-14-centos mytemp]$ whoami
    yfj
    [yfj@VM-12-14-centos mytemp]$ cat jyf1
    cat: jyf1: Permission denied
    [yfj@VM-12-14-centos mytemp]$ ./jyf2
    bash: ./jyf2: Permission denied
    [yfj@VM-12-14-centos mytemp]$ echo "hello world">jyf1
    bash: jyf1: Permission denied
    [yfj@VM-12-14-centos mytemp]$ rm jyf1
    rm: remove write-protected regular empty file ‘jyf1’? y
    [yfj@VM-12-14-centos mytemp]$ ll
    total 0
    -rwx------ 1 jyf jyf 0 Sep 27 22:31 jyf2
    -rwx------ 1 jyf jyf 0 Sep 27 22:31 jyf3
    -rw-rw-r-- 1 yfj yfj 0 Sep 27 22:34 yfj1
    -rw-rw-r-- 1 yfj yfj 0 Sep 27 22:34 yfj2
    -rw-rw-r-- 1 yfj yfj 0 Sep 27 22:34 yfj3
    [yfj@VM-12-14-centos mytemp]$ ll ../mytemp -d
    drwxrwxrwx 2 root root 4096 Sep 28 20:39 ../mytemp
    
    

    从上述可以看出用户yfj在目录myemp中对用户jyf的文件不能读,不能写,不能执行这个文件,但是,它却可以删除,因为这个文件所在的目录的给所有人都开发了写权限,如果这个作为共享目录,不就乱套了吗?

    共享目录需要满足的条件(共享目录一般由超级管理员root在其根目录下创建,为多个用户所共享)

    1.当多个用户共享一个目录,需要在该目录下,进行读写,创建删除文件
    2.但是自己只能删除自己的,而不能删除别人的(w:可以互删,但是不满足条件)

    解决方法:给目录设置粘滞位,一般由谁(root)设置谁才能取消。
    当一个目录设置为“粘滞位”(用chmod + t),则该目录下的文件只能由
    1.超级管理员删除 2.该目录的所有者删除 3.该文件所有者删除

    [root@VM-12-14-centos mytemp]# chmod +t mytemp //**给mytemp设置粘滞位**
    [root@VM-12-14-centos /]# ll
    total 76
    lrwxrwxrwx.   1 root root     7 Mar  7  2019 bin -> usr/bin
    dr-xr-xr-x.   5 root root  4096 Jul 28 11:37 boot
    drwxr-xr-x    2 root root  4096 Nov  5  2019 data
    drwxr-xr-x   19 root root  3020 Aug 26 08:05 dev
    drwxr-xr-x.  95 root root 12288 Sep 27 22:23 etc
    drwxr-xr-x.   5 root root  4096 Sep 27 20:57 home
    lrwxrwxrwx.   1 root root     7 Mar  7  2019 lib -> usr/lib
    lrwxrwxrwx.   1 root root     9 Mar  7  2019 lib64 -> usr/lib64
    drwx------.   2 root root 16384 Mar  7  2019 lost+found
    drwxr-xr-x.   2 root root  4096 Apr 11  2018 media
    drwxr-xr-x.   2 root root  4096 Apr 11  2018 mnt
    drwxrwxrwt    2 root root  4096 Sep 28 20:39 mytemp
    drwxr-xr-x.   4 root root  4096 Aug 26 08:03 opt
    dr-xr-xr-x  113 root root     0 Aug 26 08:05 proc
    dr-xr-x---.   7 root root  4096 Sep 27 22:24 root
    drwxr-xr-x   25 root root   880 Aug 26 08:06 run
    lrwxrwxrwx.   1 root root     8 Mar  7  2019 sbin -> usr/sbin
    drwxr-xr-x.   2 root root  4096 Apr 11  2018 srv
    dr-xr-xr-x   13 root root     0 Sep 27 20:28 sys
    drwxrwxrwt.   8 root root  4096 Sep 28 03:24 tmp
    drwxr-xr-x.  14 root root  4096 Jan  8  2021 usr
    drwxr-xr-x.  20 root root  4096 Jan  8  2021 var
    [jyf@VM-12-14-centos mytemp]$ rm yfj1
    rm: remove write-protected regular empty file ‘yfj1’? y
    rm: cannot remove ‘yfj1’: Operation not permitted
    [jyf@VM-12-14-centos mytemp]$ touch jyf1
    [jyf@VM-12-14-centos mytemp]$ ll
    total 0
    -rw-rw-r-- 1 jyf jyf 0 Sep 28 20:45 jyf1
    -rwx------ 1 jyf jyf 0 Sep 27 22:31 jyf2
    -rwx------ 1 jyf jyf 0 Sep 27 22:31 jyf3
    -rw-rw-r-- 1 yfj yfj 0 Sep 27 22:34 yfj1
    -rw-rw-r-- 1 yfj yfj 0 Sep 27 22:34 yfj2
    -rw-rw-r-- 1 yfj yfj 0 Sep 27 22:34 yfj3
    
    

    关于权限的总结

    目录的可执行权限是表示你可否在目录下执行命令
    如果目录没有x权限,则无法对目录执行任何命令,甚至无法cd进入目录,即使目录仍然有 -r读权限(这个地方很容易犯错,认为有读权限就可以进入目录读取目录下的文件)
    而如果目录具有x权限,但没有 r 权限,则用户可以执行命令,可以cd进入目录。但由于没有目录的读权限。所在目录下,即使可以执行ls命令,但仍然没有权限读出目录下的文档。

    [jyf@VM-12-14-centos lesson4]$ ll
    total 4
    drwxrwxr-x 2 jyf jyf 4096 Sep 28 21:56 dir
    -rw-rw-r-- 1 jyf jyf    0 Sep 16 20:18 test.c
    [jyf@VM-12-14-centos lesson4]$ chmod u-r dir
    [jyf@VM-12-14-centos lesson4]$ ls
    dir  test.c
    [jyf@VM-12-14-centos lesson4]$ cd dir
    [jyf@VM-12-14-centos dir]$ ll
    ls: cannot open directory .: Permission denied
    
    

    linux权限的总结就到这里了,我们下次再见!!!

  • 相关阅读:
    Fastflow——基于golang的轻量级工作流框架
    三维模型3DTile格式轻量化压缩在移动智能终端应用方面的重要性分析
    校园广播站人员和节目管理系统
    薪资面谈小技巧
    CSS基础学习--5 background背景
    python、pyqt5实现人脸检测、性别和年龄预测
    【Rust 日报】2023-11-19 solars:可视化太阳系
    Linux指令大全(文件和目录操作、文件内容查看和编辑、系统信息和管理、网络和通信、压缩和解压缩、权限管理、包管理……)
    13 Go的错误处理
    猿创征文|《Java》【速学】类与继承
  • 原文地址:https://blog.csdn.net/m0_55283616/article/details/126249927