centos添加用户和权限

介绍 (Introduction)

When you first start using a fresh Linux server, adding and removing users is often one of first things you’ll need to do. In this guide, we will cover how to create user accounts, assign sudo privileges, and delete users on a CentOS 8 server.

刚开始使用新的Linux服务器时,添加和删除用户通常是您需要做的第一件事。 在本指南中,我们将介绍如何在CentOS 8服务器上创建用户帐户,分配sudo特权以及删除用户。

先决条件 (Prerequisites)

This tutorial assumes you are logged into a CentOS 8 server with a non-root sudo-enabled user. If you are logged in as root instead, you can drop the sudo portion of all the following commands, but they will work either way.

本教程假定您使用非root用户启用了sudo登录到CentOS 8服务器。 如果改为以root用户身份登录,则可以删除以下所有命令的sudo部分,但它们将以两种方式起作用。

新增使用者 (Adding Users)

Throughout this tutorial we will be working with the user sammy. Please susbtitute with the username of your choice.

在整个教程中,我们将与sammy用户合作。 请使用您选择的用户名表示怀疑。

You can add a new user by typing:

您可以通过输入以下内容添加新用户:

  • sudo adduser sammy

    sudo adduser sammy

Next, you’ll need to give your user a password so that they can log in. To do so, use the passwd command:

接下来,您需要为用户提供密码,以便他们可以登录。为此,请使用passwd命令:

  • sudo passwd sammy

    sudo passwd sammy

You will be prompted to type in the password twice to confirm it. Now your new user is set up and ready for use!

系统将提示您输入两次密码以进行确认。 现在,您的新用户已经设置好并可以使用!

Note: if your SSH server disallows password-based authentication, you will not yet be able to connect with your new username. Details on setting up key-based SSH authentication for the new user can be found in step 5 of Initial Server Setup with CentOS 8.

注意:如果您的SSH服务器不允许基于密码的身份验证,则您将无法使用新的用户名进行连接。 在CentOS 8的“ 初始服务器设置”的第5步中可以找到有关为新用户设置基于密钥的SSH身份验证的详细信息。

向用户授予Sudo特权 (Granting Sudo Privileges to a User)

If your new user should have the ability to execute commands with root (administrative) privileges, you will need to give them access to sudo.

如果您的新用户应具有执行具有root (管理)特权的命令的能力,则需要向他们授予sudo访问权限。

We can do this by adding the user to the wheel group (which gives sudo access to all of its members by default).

我们可以通过将用户添加到wheel组来实现此目的(默认情况下, sudo可以对其所有成员进行sudo访问)。

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

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

  • sudo usermod -aG wheel sammy

    sudo usermod -aG轮萨米

Now your new user is able to execute commands with administrative privileges. To do so, append sudo ahead of the command that you want to execute as an administrator:

现在,您的新用户可以执行具有管理特权的命令。 为此,请将sudo附加在要以管理员身份执行的命令之前:

  • sudo some_command

    须藤some_command

You will be prompted to enter the password of the your user account (not the root password). Once the correct password has been submitted, the command you entered will be executed with root privileges.

系统将提示您输入用户帐户的密码 ( 而不是root密码 )。 提交正确的密码后,将以root特权执行您输入的命令。

使用Sudo特权管理用户 (Managing Users with Sudo Privileges)

While you can add and remove users from a group with usermod, the command doesn’t have a way to show which users are members of a group.

尽管可以使用usermod从组中添加和删除用户,但该命令无法显示哪些用户是组成员。

To see which users are part of the wheel group (and thus have sudo privileges), you can use the lid command. lid is normally used to show which groups a user belongs to, but with the -g flag, you can reverse it and show which users belong in a group:

要查看哪些用户属于wheel组(并因此具有sudo特权),可以使用lid命令。 lid通常用于显示用户所属的组,但是使用-g标志,您可以反转它并显示用户所属的组:

  • sudo lid -g wheel

    须藤盖-g轮

   
   
Output
centos(uid=1000) sammy(uid=1001)

The output will show you the usernames and UIDs that are associated with the group. This is a good way of confirming that your previous commands were successful, and that the user has the privileges that they need.

输出将显示与该组关联的用户名和UID。 这是确认先前命令是否成功以及用户具有所需特权的一种好方法。

删除用户 (Deleting Users)

If you have a user account that you no longer need, it’s best to delete it.

如果您拥有不再需要的用户帐户,则最好将其删除。

To delete the user without deleting any of their files, use the userdel command:

要删除用户而不删除其任何文件,请使用userdel命令:

  • sudo userdel sammy

    须藤userdel sammy

If you want to delete the user’s home directory along with their account, add the -r flag to userdel:

如果要删除用户的主目录及其帐户,请将-r标志添加到userdel

  • sudo userdel -r sammy

    须藤userdel -r sammy

With either command, the user will automatically be removed from any groups that they were added to, including the wheel group if applicable. If you later add another user with the same name, they will have to be added to the wheel group again to gain sudo access.

使用任一命令,都会自动将用户从添加到的任何组中删除,包括wheel组(如果适用)。 如果以后添加另一个具有相同名称的用户,则必须将其再次添加到wheel组,以获得sudo访问权限。

结论 (Conclusion)

You should now have a good grasp on how to add and remove users from your CentOS 8 server. Effective user management will allow you to separate users and give them only the access that is needed for them to do their job.

现在,您应该对如何在CentOS 8服务器中添加和删除用户有了很好的了解。 有效的用户管理将使您能够分离用户,并仅向他们提供其完成工作所需的访问权限。

You can now move on to configuring your CentOS 8 server for whatever software you need, such as a LAMP or LEMP web stack.

现在,您可以继续为所需的任何软件(例如LAMPLEMP Web堆栈)配置CentOS 8服务器。

翻译自: https://www.digitalocean.com/community/tutorials/how-to-add-and-delete-users-on-centos-8

centos添加用户和权限

Logo

更多推荐