


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
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
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
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
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
按顺序,冒号“:”分隔的字段为:
- maryq:用户帐户的名称。
- x:此字段中的“x”表示用户帐户密码已加密并保存在“/etc/shadow”文件中。
- 1001:用户帐户ID。
- 1001:此用户帐户的默认组的ID。
- Mary Quinn:这是GECOS领域。它可以保存一组逗号“,”分隔的额外信息值。我们添加的只是用户的全名。
- /home/maryq:此帐户的主目录的路径。
- /bin/bash:该帐户的默认shell的路径。
当我们的新用户首次登录时,他们将使用你为他们创建的密码。
因为我们将他的密码设置为“过期”条件,所以会提示他更改密码。他必须重新输入现有密码。
然后提示他输入新密码。
一旦他键入新密码并点击“回车”,就会要求他重新输入密码以验证它。
最后,他们登录了。从现在开始,他们必须使用新密码登录。
执行一些内部管理,并在它们的主目录中为它们创建通常的“文档”、“下载”和其他目录。
GECOS字段最多可以包含五条以逗号分隔的信息。这些很少使用。如果填充了任何内容,则通常是第一个,它保存该帐户所有者的真实名称。
这些字段包括:
- 此用户的真实名称。
- 此用户的房间号。
- 他们的工作电话。
- 他们的家庭电话。
- 任何其他信息。
如果我们想在创建帐户时提供所有这些,我们可以这样做,如下所示:
sudo useradd -s /bin/bash -m -c "Mary Quinn,Operations 1,555-6325,555-5412,Team Leader" -Gsambashare maryq
我们可以使用grep来查看此信息是否存储在“/etc/passwd”文件中。
grep maryq /etc/passwd
如果在创建帐户时没有这些信息,则可以在以后使用chfn命令添加或更改这些信息。
该信息由finger和pinky等命令使用。
finger maryq
使用adduser命令创建新用户
adduser命令将帐户的创建、其主目录、设置密码和捕获GECOS字段信息包装到一个交互式会话中。
adduser命令已经存在于我们的Ubuntu和Fedora测试机器上,但必须安装在Manjaro上。它位于Arch User Repository中,因此你需要使用诸如yay的AUR助手来安装它。
yay adduser
要启动该过程,请使用sudo并提供要添加的用户帐户的名称:
sudo adduser maryq
将创建用户帐户的默认组,并添加用户帐户,并将该组作为其默认组。将创建主目录,并将隐藏的配置文件复制到其中。
系统将提示你提供密码。
当你提供密码并单击“回车”时,系统会提示你重新输入密码以进行验证。
依次要求你提供可以进入GECOS字段的每一条信息。
提供一些信息,然后按“回车”移动到下一个字段,或者只按“回车”跳过字段。
最后,询问你提供的信息是否正确。按“Y”键,然后按“回车”完成该过程。
记住将新帐户的密码设置为“过期”,以便新用户在首次登录时需要更改密码。
sudo password --expire maryq
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.
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.
You need to enter your password.
A green "Add User" button will appear.
Click this button. The Add User dialog box appears. It contains a form that captures the new user's details.
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.
As you would expect, he has to enter it again to verify it.
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!

linux设备节点是应用程序和设备驱动程序沟通的一个桥梁;设备节点被创建在“/dev”,是连接内核与用户层的枢纽,相当于硬盘的inode一样的东西,记录了硬件设备的位置和信息。设备节点使用户可以与内核进行硬件的沟通,读写设备以及其他的操作。

区别:1、open是UNIX系统调用函数,而fopen是ANSIC标准中的C语言库函数;2、open的移植性没fopen好;3、fopen只能操纵普通正规文件,而open可以操作普通文件、网络套接字等;4、open无缓冲,fopen有缓冲。

端口映射又称端口转发,是指将外部主机的IP地址的端口映射到Intranet中的一台计算机,当用户访问外网IP的这个端口时,服务器自动将请求映射到对应局域网内部的机器上;可以通过使用动态或固定的公共网络IP路由ADSL宽带路由器来实现。

在linux中,eof是自定义终止符,是“END Of File”的缩写;因为是自定义的终止符,所以eof就不是固定的,可以随意的设置别名,linux中按“ctrl+d”就代表eof,eof一般会配合cat命令用于多行文本输出,指文件末尾。

在linux中,可以利用“rpm -qa pcre”命令判断pcre是否安装;rpm命令专门用于管理各项套件,使用该命令后,若结果中出现pcre的版本信息,则表示pcre已经安装,若没有出现版本信息,则表示没有安装pcre。

linux查询mac地址的方法:1、打开系统,在桌面中点击鼠标右键,选择“打开终端”;2、在终端中,执行“ifconfig”命令,查看输出结果,在输出信息第四行中紧跟“ether”单词后的字符串就是mac地址。

在linux中,rpc是远程过程调用的意思,是Reomote Procedure Call的缩写,特指一种隐藏了过程调用时实际通信细节的IPC方法;linux中通过RPC可以充分利用非共享内存的多处理器环境,提高系统资源的利用率。

手机远程linux工具有:1、JuiceSSH,是一款功能强大的安卓SSH客户端应用,可直接对linux服务进行管理;2、Termius,可以利用手机来连接Linux服务器;3、Termux,一个强大的远程终端工具;4、向日葵远程控制等等。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.