zoukankan      html  css  js  c++  java
  • 权限管理

    linux系统中的三类人

        属主
        属组
        其他人

      如果root用户创建了test文件,默认属主为root,属组也是root
      其他人kuying,kuying用户既不是root用户,也不在root里面

    linux中三类权限
        r read 读权限
        w write 写权限
        x execute 执行权限

      读写执行权限:
         对于目录来说
           读权限:表示用户可以查看目录有哪些文件, ls
           写权限:表示用户可以创建,删除,移动,复制文件,touch,mkdir,mv..
           执行权限:表示用户可以在目录间切换。 cd

        对于普通文件来说:
           读权限:表示用户可以查看文件内容
           写权限:表示用户可以编辑文本
           执行权限:表示用户可以执行该文件如果是个脚本的话就可以执行

      查看文件权限:
        [root@kuying ~]# ls -l test
        -rw-r–r-x1 root root 4 Jul 12 22:58 test

        rw- 表示属主的权限。表示属主对test文件拥有读写权限,无执行权限
        r– 表示属组的权限。表示属组里面的用户对test文件拥有读权限,无写和执行权限
        r-x 表示其他人的权限。表示其他人对test文件拥有读和执行权限,无写权限。

    权限设置
      用户表示:

        u 表示属主
        g 表示属组
        o 表示其他人
        a 表示所有人

        + 增加权限
        – 删除权限
        = 授予权限 #注重结果

        r 读权限
        w 写权限
        x 执行权限

    chmod 命令
        作用:修改文件的权限
      选项:
        -R 同时修改目录及目录下的所有文件

      例子1:给属主增加执行权限
        [root@kuying ~]# chmod u+x test
        [root@kuying ~]# ll
        total 8
        -rw——-. 1 root root 1258 Jul 12 22:04 anaconda-ks.cfg
        -rwxr–r– 1 root root 4 Jul 12 22:58 test

      例子2:去掉属主的执行权限
        [root@kuying ~]# chmod u-x test
        [root@kuying ~]# ll
        total 8
        -rw——-. 1 root root 1258 Jul 12 22:04 anaconda-ks.cfg
        -rw-r–r– 1 root root 4 Jul 12 22:58 test

      例子3:授予属主读写执行权限
        方法一:
          [root@kuying ~]# chmod u+x test
        方法二:

          [root@kuying ~]# chmod u=rwx test
          [root@kuying ~]# ll
          total 8
          -rw——-. 1 root root 1258 Jul 12 22:04 anaconda-ks.cfg
          -rwxr–r– 1 root root 4 Jul 12 22:58 test

      例子4:属主仅用于读权限
        方法一:
          [root@kuying ~]# chmod u-wx test
          [root@kuying ~]# ll
          total 8
          -rw——-. 1 root root 1258 Jul 12 22:04 anaconda-ks.cfg
          -r–r–r– 1 root root 4 Jul 12 22:58 test
        方法二:
          [root@kuying ~]# chmod u=r test
          [root@kuying ~]# ll
          total 8
          -rw——-. 1 root root 1258 Jul 12 22:04 anaconda-ks.cfg
          -r–r–r– 1 root root 4 Jul 12 22:58 test
      例子5:给属组添加执行权限
          [root@kuying ~]# chmod g+x test
          [root@kuying ~]# ll
          total 8
          -rw——-. 1 root root 1258 Jul 12 22:04 anaconda-ks.cfg
          -r–r-xr– 1 root root 4 Jul 12 22:58 test
      例子6:给属组删除执行权限
          [root@kuying ~]# chmod g-x test
          [root@kuying ~]# ll
          total 8
          -rw——-. 1 root root 1258 Jul 12 22:04 anaconda-ks.cfg
          -r–r–r– 1 root root 4 Jul 12 22:58 test
      例子7:授予属组中用户读写执行权限
          [root@kuying ~]# chmod g=rwx test
          [root@kuying ~]# ll
          total 8
          -rw——-. 1 root root 1258 Jul 12 22:04 anaconda-ks.cfg
          -r–rwxr– 1 root root 4 Jul 12 22:58 test
      例子8:给其他人添加执行权限
          [root@kuying ~]# chmod o+x test
          [root@kuying ~]# ll
          total 8
          -rw——-. 1 root root 1258 Jul 12 22:04 anaconda-ks.cfg
          -r–rwxr-x 1 root root 4 Jul 12 22:58 test
      例子9:给其他人删除执行权限
          [root@kuying ~]# chmod o-x test
          [root@kuying ~]# ll
          total 8
          -rw——-. 1 root root 1258 Jul 12 22:04 anaconda-ks.cfg
          -r–rwxr– 1 root root 4 Jul 12 22:58 test
      例子10:给其他人添加读写执行权限
        [root@kuying ~]# chmod o=rwx test
        [root@kuying ~]# ll
        total 8
        -rw——-. 1 root root 1258 Jul 12 22:04 anaconda-ks.cfg
        -r–rwxrwx 1 root root 4 Jul 12 22:58 test

    思考:让kuying用户拥有root用户在/tmp目录下创建对的test文件的写权限?
        -rw-r–r– 1 root root 0 Jul 12 23:16 test

      方法一:
        [root@kuying tmp]# chmod o+w test
      方法二:
        [root@kuying tmp]# usermod -G root kuying
      方法三:
        [root@kuying tmp]# chown kuying test
      方法四:
        [root@kuying tmp]# chown :kuying test

    用数字表示权限:
      r  4
      w  2
      x  1

      例:
        rwxr–r– 744
        755 rwxr-xr-x
        700 rwx——
        655 rw-r-xr-x
        644 rw-r–r–
        600 rw——-
        000 ———

      例子1:属主拥有读写权限,属组拥有读权限,其他人拥有读写执行权限
        chmod 647 test

      例子2:同时给所有用户添加执行权限
        [root@kuying tmp]# chmod +x test
        [root@kuying tmp]# ll
        total 8
        -rwx——. 1 root root 836 Jul 12 22:04 ks-script-cY6gg9
        -rwxrwxr-x 1 root root 7 Jul 12 23:22 test

      例子3:同时给所有用户删除执行权限
        [root@kuying tmp]# chmod -x test
        [root@kuying tmp]# ll
        total 8
        -rwx——. 1 root root 836 Jul 12 22:04 ks-script-cY6gg9
        ———- 1 root root 7 Jul 12 23:22 test

        [root@kuying tmp]# chmod +w test
        [root@kuying tmp]# ll
        total 8
        -rwx——. 1 root root 836 Jul 12 22:04 ks-script-cY6gg9
        –w——- 1 root root 7 Jul 12 23:22 test

      例子4:同时给所有用户添加读权限  
        [root@kuying tmp]# chmod +r test
        [root@kuying tmp]# ll
        total 8
        -rwx——. 1 root root 836 Jul 12 22:04 ks-script-cY6gg9
        -rwxrwxr-x 1 root root 7 Jul 12 23:22 test

      例子5:同时给所有用户删除读权限
        [root@kuying tmp]# chmod -r test
        [root@kuying tmp]# ll
        total 8
        -rwx——. 1 root root 836 Jul 12 22:04 ks-script-cY6gg9
        —x–x–x 1 root root 7 Jul 12 23:22 test

      例子6:同时给所有用户添加写权限
        [root@kuying tmp]# chmod +w test #如果前面不加a的话默认值添加属主的写权限
        [root@kuying tmp]# ll
        total 8
        -rwx——. 1 root root 836 Jul 12 22:04 ks-script-cY6gg9
        –w——- 1 root root 7 Jul 12 23:22 test

        [root@kuying tmp]# chmod a+w test
        [root@kuying tmp]# ll
        total 8
        -rwx——. 1 root root 836 Jul 12 22:04 ks-script-cY6gg9
        –w–w–w- 1 root root 7 Jul 12 23:22 test

      例子7:同时删除所有人的写权限
        [root@kuying tmp]# chmod a-w test
        [root@kuying tmp]# ll
        total 8
        -rwx——. 1 root root 836 Jul 12 22:04 ks-script-cY6gg9
        ———- 1 root root 7 Jul 12 23:22 test

    #设置权限注意事项:
      要么只读
      如果有写权限,那么久需要有读权限

    -R 选项
      在更改目录权限时使用

      例子1:
        [root@kuying tmp]# ls -l kuying
        total 0
        -rw-r–r– 1 root root 0 Jul 12 23:35 test1
        -rw-r–r– 1 root root 0 Jul 12 23:35 test2
        -rw-r–r– 1 root root 0 Jul 12 23:35 test3
        -rw-r–r– 1 root root 0 Jul 12 23:35 test4
        [root@kuying tmp]# chmod +x kuying   #不加-R选项仅仅只会更改kuying目录本身的权限
        [root@kuying tmp]# ls -ld kuying
        drwxr-xr-x 2 root root 58 Jul 12 23:35 kuying
        [root@kuying tmp]# ls -l kuying
        total 0
        -rw-r–r– 1 root root 0 Jul 12 23:35 test1
        -rw-r–r– 1 root root 0 Jul 12 23:35 test2
        -rw-r–r– 1 root root 0 Jul 12 23:35 test3
        -rw-r–r– 1 root root 0 Jul 12 23:35 test4
        [root@kuying tmp]# chmod -R +x kuying #加-R选项会把kuying目录以及目录下的所有文件权限都会更改
        [root@kuying tmp]# ls -l kuying
        total 0
        -rwxr-xr-x 1 root root 0 Jul 12 23:35 test1
        -rwxr-xr-x 1 root root 0 Jul 12 23:35 test2
        -rwxr-xr-x 1 root root 0 Jul 12 23:35 test3
        -rwxr-xr-x 1 root root 0 Jul 12 23:35 test4

    chown命令
        作用:更改文件的属主

      选项:

        -R 同事更改目录以及目录下的所有文件的属主和属组

      例子1:改变test文件的属主为kuying
        [root@kuying tmp]# chown kuying test

      例子2:改变test文件的属组为kuying
        [root@kuying tmp]# chown :kuying test
        [root@kuying tmp]# ll
        total 8
        drwxr-xr-x 2 root root 58 Jul 12 23:35 kuying
        -rwx——. 1 root root 836 Jul 12 22:04 ks-script-cY6gg9
        -rwx-w–w- 1 kuying kuying 36 Jul 12 23:55 test

      例子3:同时更改为test文件的属主和属组权限为root
        方法一:
          [root@kuying tmp]# chown root:root test

        方法二:
          [root@kuying tmp]# chown kuying.kuying test

        方法三:
          [root@kuying tmp]# chown root: test

        方法四:
          [root@kuying tmp]# chown kuying. test

    chown -R

      例子1:
        [root@kuying tmp]# chown kuying. kuying #不加-R仅仅更改目录本身的属主和属组

        [root@kuying tmp]# ll
        total 8
        drwxr-xr-x 2 kuying kuying 58 Jul 12 23:35 kuying
        -rwx——. 1 root root 836 Jul 12 22:04 ks-script-cY6gg9
        -rwx-w–w- 1 kuying kuying 36 Jul 12 23:55 test
        drwx——. 2 root root 6 Jul 12 22:11 vmware-root
        -rw——-. 1 root root 0 Jul 12 21:57 yum.log
        [root@kuying tmp]# ls -l kuying
        total 0
        -rwxr-xr-x 1 root root 0 Jul 12 23:35 test1
        -rwxr-xr-x 1 root root 0 Jul 12 23:35 test2
        -rwxr-xr-x 1 root root 0 Jul 12 23:35 test3
        -rwxr-xr-x 1 root root 0 Jul 12 23:35 test4
        [root@kuying tmp]# chown -R kuying. kuying #加上-R同时更改目录以及目录下的所有文件的属主和属组
        [root@kuying tmp]# ls -l kuying
        total 0
        -rwxr-xr-x 1 kuying kuying 0 Jul 12 23:35 test1
        -rwxr-xr-x 1 kuying kuying 0 Jul 12 23:35 test2
        -rwxr-xr-x 1 kuying kuying 0 Jul 12 23:35 test3
        -rwxr-xr-x 1 kuying kuying 0 Jul 12 23:35 test4

    总结chown用法:
        chown kuying: test
        chown kuying. test
        chown kuying:kuying test
        chown kuying.kuying test
        chown kuying test
        chown :kuying test
        chown .kuying test

    usermod命令
        作用:修改用户信息

      选项:
        -u 更改uid
        -g 更改基本组
        -G 更改附加组
        -s 更改shell类型
        -l 更改登录用户名

      例子1:更改kuying用户的uid为1008
        [root@kuying tmp]# usermod -u 1008 kuying

      例子2:更改kuying用户的主组为root
        [root@kuying tmp]# usermod -g root kuying

      例子3:test用户添加root为附加组
        [root@kuying tmp]# usermod -G root kuying

      例子4:更改kuying用户登录名为kuying1
        [root@kuying tmp]# usermod -l kuying1 kuying #新名在前 旧名在后

    /etc/shadow 用户密码管理

      -bash-4.2$
      如果登录用户的时候发现出现上面的错误是因为缺少用户家目录模板下面的三个隐藏文件
      处理办法:
        cp /etc/skel/.bash* /home/kuying

    文件的特殊权限
        SUID
        SGID
        SBIT
        ACL

      SUID(set uid设置用户ID):限定:只能设置在二进制可执行程序上面。对目录设置无效

          功能:程序运行时的权限从执行者变更成程序所有者的权限
          注意:设定suid的命令其他人也需要有执行权限

        uid权限设置:
          chmod u+s cmd

      例子1:给cat命令添加SUID权限

        [root@kuying ~]# chmod u+s /usr/bin/cat
        [root@kuying ~]# ls -ld /usr/bin/cat
        -rwsr-xr-x. 1 root root 54080 Apr 11 2018 /usr/bin/cat

      验证:
        再次查看/etc/shadow文件发现可以看了

      SGID
        SGID:限定:既可以给二进制可执行程序设置,也可以对目录设置
        功能:在设置了SGID权限的目录下建立文件时,新创建的文件的所属组会,继承上级目录的所属组

      例子1:给目录kuying添加SGID权限
        [root@kuying tmp]# chmod g+s kuying
        [root@kuying tmp]# ll
        total 4
        drwxr-sr-x 2 root root 6 Jul 13 00:34 kuying

        [kuying@kuying kuying]$ touch test
        [kuying@kuying kuying]$ ll
        total 0
        -rw-rw-r– 1 kuying root 0 Jul 13 00:35 test

      SBIT
        对于设置sbit权限的文件,用户只能删除自己创建的文件,无法删除其他用户的文件

    例子1:设置SBIT权限
        [root@kuying kuying]$ chmod o+t /tmp

        [kuying@kuying kuying]$ ls -ld /tmp
        drwxrwxrwt. 9 root root 174 Jul 13 00:34 /tmp
        /tmp目录本身默认已经添加了SBIT权限!

      验证:
        [kuying1@kuying tmp]$ rm -rf kuyingtest
        rm: cannot remove ‘kuyingtest’: Operation not permitted
        #验证无法删除!

      如何删除呢?
        [root@kuying tmp]# chmod o-t /tmp
        [root@kuying tmp]# su – kuying1
        Last login: Sat Jul 13 00:39:52 CST 2019 on pts/0

        [kuying1@kuying ~]$ cd /tmp
        [kuying1@kuying tmp]$ rm -rf kuyingtest

        [kuying1@kuying tmp]$ ls
        kuying ks-script-cY6gg9 test vmware-root yum.log

      ACL
        对特定的用户实现了权限管控

      例子1:设置kuying用户对文件test拥有读写权限
        [root@kuying tmp]# setfacl -m u:kuying:rw test

      验证:
        [root@kuying ~]# su – kuying1
        Last login: Sat Jul 13 00:41:13 CST 2019 on pts/0

        [kuying1@kuying ~]$ cd /tmp
        [kuying1@kuying tmp]$ cat test

        [kuying1@kuying tmp]$ echo “2” > test #kuying1用户无法进行写
        -bash: test: Permission denied
        [kuying1@kuying tmp]$ exit
        logout

        [root@kuying ~]# su – kuying
        Last login: Sat Jul 13 00:18:42 CST 2019 on pts/1
        [kuying@kuying ~]$ cd /tmp
        [kuying@kuying tmp]$ cat test
        [kuying@kuying tmp]$ echo “22” > test #kuying用户可以进行读写
        [kuying@kuying tmp]$ cat test
        22

      怎么查看acl权限:
        [root@kuying tmp]# getfacl test
        # file: test
        # owner: root
        # group: root
        user::rw-
        user:kuying:rw-
        group::r–
        mask::rw-
        other::r–

      怎么清除acl权限:
        [root@kuying tmp]# setfacl -b test
        [root@kuying tmp]# getfacl test
        # file: test
        # owner: root
        # group: root
        user::rw-
        group::r–
        other::r–

    sudo
        Sudo可以让管理员(root)事先定义某些特殊命令谁可以执行。

        Sudo配置文件:/etc/sudoers
      #该文件默认只读,不允许修改,因此不能直接修改。

      a. 配置sudo文件请使用“#visudo”,打开之后其使用方法和vim一致

      例子1:让kuying用户拥有创建用户的功能
        [root@kuying tmp]# visudo

        kuying ALL=(ALL) /usr/sbin/useradd
        #kuying是用户名

        ALL 可以从任意主机进行登录

      (ALL) 以root的身份进行命令的运行
      /usr/sbin/useradd表示kuying用户可以执行创建用户以root的身份

      [kuying@kuying ~]$ useradd kuying7
      -bash: /usr/sbin/useradd: Permission denied
      [kuying@kuying ~]$ sudo useradd kuying7 #配置visudo后需要使用sudo去执行相应的命令

      We trust you have received the usual lecture from the local System
      Administrator. It usually boils down to these three things:

      #1) Respect the privacy of others.
      #2) Think before you type.
      #3) With great power comes great responsibility.

      [sudo] password for kuying: #在这里输入的时kuying的密码不是root!
      [kuying@kuying ~]$ id kuying7
      uid=1011(kuying7) gid=1011(kuying7) groups=1011(kuying7)
      [kuying@kuying ~]$ sudo useradd kuying8 #5分钟之内不需要输入密码!
      [kuying@kuying ~]$
      [kuying@kuying ~]$
      [kuying@kuying ~]$ id kuying8
      uid=1012(kuying8) gid=1012(kuying8) groups=1012(kuying8)
      kuying1 ALL=(ALL) NOPASSWD: /usr/sbin/useradd

  • 相关阅读:
    Flume配置
    HDU5312 Sequence
    CF(D. Fibonacci Sums)dp计数
    【算法拾遗】二分查找递归非递归实现
    ubuntu下怎么给普通用户赋予sudo权限
    在Oracle 11.2.0.1.0下dbms_stats.gather_table_stats收集直方图不准
    ZOJ3622 Magic Number(水题)
    小试ImageMagik——开发篇
    一起talk C栗子吧(第二十七回:C语言实例--插入排序)
    依据输入的内容打印出菱形
  • 原文地址:https://www.cnblogs.com/biht/p/11216755.html
Copyright © 2011-2022 走看看