理解 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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • ajax的跨域请求_js解决跨域问题

    ajax的跨域请求_js解决跨域问题什么是AJAX?AJAX是无需刷新页面就能够从服务器去的数据的一种方法,负责Ajax运作的核心对象是XMLHttpRequest(XHR)对象。同源策略是对XHR的一个主要约束,它为通信设置了“相同的域、相同的端口、相同的协议”这一限制。试图访问上述限制之外的资源都会引发安全错误,除非采用被认可的跨域解决方案。这个方案叫做CORS(Cross-OriginResourceSharing)跨源…

    2022年8月24日
  • 初识AngularJS

    初识AngularJS初识AngularJS一、AngularJS是什么?AngularJS,简称:ng它是一款非常优秀的前端高级JS框架由MiskoHevery等人创建2009年被Google公司收购,用于其多款产品有一个全职的开发团队继续开发和维护这个库这个框架可以轻松构建SPA应用程序通过指令扩展HTML,通过表达式将数据绑定到HTML(基本不需要DOM操作)二、SPA应用程序简单介绍单页面应用

    2022年7月25日
  • 有理数类设计

    有理数类设计

    2021年10月6日
  • PhpSpreadsheet_php file_put_contents

    PhpSpreadsheet_php file_put_contentsphp用PhpSpreadsheet对Excel进行读取、写入、修改十分便捷,下面将我在工作中用到过的操作进行总结。1、读取Excel:见文章PhpSpreadsheet读取单元格内容的坑2、修改Excel:示例代码(只保留了基础代码):$this->view->title=’填写Excel’;$abs_excel_path=$_SERVER[‘DOCUMENT_ROO…

    2022年9月16日
  • 数据库入门基础[通俗易懂]

    数据库入门基础[通俗易懂]一、什么是数据库很多人不明白,存储数据用文件就可以了,为什么还要弄个数据库?首先文件保存数据有以下几个缺点:·文件的安全性问题·文件不利于数据查询和管理·文件不利于存储海量数据·文件在程序中控制不方便为了解决上述问题,专家们设计出更加利于管理数据的软件——数据库,它能更有效的管理数据。数据库可以提供远程服务,即通过远程连接来使用数据库,因此也称为数据库服务器。数据库存储介质…

    2022年6月20日
  • invoke方法的使用

    invoke方法的使用

    2022年6月12日

发表回复

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

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