su 命令用来切换用户
su - USERNAME:使用 login shell 方式切换用户sudo 命令以其他用户身份执行命令。但可以通过 visudo 临时赋予普通用户一些管理员用户的权限,如关机。
使用 visudo 命令后,打开 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
用户 hope 可以以任何用户或组的身份运行所有命令,登录到此配置适用的任何主机:
hope ALL=(ALL:ALL) ALL
首先在 root 用户下,设置 30 分钟后关机,如果需要取消则需要使用 shutdown -c 命令,而普通用户无法执行。

然后,在 user3 用户下,使用 visudo 修改 sudoers,添加下面语句:
user3 ALL=/sbin/shutdown -c

可以看到,在 user3 用户下,直接使用 shutdown -c 是不够权限的,但可以使用:
sudo /sbin/shutdown -c
来取消 root 用户的定时关机。