理解 sudo 和 sudoers[通俗易懂]

理解 sudo 和 sudoers[通俗易懂]在Linux上,只有root用户可以执行任何命令,其他用户必须使用sudo才可执行特殊的命令.sudo是通过sudoers进行配置的.默认配置/etc/sudoers:##ThisfileMUSTbeeditedwiththe’visudo’commandasroot.##Pleaseconsideraddinglocalcontentin/etc/sudoers.d/insteadof#directlymodifying

大家好,又见面了,我是你们的朋友全栈君。

Linux 上, 只有 root 用户可以执行任何命令, 其他用户必须使用 sudo 才可执行特殊的命令.

sudo 是通过 sudoers 进行配置的.

默认配置

/etc/sudoers:

#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults        env_reset
Defaults        mail_badpass
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d

添加配置

不建议直接修改默认配置文件, 我们可以使用 #include#includedir 添加自定义的配置文件.

/etc/sudoers.d/README:

#
# As of Debian version 1.7.2p1-1, the default /etc/sudoers file created on
# installation of the package now includes the directive:
# 
#       #includedir /etc/sudoers.d
# 
# This will cause sudo to read and parse any files in the /etc/sudoers.d 
# directory that do not end in '~' or contain a '.' character.
# 
# Note that there must be at least one file in the sudoers.d directory (this
# one will do), and all files in this directory should be mode 0440.
# 
# Note also, that because sudoers contents can vary widely, no attempt is 
# made to add this directive to existing sudoers files on upgrade.  Feel free
# to add the above directive to the end of your /etc/sudoers file to enable 
# this functionality for existing installations if you wish!
#
# Finally, please note that using the visudo command is the recommended way
# to update sudoers content, since it protects against many failure modes.
# See the man page for visudo for more information.
#

理解配置

root    ALL=(ALL:ALL) ALL

上面的配置表示: root 用户可以在 任意机器 上以 任意用户任意用户组任意组合 执行 任意命令.

  • root: 用户 (%admin 表示 用户组)
  • ALL=: 所有 host (sudoers 配置可能被用到多台机器上)
  • ALL:: 任意 用户
  • :ALL: 任意 用户组
  • ALL: 任意 命令

常见配置

允许用户使用 sudo 执行任意命令, 但需要输入用户密码

user ALL=(ALL:ALL) ALL

允许用户不需要密码使用 sudo 执行任意命令

user ALL=(ALL:ALL) NOPASSWD: ALL

参考文档

公众号: 源自开发者

原文链接: https://goworker.cn/posts/linux-sudoers/

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/151372.html原文链接:https://javaforall.net

(0)
上一篇 2022年6月20日 下午3:46
下一篇 2022年6月20日 下午4:00


相关推荐

  • Ubuntu18.04LTS wps无法输入汉字

    Ubuntu18.04LTS wps无法输入汉字1 打开终端 sudogedit usr bin wps2 从第二行加上 exportXMODIF im ibux exportQT IM MODULE ibux 3 然后保存退出 回到终端 4 然后在终端依次输入 sudogedit usr bin etsudogedit usr bin wpp 重复步骤 25 关闭终端 打开 wps 即可

    2026年3月18日
    2
  • 白话空间统计番外:再谈莫兰指数(Moran’s I)

    白话空间统计番外:再谈莫兰指数(Moran’s I)经典相关性分析是两条数据(属性维度)之间的相互依赖关系,那么空间自相关就是在空间范围内的相互依赖程度。全局的莫兰指数就是用来衡量空间自相关程度的。在ArcGIS的工具集里面,这个工具干脆就直接叫做“空间自相关”(SpatialAutocorrelation(GlobalMoran’sI))。

    2022年6月25日
    33
  • webview禁止长按复制_chrome复制插件

    webview禁止长按复制_chrome复制插件8.长按事件因为webview长按时将会调用系统的复制控件://长按复制粘贴mWebView.setOnLongClickListener(newView.OnLongClickListener(){@OverridepublicbooleanonLongClick(Viewview){

    2026年4月18日
    7
  • 什么是NP问题,什么是NP hard问题,什么是NP完全问题

    什么是NP问题,什么是NP hard问题,什么是NP完全问题先来看一个小故事:(转自:http://zhm2k.blog.163.com/blog/static/5981506820095233143571/)假如老板要你解决一个问题,你绞尽脑汁还是想不出来,叫天天不应,叫地地不灵,这时你走进老板办公室,可以采取3种策略:1)一副倒霉像,神情猥琐,可怜巴巴的说:老板,我没做出来,我想我是太蠢了……boss:蠢材!滚!(失败……)2)

    2025年6月15日
    5
  • 下列python语句的输出结果是print_下列 Python语句的输出结果是「建议收藏」

    下列python语句的输出结果是print_下列 Python语句的输出结果是「建议收藏」【填空题】遍历输出文件所有行。f=open(“d:\\r2.txt”,”r”)whileTrue:str=print(str,end=”)ifnotstr:breakf.close()【填空题】Pyhon语句序列”s1=’redhat’;print(str.upper(s1))”的运行结果是?【单选题】下列语句中,()在Python中是非法的。【单选题】执…

    2025年6月16日
    2
  • PLSQL连接本地oracle或远程oracle数据库,实现随意切换

    PLSQL连接本地oracle或远程oracle数据库,实现随意切换前言每次安装 Oracle 以后 都会出现使用 plsql 连接不上的问题 多次重启电脑 重装系统的磨人经历之后 终于做出这么一篇文章 希望能帮助广大技术人员减少一些时间 顺利进行连接 注 也可以用 plsql 连接远程数据库 只要有 oracle 的 network admin tnsnames ora 就行 首先下载 64 位 oracle 以及 32 位轻量级客户端 注意版本的对应 我用的是 11g 的 oracl

    2026年3月20日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号