美文网首页
Linux中sudoers文件详解

Linux中sudoers文件详解

作者: 刘小白DOER | 来源:发表于2021-03-12 12:44 被阅读0次

        笔者在添加新用户后或者在centos系统上使用sudo时会提示没有权限,需要编辑修改/etc/sudoers,于是笔者将WSL Ubuntu、树莓派4B,centos7系统的这个文件罗列出来看看到底怎么配置。

        sudo的作用是啥呢?Sudoers allows particular users to run various commands as the root user, without needing the root password。

    1、WSL Ubuntu

         文件的第一句话:This file MUST be edited with the 'visudo' command as root. 这个文件需要root用户使用visudo来编辑修改,这个编辑器库检查语法,避免修改错误。

        /etc/sudoers.d/目录下的文件也同样有效,提示可以在目录下新建个配置文件来避免直接修改sudoers文件。Please consider adding local content in /etc/sudoers.d/ instead of directly modifying this file.

        文件里面最重要的是三个配置:

    超级用户拥有所有权限

    # User privilege specification  

    root    ALL=(ALL:ALL) ALL

     admin用户组拥有所有权限

    # Members of the admin group may gain root privileges  

    %admin ALL=(ALL) ALL

     允许使用sudo来获得所有权限

    # Allow members of group sudo to execute any command    

    %sudo  ALL=(ALL:ALL) ALL

    这条配置的含义是:

    第一个,指定的是用户,也可以是别名;

    第二个,ALL指定的是用户所在的主机,可以是ip,也可以是主机名;

    第三个,括号里指定的也是用户,指定以什么用户身份执行sudo

    第四个,ALL指定的是执行的命令;

    文件最后包含sudoers.d目录。

    #includedir /etc/sudoers.d

    2、树莓派4B

        相比于WSL Ubuntu,树莓派resdebian就只有其中两条主要的配置。

    # User privilege specification

    root    ALL=(ALL:ALL) ALL

    # Allow members of group sudo to execute any command

    %sudo  ALL=(ALL:ALL) ALL

    3、CentOS7

        相比于上面,CentOS7的sudoers文件就丰富的多,可以定义具体的权限。但也有些区别。

        root ALL=(ALL) ALL  。括号中只有一个ALL。

    wheel用户在运行所有命令,可以不输入密码。

    %wheel ALL=(ALL) NOPASSWD: ALL

    允许使用用户挂载U盘

    %users ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom

    允许使用用户关机

    %users localhost=/sbin/shutdown -h now

        配置文件中列出很多别名NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS。比如PROCESSES = /bin/nice, /bin/kill, /usr/bin/kill, /usr/bin/killall。

        那么下面的配置就是允许sys用户组可以执行别名定义的应用。

        %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS

    相关文章

      网友评论

          本文标题:Linux中sudoers文件详解

          本文链接:https://www.haomeiwen.com/subject/szfzqltx.html