Posted in

Linux: 理解 sudo 来管理你的服务器_AI阅读总结 — 包阅AI

包阅导读总结

1. 关键词:Linux、sudo、超级用户、权限、配置

2. 总结:本文介绍了 Linux 中的 sudo 命令,解释了其作用是赋予用户临时提升的权限以执行管理任务,还说明了 sudo 的发展历程、添加用户到相关组的方法、sudoers 文件的配置及风险,强调了 sudo 对 Linux 管理和使用的重要性。

3. 主要内容:

– sudo 简介:

– 代表“super user do”,相当于 Windows 的“run as”命令。

– 发展历程:

– 由 Robert Coggeshall 和 Cliff Spenser 编写原始子系统,后经修改和维护。

– 作用:

– 给特定用户组的用户临时提升权限以运行管理任务。

– 输入命令后需输入用户密码验证。

– 成功运行命令后有 5 分钟的宽限期。

– 添加用户到组:

– 不同发行版使用不同组,如 Ubuntu 用 sudo 组,Fedora 用 wheel 组。

– 使用相应命令添加用户,用户需重新登录生效。

– sudoers 文件:

– 用于 sudo 配置,使用内置编辑器 visudo 以防错误。

– 可创建别名以定制规则,如为特定用户组赋予特定命令权限。

思维导图:

文章地址:https://thenewstack.io/linux-understand-sudo-to-rule-your-server/

文章来源:thenewstack.io

作者:Jack Wallen

发布时间:2024/7/12 14:19

语言:英文

总字数:1107字

预计阅读时间:5分钟

评分:86分

标签:Linux,sudo,服务器管理,安全,用户权限


以下为原文内容

本内容来源于用户推荐转载,旨在分享知识与观点,如有侵权请联系删除 联系邮箱 media@ilingban.com

You’ve probably seen the XKCD cartoon wherein one person asks another to make them a sandwich. When the second person refuses, the first responds with, “sudo make me a sandwich.”

Of course, any Linux user would smirk at the idea, knowing the second person would then have no choice but to make the sandwich. Why? Because… sudo!

Sudo stands for super user do and is the equivalent of the Windows run as command.

Let me give you a bit of background.

When I first started using Linux (around 1997), in order to install software, compile a kernel, or do anything that required administrative rights, I had to first change to the root (or super) user with the command su (or su –, if you needed to include root’s environment). Back then, we thought nothing of it. In fact, one could log into the Linux computer as the root user or even secure shell into a machine as the root user.

It was a real cats and dogs living together situation and we had no idea the dangers it posed.

You see, with the root user available, anyone could access those powers (if they knew — or could crack — the root user’s password). And allowing root SSH access was a disaster waiting to happen.

Imagine this scenario: You log into your Linux machine as the root user and do some random task. You’re then called away from that computer and forget to lock the desktop. Anyone could then sidle on up to the machine and have their way… with full admin powers.

You do not want to land in such a situation. In fact, as a rule of thumb, you should never log in as the root user and avoid changing to root at all costs. That’s why you need the help of sudo.

Fortunately, well before I started using Linux, Robert Coggeshall and Cliff Spenser wrote the original subsystem for sudo. The initial release was sometime around 1980. Between 1986 and 1993, sudo was substantially modified by the IT staff at the University of Colorado Bolder. Around 1994, sudo fell under the watch and care of Todd C. Miller (an OpenBSD developer) who has been the caretaker ever since.

What Does sudo Do?

Essentially, sudo gives users (who belong to the necessary group) temporarily elevated permission, so they can run admin tasks (such as installing software or modifying configuration files with the likes of /etc.

To use this tool, users type sudo followed by the command they want to run. They are then prompted for their user password and, upon successful authentication, the command will run.

By using sudo, the root user is not used, touched, or affected. Once a user successfully runs a command with sudo, they have a grace period of 5 minutes until they’ll be prompted to type their user password for another sudo command.

In other words, you could run a command like sudo apt-get upgrade -y and, once it completes, turn around and run a command like sudo systemctl restart sshd, without having to retype your user password. If the next command is run outside of that five-minute window, you’ll have to retype your user password.

Adding Users to the Required Group

When you add a user to Linux, some distributions will automatically add them to sudo. Most, however, do not. How you add a user to the necessary group depends on the distribution you use. For example, Ubuntu-based distributions use the sudo group, whereas Fedora-based distributions use the wheel group.

So, to add a user to the sudo group, the command would be something like this:

sudo usermod aG sudo USER

Where USER is the username to be added.

To add a user to the wheel group, the command would be something like this:

sudo usermod aG wheel USER

Where USER is the username.

The user must then log out and log back in for the changes to take effect. Once they’ve logged back in, they have the full power of sudo at their disposal.

The sudoers File

This is where sudo gets a bit tricky. The sudoers file is where sudo is configured. This file is so important that it has its own built-in editor (in other words, never edit /etc/sudoers with a normal text editor), called visudo. The visudo editor validates the syntax of the sudoers file when you go to save. This way you are less likely to introduce errors in the configuration, which could be disastrous.

To open the sudoers file, issue the command:

You’ll be prompted for your user password and, upon successful authentication, you’ll be presented with the sudoers file in your default text editor (such as nano). The layout of a sudoers file line looks like this:

Each ALL has a specific meaning:

  • The first ALL indicates that the rule applies to all hosts.
  • The second ALL indicates that the root users can run commands as all users.
  • The third ALL indicates the root user can run commands as all groups.
  • The fourth ALL indicates the rules apply to all commands.

I told you it was tricky.

You can create aliases in the sudoers file, to make creating custom rules even easier. Let’s say you want to create a rule that applies to users olivia, camille, anton, and clara. Let’s say you want to give those users access to the apt-get update and apt-get upgrade commands and only those commands. For that, you could create an alias within the sudoers file like this:

User_Alias GROUPONE = olivia, camille, anton, clara

You would then give the new group access to the commands like this:

GROUPONE ALL = /usr/bin/aptget update

GROUPONE ALL = /usr/bin/aptget upgrade

After saving and closing the file, the users in GROUPONE will then be able to run the two commands.

Your best bet, however, is to use the sudo and wheel groups, so you don’t risk wreaking havoc on your machine via the sudoers file. However, if you need to get granular with admin permissions, sudoers is the way to go.

The sudo command is a must for proper Linux administration and usage. To learn more about sudo, read the man page with the command man sudo.

YOUTUBE.COM/THENEWSTACK

Tech moves fast, don’t miss an episode. Subscribe to our YouTubechannel to stream all our podcasts, interviews, demos, and more.

GroupCreated with Sketch.