zoukankan      html  css  js  c++  java
  • 10.Linux用户权限

    1.权限基本概述

    image.png

    1. 什么是权限?

    我们可以把它理解为操作系统对用户能够执行的功能所设立的限制,主要用于约束用户能对系统所做的操作,以及内容访问的范围,或者说,权限是指某个特定的用户具有特定的系统资源使用权力。

    2. 为什么要有权限?

    因为系统中不可能只存在一个root用户,一定会存在多个用户,为了保护每个登陆用户的隐私和工作环境,所以就有了权限。

    3. 权限与用户之间的关系?

    在Linux系统中,针对文件定义了三种身份,分别是属主(owner)、属组(group)、其他人(others),每一种身份又对应三种权限,分别是可读(readable)、可写(writable)、可执行(excutable)。

    4. 权限中的rwx分别代表什么含义?

    当我们使用ls -l查看一个文件的详细属性时,能看到每个文件都有一个9位基本权限位,比如: rwxr-xr-x其中每三位字符为一组,分别表示属主权限位,属组权限位,匿名权限位。
    linux中基本权限位则是使用这9位字符来表示,主要控制文件属主(User)、属组(Group)、其他用户(Other)
    image.png

    • 方式一: ugo user group other all=ugo
    [root@yinwucheng ~]# touch file       创建文件
    [root@yinwucheng ~]# chmod a=rwx file      给所有人添加读写执行权限
    [root@yinwucheng ~]# ll
    total 0
    -rwxrwxrwx. 1 root root 0 Aug  7 17:54 file
    [root@yinwucheng ~]# chmod a=-rwx file   取消所有的权限
    [root@yinwucheng ~]# ll
    total 0
    ----------. 1 root root 0 Aug  7 17:54 file
    [root@yinwucheng ~]# chmod ug=rwx,o=r file   属主读写执行,属组读写,其他人无权限
    [root@yinwucheng ~]# ll
    total 0
    -rwxrwxr--. 1 root root 0 Aug  7 17:54 file
    
    • 方式二: number r=4 w=2 x=1 -=0
      针对文件最高设定为 777  但是一般都是666
      1. 设定属主(读写) 属组(读)权限 其他人(无)  rw-r------
      翻译为数字 640
    [root@oldboyedu opt]# chmod 640 1.txt
    [root@oldboyedu opt]# ll 1.txt
    -rw-r-----. 1 oldboy root 8 Aug  7 10:07 1.txt
    

    2. 设定属主(读写) 属组(无)权限 其他人(无)

    [root@oldboyedu opt]# chmod 600 1.txt
    [root@oldboyedu opt]# ll
    total 4
    -rw-------. 1 oldboy root 8 Aug  7 10:07 1.txt
     针对目录设定权限:  777
    选项: -R递归修改
    [root@yinwucheng ~]# mkdir dir
    [root@yinwucheng ~]# chmod 777 dir/  #修改目录允许所有人访问
    [root@yinwucheng ~]# chmod -R 755 dir/ #修改目录及子目录权限
    [root@yinwucheng ~]# ll -d dir/
    drwxr-xr-x 2 root root 6 Apr 13 03:34 dir/
    

    针对 hr 部门的访问目录/home/hr 设置权限,要求如下:

    1. root 用户和 hr 组的员工可以读、写、执行
    2. 其他用户没有任何权限

    [root@yinwucheng ~]# groupadd hr
    [root@yinwucheng ~]# useradd hr01 -G hr
    [root@yinwucheng ~]# useradd hr02 -G hr
    [root@yinwucheng ~]# mkdir /home/hr
    [root@yinwucheng ~]# chgrp hr /home/hr
    [root@yinwucheng ~]# chmod 770 /home/hr
    [root@yinwucheng ~]# ll -d /home/hr
    drwxrwx--- 2 root hr 6 Apr 13 03:26 /home/hr
    

    权限验证

    针对文件:

    r: 能读取文件内容
    cat head less tail more
    w: 能写入文件 (不能看)
    vim
    x: 能执行文件(如果没有r权限,单有x 没有用)
    rw: 能查看文件,能编辑文件. 不能执行.不能删除,不能移动,不能复
    制 (1)
    rx: 能查看文件,不能编辑,能执行. 不能删除,不能移动,不能复制 (2)
    rwx: 能查看文件,能编辑文件,能执行. 不能删除移动复制 --->使用
    较少

    针对目录的权限:

    r: 具有浏览目录的权限,无法进入目录,使用ls 查看目录下的文件会
    报错,但会显示文件名称, 如果使用 ls-l 只能看到文件名称,其他的
    全部无法查看.
    w: 什么权限也没有
    x: 能进入目录,其他什么也没有
    rx: 能进入目录,能查看目录下的文件,至于操作文件,需要看文件本
    身的权限. (1)
    rw:
    rwx: 如果目录赋予了w权限,则该目录下的文件可以复制删除移
    动修改
    文件使用最多: rw=6 rx=5 644
    目录使用最多: rx = 5 755
    属主和属组变更
    chown 更改属主以及属组 -R:递归修改

    准备环境,创建文件和目录

    • [root@yinwucheng ~]# mkdir dir/test1 && touch dir/file
    示例1: 修改所属主为bin
    [root@yinwucheng ~]# chown bin dir/
    示例2: 修改所属组为adm
    [root@yinwucheng ~]# chown .adm dir/
    示例3: 递归修改目录及目录下的所有文件属主和属组
    [root@yinwucheng ~]# chown -R root.root dir/
    
    • 今日总结

    1.什么是权限? 权限就是一种约束解释
    2.为什么要有权限?
    3.权限和用户之间的关系?
    4.权限如何设定? chmod
    ugo方式 out
    number方式 7 rwx 6 rw 5 rx 4 r 3 wx 2w 1 x
    r =4
    w=2
    x= 1
    755 --->7属主 5 属组 5 其他
    5.测试r w x 每一位的权限?
    6.通常权限的组合使用方式?
    文件: r rw rx
    目录: rx rwx
    7.如何变更一个文件属主和属组. chown -R 递归

  • 相关阅读:
    How to fix the error; 'Error: Bootstrap tooltips require Tether (http://github.hubspot.com/tether/)'
    CentOS 7.2安装教程
    webpack打包生成多个vendor的配置方法
    webpack务虚扫盲
    Webpack引入jquery及其插件的几种方法
    多线程Lock版生产者和消费者模式
    多线程共享全局变量以及锁机制
    多线程Thread类
    多线程threading模块
    Python正则表达式(二)
  • 原文地址:https://www.cnblogs.com/yinwu/p/11486294.html
Copyright © 2011-2022 走看看