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


相关推荐

  • 快速排序quicksort_快速排序的原理

    快速排序quicksort_快速排序的原理一、简介快速排序是(Quicksort)是对冒泡排序的一种改进,是非常重要且应用比较广泛的一种高效率排序算法。二、算法思路快速排序是通过多次比较和交换来实现排序,在一趟排序中把将要排序的数据分成两个独立的部分,对这两部分进行排序使得其中一部分所有数据比另一部分都要小,然后继续递归排序这两部分,最终实现所有数据有序。大致步骤如下:首先设置一个分界值也就是基准值又是也称为监视哨,通过该分界值将数据分割成两部分。将大于或等于分界值的数据集中到右边,小于分界值的数据集中到左边。一趟排序过后,左边部

    2022年8月12日
    5
  • JWT——Token认证的两种实现和安全详解

    JWT——Token认证的两种实现和安全详解前言 最近因为项目中需要解决跨域取值的问题 所有考虑到用 Token 认证做技术支撑点 自己看了许多与之相关的文章 从中总结出了以下两个要点 签名和 token 时间 在说这两个要点之前先大概简单说一下与之有关的一些问题 首先 如果你对 token 认证的知识一点都不了解 那么我觉得这篇文章还不太适合你 因为我在这里不会在把相关的基础知识再说明一遍 因为网上有很多相关的文章 讲的都比较好 我会在文章

    2025年9月19日
    2
  • C++中this指针的理解和用法

    C++中this指针的理解和用法关于this指针的一个精典回答:当你进入一个房子后,你可以看见桌子、椅子、地板等,但是房子你是看不到全貌了。对于一个类的实例来说,你可以看到它的成员函数、成员变量,但是实例本身呢?this是一个指针,它时时刻刻指向你这个实例本身。 个人理解:(ps:class类就好比这座房子,this就好比一把钥匙,通过钥匙来打开了这座房子的门,那么里面的东西就随意你取用了)

    2022年5月16日
    33
  • 免费mac激活码2021【2021免费激活】

    (免费mac激活码2021)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~M…

    2022年3月20日
    54
  • 《剑指offer》– 构建乘积数组、求1+2+3+…+n、不用加减乘除做加法、包含min函数的栈、用两个栈实现队列

    《剑指offer》– 构建乘积数组、求1+2+3+…+n、不用加减乘除做加法、包含min函数的栈、用两个栈实现队列

    2021年10月3日
    45
  • 网站的引导页介绍

    外贸企业站就是要走出过门,买域名,买空间的、请设计公司、请这请那,一切的努力都是要为了能通过网站给公司带来订单,“一分耕耘一分收获”,但是耕耘了未必就有收获,很多企业站点建好了,可是并没有发现,它所给

    2021年12月25日
    39

发表回复

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

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