Linux中sudoers文件详解

Linux中sudoers文件详解,第1张

    笔者在添加新用户后或者在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用户在运行所有命令,可以不输入密码。

%wheelALL=(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

修改 /etc/sudoers 文件,添加我们普通用户名,即可获得我们的root用户权限

切换到超级管理员

执行命令vim /etc/sudoers 或者 vi /etc/sudoers 进入编辑状态,如图找到

rootALL=(ALL) ALL 这一行

添加以下命令

用户名ALL=(ALL) ALL#添加这条命令,为普通用户提升权限

如 :tom ALL=(ALL) ALL

修改后保存不成功 使用命令 :wq! 进行强制保存


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11397148.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-15
下一篇 2023-05-15

发表评论

登录后才能评论

评论列表(0条)

保存