(Introduction)

The sudo command provides a mechanism for granting administrator privileges — ordinarily only available to the root user — to normal users. This guide will show you how to create a new user with sudo access on CentOS 8, without having to modify your server’s /etc/sudoers file.

sudo命令提供了一种向普通用户授予管理员特权(通常仅对root用户可用)的机制。 本指南将向您展示如何在CentOS 8上创建具有sudo访问权限的新用户,而无需修改服务器的/etc/sudoers文件。

Note: If you want to configure sudo for an existing CentOS user, skip to step 3.

注意:如果要为现有的CentOS用户配置sudo ,请跳至步骤3。

(Step 1 — Logging Into Your Server)

SSH in to your server as the root user:

root用户身份登录到服务器:



  • ssh root@your_server_ip_address
    ssh root @ your_server_ip_address



Use your server’s IP address or hostname in place of your_server_ip_address above.

使用服务器的IP地址或主机名代替上面的your_server_ip_address

(Step 2 — Adding a New User to the System)

Use the adduser command to add a new user to your system:

使用adduser命令将新用户添加到您的系统:



  • adduser sammy
    adduser sammy



Be sure to replace sammy with the username you’d like to create.

确保将sammy替换为您要创建的用户名。

Use the passwd command to update the new user’s password:

使用passwd命令更新新用户的密码:



  • passwd sammy
    密码萨米



Remember to replace sammy with the user that you just created. You will be prompted twice for a new password:

请记住用刚创建的用户替换sammy 。 系统将提示您两次输入新密码:

Output
   
   Changing password for user sammy.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

步骤3 —将用户添加到wheel组 (Step 3 — Adding the User to the wheel Group)

Use the usermod command to add the user to the wheel group:

使用usermod命令将用户添加到Wheel组:



  • usermod -aG wheel sammy
    usermod -aG车轮萨米



Once again, be sure to replace sammy with the username you’d like to give sudo priveleges to. By default, on CentOS, all members of the wheel group have full sudo access.

再次确保将sammy替换为您要赋予sudo特权的用户名。 默认情况下,在CentOS上, wheel组的所有成员都具有完全的sudo访问权限。

步骤4 —测试sudo访问 (Step 4 — Testing sudo Access)

To test that the new sudo permissions are working, first use the su command to switch from the root user to the new user account:

要测试新的sudo权限是否正常运行,请首先使用su命令从root用户切换到新用户帐户:



  • su - sammy
    苏- 萨米



As the new user, verify that you can use sudo by prepending sudo to the command that you want to run with superuser privileges:

作为新用户,验证您可以使用sudo通过预先sudo到您想要拥有超级用户权限运行以下命令:



  • sudo command_to_run
    须藤command_to_run



For example, you can list the contents of the /root directory, which is normally only accessible to the root user:

例如,您可以列出/root目录的内容,该目录通常只能由root用户访问:



  • sudo ls -la /root



The first time you use sudo in a session, you will be prompted for the password of that user’s account. Enter the password to proceed:

第一次在会话中使用sudo时,系统将提示您输入该用户帐户的密码。 输入密码以继续:

Output
   
   [sudo] password for sammy:

Note: This is not asking for the root password! Enter the password of the sudo-enabled user, not the root password.

注意:不是要求输入root密码! 输入启用了sudo的用户的密码,而不是root密码。

If your user is in the proper group and you entered the password correctly, the command that you issued with sudo will run with root privileges.

如果您的用户在适当的组中,并且您输入的密码正确,那么使用sudo发出的sudo将以root特权运行。

(Conclusion)

In this quickstart tutorial we created a new user account and added it to the wheel group to enable sudo access. For more detailed information on setting up a CentOS 8 server, please read our Initial Server Setup with CentOS 8 tutorial.

在本快速入门教程中,我们创建了一个新的用户帐户,并将其添加到wheel组中以启用sudo访问。 有关设置CentOS 8服务器的更多详细信息,请阅读我们的CentOS 8初始服务器设置教程。

翻译自: https://www.digitalocean.com/community/tutorials/how-to-create-a-new-sudo-enabled-user-on-centos-8-quickstart