zoukankan      html  css  js  c++  java
  • day13 Linux系统权限位介绍

    day13 Linux系统权限位介绍

    Linux系统权限位介绍

    1.什么是权限

    在Linux系统中,如果我们想限制某个用户对系统的操作,就可以通过设置不同的权限达到我们想要的效果。

    2.用户与用户组和权限的关系

    通过前面用户管理我们知道,Linux里针对文件定义了三种角色,分别是所属用户(owner),所属组(group),其他用户(others),每一种角色又对应了三种权限,分别是可读,可写,可执行。

    当用户访问文件时,流程如下:

    1.如果是文件的所有者,则按所有者的权限进行操作。

    2.如果是文件的所属组,则按所属组的权限进行操作。

    3.如果是文件不是所属用户,也不是所属组,则按其他用户进行权限进行操作。

    3.文件权限位说明

    在Linux中,每个文件或目录都有一组共有9个基础权限位,每三位字符分为一组,它们分别是所属用户权限位,用户组权限位,其他用户权限位。最终形式"rw-r--r--"。

    Linux正是通过这9个权限位来控制文件用户、用户组以及其他用户对文件的访问权限的。

    rwx r-x r-x
    所属用户的权限 所属用户组的权限 其他用户的权限

    4.rwx权限的含义

    字母权限 数字权限 权限含义
    r(read) 4 读取权限
    w(write) 2 写入权限
    x(execut) 1 执行权限
    - 0 没有权限

    第二章 Linux文件及目录权限

    1.文件权限和目录权限区别

    虽然文件和目录的权限都是rwx三种,但是Linux系统中对文件和目录的权限是不同含义和区别的。

    权限 对文件的作用 对目录的作用
    r读取权限 具有读取,浏览文件内容的权限 具有浏览目录及其子目录的权限
    w写入权限 具有修改,增加,删除文件内容的权限 具有增加,删除或修改目录内文件的权限
    x执行权限 具有执行文件的权限 具有进入目录的权限

    2.文件权限实验

    涉及到命令:

    chmod 【ugoa】【-+=】flie

    实验流程:

    使用普通用户测试以下权限的目录:

    ---
    r--
    rw-
    r-x
    -w-
    -wx
    --x
    rwx
    

    实验过程:---权限测试

    1.root用户去掉文件的其他用户可读属性
    [root@centos7-100 owner]# ll
    总用量 4
    -rw-r-----. 1 root root 6 10月  2 22:24 owner.txt
    2.切换到普通用户查看文件权限
    [root@centos7-100 owner]# su - oldboy
    上一次登录:日 10月  3 10:15:31 CST 2021pts/0 上
    [oldboy@centos7-100 ~]$ cd /opt/owner/
    -bash: cd: /opt/owner/: 权限不够                               #因为没有x权限不能进入目录
    
    3.使用root用户增加目录r的权限
    [root@centos7-100 opt]# chmod o=r owner/
    [root@centos7-100 opt]# ll
    总用量 0
    drwxr-xr-x. 2 root root 24 10月  3 10:23 others
    drwxr-xr--. 2 root root 23 10月  3 10:23 owner
    [oldboy@centos7-100 opt]$ cd owner
    -bash: cd: owner: 权限不够
    [oldboy@centos7-100 opt]$ ls owner
    ls: 无法访问owner/owner.txt: 权限不够
    owner.txt                                                  #因为只有r权限所以只能查看到owner目录下面有一个owner.txt文件
    
    4.使用root用户增加目录w权限
    [root@centos7-100 opt]# chmod o=w owner
    [root@centos7-100 opt]# ll
    总用量 0
    drwxr-xr-x. 2 root root 24 10月  3 10:23 others
    drwxr-x-w-. 2 root root 23 10月  3 10:23 owner
    [root@centos7-100 opt]# ll
    总用量 0
    drwxr-xr-x. 2 root root 24 10月  3 10:23 others
    drwxr-xrwx. 2 root root 23 10月  3 10:23 owner
    [root@centos7-100 opt]# chmod o-r owner
    [root@centos7-100 opt]# ls
    others  owner
    [root@centos7-100 opt]# su - oldboy
    上一次登录:日 10月  3 11:33:09 CST 2021pts/0 上
    [oldboy@centos7-100 ~]$ ls
    [oldboy@centos7-100 ~]$ cd /opt
    [oldboy@centos7-100 opt]$ ls
    others  owner
    [oldboy@centos7-100 opt]$ cd owner
    [oldboy@centos7-100 owner]$ ls
    ls: 无法打开目录.: 权限不够
    [oldboy@centos7-100 owner]$ mkdir zhaocheng             #增加x权限就可以进入添加目录
    
    5.只有一个r权限测试
    [root@centos7-100 ~]# chmod 0=r /opt/owner
    chmod: 无效模式:"0=r"
    Try 'chmod --help' for more information.
    [root@centos7-100 ~]# chmod 0=rwx /opt/owner
    chmod: 无效模式:"0=rwx"
    Try 'chmod --help' for more information.
    [root@centos7-100 ~]# chmod o=r /opt/owner
    [root@centos7-100 ~]# su - oldboy
    上一次登录:日 10月  3 11:43:48 CST 2021pts/0 上
    [oldboy@centos7-100 ~]$ cd /ot
    -bash: cd: /ot: 没有那个文件或目录
    [oldboy@centos7-100 ~]$ cd /opt
    [oldboy@centos7-100 opt]$ ll owner/
    ls: 无法访问owner/owner.txt: 权限不够
    ls: 无法访问owner/zhaocheng: 权限不够
    总用量 0
    -????????? ? ? ? ?            ? owner.txt
    d????????? ? ? ? ?            ? zhaocheng
    
    

    使用普通用户测试以下权限的文件:

    ---
    r--
    rw-
    r-x
    -w-
    -wx
    --x
    rwx
    

    实验过程:--权限测试

    [root@centos7-100 ~]# cd /opt
    [root@centos7-100 opt]# ls
    others  owner
    [root@centos7-100 opt]# ll
    总用量 0
    drwxr-xr-x. 2 root root 24 10月  3 10:23 others
    drwxr-xr--. 3 root root 40 10月  3 11:44 owner
    [root@centos7-100 opt]# ll owner
    总用量 4
    -rw-r-----. 1 root   root   6 10月  2 22:24 owner.txt
    drwxrwxr-x. 2 oldboy oldboy 6 10月  3 11:44 zhaocheng    #查看文件的权限
    
    1.增加文件r权限
    实验:
    [oldboy@centos7-100 opt]$ ll owner/
    总用量 4
    -rw-r--r--. 1 root   root   6 10月  2 22:24 owner.txt
    drwxrwxr-x. 2 oldboy oldboy 6 10月  3 11:44 zhaocheng
    [oldboy@centos7-100 opt]$ cd owner/
    [oldboy@centos7-100 owner]$ cat owner.txt 
    owner      
    [oldboy@centos7-100 owner]$ echo 123 >>owner.txt 
    -bash: owner.txt: 权限不够                                                  #只有r权限
    
    
    2.增加w的权限
    [root@centos7-100 owner]# ll
    总用量 4
    -rw-r--r--. 1 root root 6 10月  3 16:49 owner.txt
    [root@centos7-100 owner]# chmod o+w owner.txt 
    [root@centos7-100 owner]# ll
    总用量 4
    -rw-r--rw-. 1 root root 6 10月  3 16:49 owner.txt
    [root@centos7-100 owner]# su - oldboy
    上一次登录:日 10月  3 11:59:59 CST 2021pts/0 上
    [oldboy@centos7-100 ~]$ cd /opt/owner/
    [oldboy@centos7-100 owner]$ echo oldboy >>owner.txt 
    [oldboy@centos7-100 owner]$ cat owner.txt 
    a
    b
    c
    oldboy
    
    3.增加x的权限
    [root@centos7-100 owner]# su - oldboy
    上一次登录:日 10月  3 11:59:59 CST 2021pts/0 上
    [oldboy@centos7-100 ~]$ cd /opt/owner/
    [oldboy@centos7-100 owner]$ echo oldboy >>owner.txt 
    [oldboy@centos7-100 owner]$ cat owner.txt 
    a
    b
    c
    oldboy
    [oldboy@centos7-100 owner]$ ./owner.txt
    -bash: ./owner.txt: 权限不够
    [oldboy@centos7-100 owner]$ su - root
    密码:
    上一次登录:日 10月  3 16:47:27 CST 2021pts/0 上
    [root@centos7-100 ~]# cd /opt/owner/
    [root@centos7-100 owner]# chmod o+x owner.txt 
    [root@centos7-100 owner]# su - oldboy
    上一次登录:日 10月  3 16:51:17 CST 2021pts/0 上
    [oldboy@centos7-100 ~]$ cd /opt/owner
    [oldboy@centos7-100 owner]$ ./owner.txt 
    ./owner.txt:行1: a: 未找到命令
    ./owner.txt:行2: b: 未找到命令
    ./owner.txt:行3: c: 未找到命令
    ./owner.txt:行4: oldboy: 未找到命令                                # 增加了x即执行的权限
    

    第三章 Linux权限命令

    1.chmod 更改文件或目录的权限

    命令说明:

    1.chmod 命令是用来改变文件或目录权限的命令
    2.但是只有文件的属主和超级用户root才能够执行这个命令
    

    命令格式:

    chmod 支持两种修改权限的模式一种是字母表达,一种是数字表达

    chmod [ugoa][-+=][rwx][文件或目录]
    chmod |7|6|5|4|3|2|1| 【文件或目录】
    

    用户位说明:

    u 所属用户
    g 所属组
    o 其他用户
    a 代表所有ugo
    

    操作字符说明:

    - 取消权限
    + 添加权限
    = 取消所有的权限,然后赋予给定的权限
    

    关键参数:

    -R 递归处理指定的目录及子目录下的所有文件
    

    命令实践:

    -rwx------. 1 oldboy oldboy 7 10月  2 22:24 others.txt
    [oldboy@centos7-100 others]$ chmod o=rwx others.txt 
    [oldboy@centos7-100 others]$ ll
    总用量 4
    -rwx---rwx. 1 oldboy oldboy 7 10月  2 22:24 others.txt
    [oldboy@centos7-100 others]$ chmod u=rwx others.txt 
    [oldboy@centos7-100 others]$ ll
    总用量 4
    -rwx---rwx. 1 oldboy oldboy 7 10月  2 22:24 others.txt
    [oldboy@centos7-100 others]$ chmod g=rwx others.txt 
    [oldboy@centos7-100 others]$ ll
    总用量 4
    -rwxrwxrwx. 1 oldboy oldboy 7 10月  2 22:24 others.txt
    [oldboy@centos7-100 others]$ chmod 774 others.txt 
    [oldboy@centos7-100 others]$ ll
    总用量 4
    -rwxrwxr--. 1 oldboy oldboy 7 10月  2 22:24 others.txt
    [oldboy@centos7-100 others]$ chmod a= others.txt 
    [oldboy@centos7-100 others]$ ll
    总用量 4
    ----------. 1 oldboy oldboy 7 10月  2 22:24 others.txt
    

    2.chown更改文件或目录的所属用户及用户组

    命令语法:

    chown 【选项】【用户:用户组】【文件或目录】
    chown 【选项】【用户.用户组】 【文件或目录】
    

    注意:

    chown 需要超级用户root的权限才能执行此命令。
    只有超级用户和属于组的文件所有者才能变更文件关联组。非超级用户如需要设置关联组可能需要使用 [chgrp]命令。
    

    常用写法:

    同时更改用户和用户组
    

    实例:

    1.不是root用户,普通用户不能修改所属主和所属组的权限
    
    [oldboy@centos7-100 others]$ chown .root others.txt 
    chown: 正在更改"others.txt" 的所属组: 不允许的操作
    [oldboy@centos7-100 others]$ cd ..
    [oldboy@centos7-100 opt]$ ll
    总用量 0
    drwxr-xr-x. 2 oldboy oldboy 24 10月  3 10:23 others
    drwxr-xrwx. 2 root   root   23 10月  3 16:48 owner
    [oldboy@centos7-100 opt]$ chown -R .root others
    chown: 正在更改"others/others.txt" 的所属组: 不允许的操作
    chown: 正在更改"others" 的所属组: 不允许的操作
    [oldboy@centos7-100 opt]$ chown -R root.root others
    chown: 正在更改"others/others.txt" 的所有者: 不允许的操作
    chown: 正在更改"others" 的所有者: 不允许的操作
    
    [oldboy@centos7-100 opt]$ ls
    others  owner
    [oldboy@centos7-100 opt]$ cd others/
    [oldboy@centos7-100 others]$ ls
    others.txt
    [oldboy@centos7-100 others]$ ll
    总用量 4
    ----------. 1 oldboy oldboy 7 10月  2 22:24 others.txt
    [oldboy@centos7-100 others]$ chmod root. others.txt 
    chmod: 无效模式:"root."
    Try 'chmod --help' for more information.
    [oldboy@centos7-100 others]$ chown root. others.txt 
    chown: 正在更改"others.txt" 的所有者: 不允许的操作
    [oldboy@centos7-100 others]$ su - oldboy
    密码:
    ^C
    
    
    2.切换root用户登录
    [oldboy@centos7-100 others]$ su - root
    密码:
    上一次登录:日 10月  3 17:03:35 CST 2021pts/0 上
    [root@centos7-100 ~]# cd /opt
    [root@centos7-100 opt]# l
    -bash: l: 未找到命令
    [root@centos7-100 opt]# ll
    总用量 0
    drwxr-xr-x. 2 oldboy oldboy 24 10月  3 10:23 others
    drwxr-xrwx. 2 root   root   23 10月  3 16:48 owner
    [root@centos7-100 opt]# cd others/
    [root@centos7-100 others]# ls
    others.txt
    [root@centos7-100 others]# l
    -bash: l: 未找到命令
    [root@centos7-100 others]# ll
    总用量 4
    ----------. 1 oldboy oldboy 7 10月  2 22:24 others.txt
    [root@centos7-100 others]# cd ..
    
    
    3.只修改所属组权限
    [root@centos7-100 opt]# chown .root others
    [root@centos7-100 opt]# ll
    总用量 0
    drwxr-xr-x. 2 oldboy root 24 10月  3 10:23 others
    drwxr-xrwx. 2 root   root 23 10月  3 16:48 owner
    
    
    4.只修改所属用户的权限
    [root@centos7-100 opt]# chown root. others
    [root@centos7-100 opt]# ll
    总用量 0
    drwxr-xr-x. 2 root root 24 10月  3 10:23 others
    drwxr-xrwx. 2 root root 23 10月  3 16:48 owner
    [root@centos7-100 opt]# cd others/
    [root@centos7-100 others]# ll
    总用量 4
    ----------. 1 oldboy oldboy 7 10月  2 22:24 others.txt
    [root@centos7-100 others]# cd ..
    
    
    5.修改所属用户和用户组的权限加-R
    [root@centos7-100 opt]# chown -R root.root others
    [root@centos7-100 opt]# ll
    总用量 0
    drwxr-xr-x. 2 root root 24 10月  3 10:23 others
    drwxr-xrwx. 2 root root 23 10月  3 16:48 owner
    [root@centos7-100 opt]# cd others/
    [root@centos7-100 others]# ll
    总用量 4
    ----------. 1 root root 7 10月  2 22:24 others.txt
    
  • 相关阅读:
    团队项目-需求分析
    设计模式第二次作业
    设计模式第一次作业
    高级软件工程团队作业(选题报告)
    高级软件工程团队结对作业(学生部门智能匹配)
    高级软件工程团队第一次作业
    高级软件工程结对作业
    高级软件工程第二次作业
    高级软件工程第一次作业
    POJ 2752 Seek the Name, Seek the Fame next数组理解加深
  • 原文地址:https://www.cnblogs.com/zhaocheng690/p/15387212.html
Copyright © 2011-2022 走看看