本教程介绍如何在 Ubuntu Linux 操作系统中添加、删除和授予用户Sudo权限。
在 Linux 和 Unix 操作系统中,有一个特殊的用户叫做 root,用户可以在root类 Unix 系统中做任何事情。
在日常活动中使用 root 用户可能很危险,因此不建议这样做,一个错误的命令可以毁掉整个系统!
Sudo 允许授权用户以 root 级别权限执行任务,即使他们不知道 root 用户密码。
这就是为什么创建一个普通用户并将他添加到 sudo 用户组以执行管理任务很重要的原因,因此,该用户在运行以 sudo 为前缀的命令时,既可以作为普通用户,也可以作为管理用户。
这些是成为 sudo 用户的一些优势。现在,让我们继续看看如何在 Ubuntu Linux 中为用户添加、删除和授予 Sudo 权限。
首先,我们将创建一个普通用户。
首先,让我们创建一个普通用户,例如“senthil”。
为此,请运行:
$ sudo adduser senthil
将“senthil”替换为您自己的用户名。
样本输出:
Adding user `senthil' ...
Adding new group `senthil' (1001) ...
Adding new user `senthil' (1001) with group `senthil' ...
Creating home directory `/home/senthil' ...
Copying files from `/etc/skel' ...
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: password updated successfully
Changing the user information for senthil
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] y
我们刚刚创建了一个名为“senthil”的新用户,该用户尚未获得 sudo 访问权限。所以他不能执行任何管理任务。
您可以验证用户是否具有 sudo 访问权限,如下所示:
$ sudo -l -U senthil
样本输出:
User senthil is not allowed to run sudo on ubuntu2204.
使用以下命令将新创建的用户添加到sudo 组:
$ sudo adduser senthil sudo
样本输出:
Adding user `senthil' to group `sudo' ...
Adding user senthil to group sudo
Done.
我们向用户“senthil”授予了 sudo 权限。
您还可以使用以下命令将用户添加到 sudo 组。
$ sudo usermod -aG sudo senthil
要验证用户是否已添加到 sudo 组中,请运行:
$ sudo -l -U senthil
样本输出:
Matching Defaults entries for senthil on ubuntu2204:
env_reset, mail_badpass,
secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin,
use_pty
User senthil may run the following commands on ubuntu2204:
(ALL : ALL) ALL
在这里,“(全部:全部)全部”line 表示用户拥有无限权限,可以在系统上运行任何命令。在我们的例子中,“ senthil ”用户已被添加到sudo 用户组中。从现在开始,他可以执行各种管理任务。
如果您打开sudoers文件的内容;
$ sudo cat /etc/sudoers
您会看到如下所示的一些行。
# 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
正如您在上面的输出中看到的,sudo 组的所有成员都可以执行任何命令。
注意:在某些 Linux 系统中,例如 Arch Linux,您需要在创建新的 sudo 用户之前安装“sudo”包。
# pacman -S sudo
在 Debian 上:
# apt install sudo
在 Ubuntu 桌面版和服务器版上,默认安装“sudo”。
要验证用户是否能够执行管理任务,请注销并以新用户身份重新登录。
或者,您可以立即切换到另一个用户,而无需退出当前会话,如下所示。
$ sudo -i -u <username>
例子:
$ sudo -i -u Senthil
我们切换到用户“senthil”。
现在,运行任何带有前缀“sudo”的命令,如下所示。
$ sudo apt update
是的,用户“senthil 可以使用 sudo 权限运行管理命令。
您可以删除用户的 sudo 权限,而无需完全删除他/她。
警告:在 Ubuntu 系统中执行此操作时必须小心。不要从“sudo”组中删除真正的管理员。系统中应该至少有一个 sudo 用户。
首先,确保您已从用户“senthil”会话中注销并以另一个 sudo 用户身份登录。
要撤销用户的 sudo 权限(例如 senthil),命令将是:
$ sudo deluser senthil sudo
上述命令将从“sudo”组中删除名为“senthil”的用户。
样本输出:
Removing user `senthil' from group `sudo' ...
Done.
请注意,此命令只会从 sudo 组中删除用户 'senthil',但不会从系统中永久删除用户。
或者,运行以下命令来撤销用户的 sudo 权限:
$ sudo gpasswd -d senthil sudo
现在,该用户成为普通用户,并且无法使用 sudo 权限执行任何管理任务。
要验证用户是否真的已从“sudo”组中删除,请运行:
$ sudo -l -U senthil
样本输出:
User senthil is not allowed to run sudo on ubuntu2204.
已删除用户的 sudo 权限。
在上述步骤中,我们只从“sudo”组中删除了用户。但是用户仍然存在于系统中。要从 Linux 系统中完全删除用户,请以 root 或 sudo 用户身份登录并运行:
$ sudo deluser <username>
例子:
$ sudo deluser senthil
如果要删除用户及其主目录和邮件假脱机,请运行:
$ sudo deluser --remove-home senthil
样本输出:
Looking for files to backup/remove ...
Removing files ...
Removing user `senthil' ...
Warning: group `senthil' has no more members.
Done.
adduser有关更多详细信息,请查看deluser和sudo命令的手册页。
$ man adduser
$ man deluser
$ man sudo
在这个详细的教程中,我们了解了关于 sudo 的几个重要事项,首先,简要介绍了 sudo 及其好处,然后讨论了如何在 Ubuntu 22.04 LTS 操作系统中添加、删除和授予用户 sudo 权限,最后,我们看到了如何撤销 sudo 权限以及如何永久删除用户。
尽管它是专门为 Ubuntu 编写的,但这种方法对于其他基于 Ubuntu 和基于 DEB 的系统是完全相同的。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。