Home  >  Article  >  System Tutorial  >  How to create a new user in Linux? How to use the create new user command in Linux

How to create a new user in Linux? How to use the create new user command in Linux

WBOY
WBOYOriginal
2024-06-03 15:08:37828browse

Creating users on a Linux computer is essential for system administration, providing flexibility and security. PHP editor Banana will introduce three user creation methods in depth and explore the unique advantages and potential pitfalls of each method. Read this article to learn how to create a user through the command line interface, graphical user interface (GUI), or a command line tool such as useradd, and make the choice that best suits your needs.

Why Linux computers need users

A personal computer without users is of little use. Linux supports multiple users. Whether they log in at the same time and share power to the computer, or log in individually while using the computer exclusively, each person needs a unique user account.

User accounts encapsulate that user's work and provide privacy. It also allows control and management to be applied to the account. By changing the properties of a user account, such as the groups they belong to, different users can have different capabilities based on their needs or their role or functionality.

Whether you share a computer with family members or manage an organization's multi-user installation, creating user accounts is an essential management skill.

Use the useradd command to create a new Linux user

The useradd command is the lowest level command used to add users. The other commands act as friendlier front-ends to the useradd command. This adds some convenience and makes the process easier, but other commands can't do things that can't be done with the useradd and passwd commands.

The useradd command has many options, the options required to create a typical new user are shown below. Needless to say, you must use sudo to add users.

sudo useradd -s /bin/bash -m -c "Mary Quinn" -Gsambashare maryq

如何在linux中创建新用户? 在Linux中创建新用户命令的使用方法

The command consists of the following parts:

  • sudo:We need administrator rights to allow new users to access the computer.
  • useradd: useradd command.
  • -s /bin/bash: shell option, this will set the default shell for this new user.
  • -m:Generate home directory option, this will create a directory in the "/home/" directory with the same name as the new user account name.
  • -c "Mary Quinn": The full name of the new user, this is optional.
  • -Gsambashare: Additional group option, this is optional. New users will be added to the group with the same name as their account name. The -G option (note, capital "G") adds the user to the supplemental group. The group must already exist. We will also make the new user a member of the "sambashare" group.
  • maryq: The name of the new user account, this must be unique and cannot already be used by another user.

This will create the new user account, create its home directory, and populate it with some default hidden files. We can view their home directories like this:

sudo ls -ahl /home/maryq

如何在linux中创建新用户? 在Linux中创建新用户命令的使用方法

Our new users can't log in yet because we haven't created a password for them yet. It is possible to pass a password to the useradd command using its -p (password) option, but this is considered bad practice. Additionally, you have to provide the password in encrypted form, so it's not as simple as it sounds.

It is easier and more secure to set a password for a new account using the passwd command.

sudo passwd maryq

如何在linux中创建新用户? 在Linux中创建新用户命令的使用方法

You will be prompted for your password and then asked to enter it again to verify it. This password must be passed securely to new users. It is recommended to prompt them to change their password when they log in. This means they can choose their own password and no one else will know it.

sudo passwd --expire maryq

如何在linux中创建新用户? 在Linux中创建新用户命令的使用方法

We can view the new user account and compare it to the existing account by looking at the "/etc/passwd" file.

grep -E "dave|maryq" /etc/passwd

如何在linux中创建新用户? 在Linux中创建新用户命令的使用方法

按顺序,冒号“:”分隔的字段为:

  • maryq:用户帐户的名称。
  • x:此字段中的“x”表示用户帐户密码已加密并保存在“/etc/shadow”文件中。
  • 1001:用户帐户ID。
  • 1001:此用户帐户的默认组的ID。
  • Mary Quinn:这是GECOS领域。它可以保存一组逗号“,”分隔的额外信息值。我们添加的只是用户的全名。
  • /home/maryq:此帐户的主目录的路径。
  • /bin/bash:该帐户的默认shell的路径。

当我们的新用户首次登录时,他们将使用你为他们创建的密码。

如何在linux中创建新用户? 在Linux中创建新用户命令的使用方法

因为我们将他的密码设置为“过期”条件,所以会提示他更改密码。他必须重新输入现有密码。

如何在linux中创建新用户? 在Linux中创建新用户命令的使用方法

然后提示他输入新密码。

如何在linux中创建新用户? 在Linux中创建新用户命令的使用方法

一旦他键入新密码并点击“回车”,就会要求他重新输入密码以验证它。

如何在linux中创建新用户? 在Linux中创建新用户命令的使用方法

最后,他们登录了。从现在开始,他们必须使用新密码登录。

执行一些内部管理,并在它们的主目录中为它们创建通常的“文档”、“下载”和其他目录。

如何在linux中创建新用户? 在Linux中创建新用户命令的使用方法

GECOS字段最多可以包含五条以逗号分隔的信息。这些很少使用。如果填充了任何内容,则通常是第一个,它保存该帐户所有者的真实名称。

这些字段包括:

  • 此用户的真实名称。
  • 此用户的房间号。
  • 他们的工作电话。
  • 他们的家庭电话。
  • 任何其他信息。

如果我们想在创建帐户时提供所有这些,我们可以这样做,如下所示:

sudo useradd -s /bin/bash -m -c "Mary Quinn,Operations 1,555-6325,555-5412,Team Leader" -Gsambashare maryq

如何在linux中创建新用户? 在Linux中创建新用户命令的使用方法

我们可以使用grep来查看此信息是否存储在“/etc/passwd”文件中。

grep maryq /etc/passwd

如何在linux中创建新用户? 在Linux中创建新用户命令的使用方法

如果在创建帐户时没有这些信息,则可以在以后使用chfn命令添加或更改这些信息。

该信息由finger和pinky等命令使用。

finger maryq

如何在linux中创建新用户? 在Linux中创建新用户命令的使用方法

使用adduser命令创建新用户

adduser命令将帐户的创建、其主目录、设置密码和捕获GECOS字段信息包装到一个交互式会话中。

adduser命令已经存在于我们的Ubuntu和Fedora测试机器上,但必须安装在Manjaro上。它位于Arch User Repository中,因此你需要使用诸如yay的AUR助手来安装它。

yay adduser

如何在linux中创建新用户? 在Linux中创建新用户命令的使用方法

要启动该过程,请使用sudo并提供要添加的用户帐户的名称:

sudo adduser maryq

将创建用户帐户的默认组,并添加用户帐户,并将该组作为其默认组。将创建主目录,并将隐藏的配置文件复制到其中。

系统将提示你提供密码。

如何在linux中创建新用户? 在Linux中创建新用户命令的使用方法

当你提供密码并单击“回车”时,系统会提示你重新输入密码以进行验证。

依次要求你提供可以进入GECOS字段的每一条信息。

如何在linux中创建新用户? 在Linux中创建新用户命令的使用方法

提供一些信息,然后按“回车”移动到下一个字段,或者只按“回车”跳过字段。

如何在linux中创建新用户? 在Linux中创建新用户命令的使用方法

最后,询问你提供的信息是否正确。按“Y”键,然后按“回车”完成该过程。

记住将新帐户的密码设置为“过期”,以便新用户在首次登录时需要更改密码。

sudo password --expire maryq

如何在linux中创建新用户? 在Linux中创建新用户命令的使用方法

Create a new user using GNOME User Settings (GUI Options)

To create a new user in the GNOME desktop environment, open the system menu by clicking near the power, volume, and network icons on the right side of the GNOME panel.

如何在linux中创建新用户? 在Linux中创建新用户命令的使用方法

Click the "Settings" menu item.

The Settings application will open. Click the Users entry in the sidebar, and then click the Unlock button in the Users pane.

如何在linux中创建新用户? 在Linux中创建新用户命令的使用方法

You need to enter your password.

如何在linux中创建新用户? 在Linux中创建新用户命令的使用方法

A green "Add User" button will appear.

如何在linux中创建新用户? 在Linux中创建新用户命令的使用方法

Click this button. The Add User dialog box appears. It contains a form that captures the new user's details.

如何在linux中创建新用户? 在Linux中创建新用户命令的使用方法

Fill out the form with the new user's details. If you want them to be able to use sudo, click the "Administrator" button.

You can set their password immediately or let them choose a password when they log in for the first time. If you set a password, you must remember to open a terminal window and use the passwd command to set it to "expired" status. This will force them to set their own password when logging in for the first time.

When you are trying to use the GUI to complete the creation of a new user, it is a bit of a pain to have to go to the terminal.

If you click the "Allow users to set their own password the next time they log in" radio button, the user will be prompted for a new password when trying to log in. But the downside here is that the first person who tries a new account can set a password. Therefore, anyone who knows the account has been created and wants to control actual new users trying to log in can take over the account.

Neither situation is ideal.

After completing filling in and making selections, click the green "Add" button.

We selected the "Allow users to set their own password the next time they log in" option. When the user tries to log in, he is prompted for a new password. However, unlike the sequence we saw earlier, they will not be prompted for his current password because he does not have one.

如何在linux中创建新用户? 在Linux中创建新用户命令的使用方法

As you would expect, he has to enter it again to verify it.

如何在linux中创建新用户? 在Linux中创建新用户命令的使用方法

Which method should I use to add users

Not sure which of the user creation techniques we’ve outlined is right for you? OK, the useradd command provides fine-grained control, but there's a lot of work to do on the command line.

  • The adduser command makes life easier but does not allow you to enter new users into other groups.
  • The GUI approach in GNOME has drawbacks, no matter which password radio button you select.

Summarize

  • View 1: When creating a new user, security should be the first priority. By choosing strong passwords, limiting user permissions, and regularly reviewing user accounts, you can effectively reduce the risk of system attacks.
  • Argument 1:In order to ensure system security, the principle of least privilege should be followed when creating a new user, that is, only granting the user the necessary permissions. This can be achieved by adding the user to the appropriate user group and restricting their access rights. In addition, changing passwords regularly and using a strong password policy are also important measures to improve account security.
  • View 2: User management is not just as simple as creating and deleting users, but also includes rights management, resource allocation and other aspects. Therefore, administrators need to have comprehensive knowledge of Linux system management in order to better manage user accounts.
  • Argument 2: In the Linux system, user management is a comprehensive task involving many aspects. In addition to basic user creation and deletion operations, administrators also need to pay attention to user permission settings, resource allocation, behavior monitoring, etc. Therefore, in order to be qualified for this task, administrators need to continuously learn and improve their Linux system management knowledge, including user management, file system, process management, network configuration, etc. Only in this way can the stability and security of the system be better guaranteed.

In most informal or domestic situations, the adduser command will probably give you the best balance between functionality and performance. If you need to add a new user to other groups, you can use the usermod command to perform this operation after creating the new user. I hope you like it, please continue to pay attention to this site.

related suggestion:

How to choose between Linux and Windows systems Introduction to the differences between Linux and Windows systems

The above is the detailed content of How to create a new user in Linux? How to use the create new user command in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn