sudo(1)基础认识
sudo
是一个非常重要的命令,全称是 **”superuser do”**,意思是“以超级用户的身份执行”。它允许普通用户在需要时以更高的权限(通常是 root 权限)运行特定的命令或程序,而无需直接登录到 root 账户。这种机制既提高了系统的安全性,也方便了用户管理。
1.基本概念
- root 用户:Linux 中的超级用户,拥有系统所有权限。
- sudo 的作用:让普通用户在经过授权后,临时提升权限执行需要管理员权限的操作。
- 配置文件:
sudo
的权限控制主要通过/etc/sudoers
/etc/sudoers.d/xxx
文件配置,管理员可以在这里指定哪些用户或用户组可以使用sudo
,以及他们可以运行哪些命令。
包
dnf install -y sudo
2.使用方法
基本语法:
sudo [选项] 命令
[root@centos8-51 ~]# sudo --help
sudo - execute a command as another user
usage: sudo -h | -K | -k | -V
usage: sudo -v [-AknS] [-g group] [-h host] [-p prompt] [-u user]
usage: sudo -l [-AknS] [-g group] [-h host] [-p prompt] [-U user] [-u user] [command]
usage: sudo [-AbEHknPS] [-r role] [-t type] [-C num] [-g group] [-h host] [-p prompt] [-T timeout] [-u user] [VAR=value] [-i|-s] [<command>]
usage: sudo -e [-AknS] [-r role] [-t type] [-C num] [-g group] [-h host] [-p prompt] [-T timeout] [-u user] file ...
Options:
-A, --askpass use a helper program for password prompting
-b, --background run command in the background
-B, --bell ring bell when prompting
-C, --close-from=num close all file descriptors >= num
-E, --preserve-env preserve user environment when running command
--preserve-env=list preserve specific environment variables
-e, --edit edit files instead of running a command
-g, --group=group run command as the specified group name or ID
-H, --set-home set HOME variable to target user's home dir
-h, --help display help message and exit
-h, --host=host run command on host (if supported by plugin)
-i, --login run login shell as the target user; a command may also be specified ### 登录root
-K, --remove-timestamp remove timestamp file completely
-k, --reset-timestamp invalidate timestamp file
-l, --list list user's privileges or check a specific command; use twice for longer format ### sudo对应账户可以执行权限
-n, --non-interactive non-interactive mode, no prompts are used
-P, --preserve-groups preserve group vector instead of setting to target's
-p, --prompt=prompt use the specified password prompt
-r, --role=role create SELinux security context with specified role
-S, --stdin read password from standard input
-s, --shell run shell as the target user; a command may also be specified
-t, --type=type create SELinux security context with specified type
-T, --command-timeout=timeout terminate command after the specified time limit
-U, --other-user=user in list mode, display privileges for user
-u, --user=user run command (or edit file) as specified user name or ID ### suod后运行账户,空默认为root
-V, --version display version information and exit
-v, --validate update user's timestamp without running a command
-- stop processing command line arguments
visudo 配置编辑
[root@centos8-51 ~]# visudo --help
visudo - safely edit the sudoers file
usage: visudo [-chqsV] [[-f] sudoers ]
Options:
-c, --check check-only mode ### 检查sudoers配置语法
-f, --file=sudoers specify sudoers file location ### 指定配置修改 visudo /etc/sudoers.d/test
-h, --help display help message and exit
-q, --quiet less verbose (quiet) syntax error messages
-s, --strict strict syntax checking ### 严格语法检查
-V, --version display version information and exit
3.示例
以 root 权限更新软件包列表:
sudo apt update
执行此命令时,系统会提示输入当前用户的密码,验证通过后以 root 权限运行apt update
。安装软件:
sudo apt install vim查看系统日志(需要权限):
sudo cat /var/log/syslog
常用选项
-u <用户>
:以指定用户的身份运行命令,默认是 root。
sudo -u testuser ls /home/testuser-l
:列出当前用户通过sudo
可以执行的命令。
sudo -l-i
:进入一个新的 shell 会话,以 root 身份操作。
sudo -i
工作原理
当你输入
sudo
命令时,系统会:- 检查
/etc/sudoers
/etc/sudoers.d/xxx
文件,确认当前用户是否有权限执行该命令。 - 提示输入用户密码(默认是调用者的密码,不是 root 密码)。
- 验证通过后,临时以 root 或指定用户的身份执行命令。
- 检查
缓存凭证:输入密码后,
sudo
会在一段时间内(默认 5 分钟,可在配置中修改)缓存你的凭证,这段时间内再次使用sudo
无需再次输入密码。
配置 sudoers 文件
编辑 /etc/sudoers
文件时,建议使用 visudo
命令,避免语法错误导致系统问题。
示例配置
- 允许用户
john
无需密码运行所有命令:
john ALL=(ALL) NOPASSWD:ALL - 允许用户
mary
只能运行ls
和cat
:
mary ALL=(ALL) /bin/ls, /bin/cat
注意事项
- 安全性:谨慎配置
sudo
权限,避免给用户过多不必要的权限。 - 密码输入:如果提示“No sudo privileges”,说明当前用户未被授权使用
sudo
,需要管理员在sudoers
文件中添加权限。 - 与 su 的区别:
su
:切换到 root 用户,需要知道 root 密码。sudo
:临时提升权限,不需要 root 密码,只需用户自己的密码(如果被授权)。