As a systems administrator or regular Linux user, you may be required to create additional users in the system so that other users can reap the benefits of interacting with the system. In this tutorial, we will walk you through how to create and manage new users on a Linux system.
作为系统管理员或常规Linux用户,可能需要您在系统中创建其他用户,以便其他用户可以获得与系统交互的好处。 在本教程中,我们将引导您逐步了解如何在Linux系统上创建和管理新用户。
创建新用户 (Creating New Users)
There are 2 commands that can be used to create users in the system.
有2个命令可用于在系统中创建用户。
- adduser 添加用户
- useradd 用户添加
Let’s have a look at each of them in detail.
让我们详细了解它们中的每一个。
adduser命令 (adduser command)
adduser
command is a user-friendly Perl script that creates a fully functional Linux user on the system.
adduser
命令是用户友好的Perl脚本,可在系统上创建功能齐全的Linux用户。
句法 (Syntax)
The syntax for adding a new user to a Linux system is as shown below
将新用户添加到Linux系统的语法如下所示
# adduser username
adduser
command offers a high-level interface and performs the following functions
adduser
命令提供了高级界面并执行以下功能
- Creates a new user called ‘username’ 创建一个名为“用户名”的新用户
- Creates a new group with the new user’s username 用新用户的用户名创建一个新组
- Places the new user in the newly created group bearing user’s username 将新用户放置在带有用户名的新创建的组中
- Creates a home directory (/home/username) and later copy the files from
/etc/skel
into it. 创建一个主目录(/ home / username),然后将文件从/etc/skel
复制到其中。 - Prompts for additional information about the new user such as Full Name , Room Number, Work Phone, and Home Phone 提示输入有关新用户的其他信息,例如全名,房间号,工作电话和家庭电话
添加新用户 (Adding a new user)
In this example, we are going to add a user ‘penguin’ to the system
在此示例中,我们将向系统添加用户“企鹅”
# adduser penguin
>Sample Output
>样品输出
The list of users in a Linux system is stored in /etc/passwd
file. Therefore, to verify that the user has been created run
Linux系统中的用户列表存储在/etc/passwd
文件中。 因此,要验证是否已创建用户,请运行
cat /etc/passwd
>Sample Output
>样品输出
useradd命令 (useradd command)
useradd
command is a native binary command and is considered a low-level utility for adding users to the system. The command cannot achieve most of the functionalities that adduser
accomplishes at once and therefore , requires more options in its syntax.
useradd
命令是本机二进制命令,被认为是用于将用户添加到系统的低级实用程序。 该命令无法实现adduser
完成的大多数功能,因此,其语法需要更多选项。
句法 (Syntax)
The syntax for using the useradd
command is
使用useradd
命令的语法是
useradd -option username
-m选项 (-m option)
By default, useradd
command does not create a home directory for the new user like adduser
command. This is show in the output below where a new user ‘james’ has been created.
默认情况下, useradd
命令不会像adduser
命令那样为新用户创建主目录。 这显示在下面的输出中,其中已创建了新用户“ james”。
Sample Output
样本输出
To create a user along with the home directory, use the -m
option as shown.
要与主目录一起创建用户,请使用-m
选项,如图所示。
$ useradd -m username
For example, to create user ‘james’ alongside his home directory, execute:
例如,要在用户的主目录旁边创建用户“ james”,请执行以下命令:
$ useradd -m james
Now you can verify the existence of the home directory for the user ‘james’ by running:
现在,您可以通过运行以下命令来验证用户“ james”的主目录是否存在:
$ ls /home
Sample Output
样本输出
-u(–uid)选项 (-u (–uid) option)
The -u
option creates the user with a specific UID (User ID). A User ID is a positive integer that is automatically assigned to a new user by the Linux system.
-u
选项创建具有特定UID(用户ID)的用户。 用户ID是一个正整数,Linux系统会自动将其分配给新用户。
For example, to create a user ‘james’ with a UID of 1400 execute:
例如,要创建UID为1400的用户“ james”,请执行以下操作:
$ useradd -u 1400 james
This can be verified by the command
可以通过以下命令验证
$ id james
Sample Output
样本输出
Alternatively, you can verify the UID by running
或者,您可以通过运行来验证UID
id -u james
Sample Output
样本输出
-g(–gid)选项 (-g (–gid) option)
The -g
or --gid
option creates a user belonging to a certain group by specifying either the group name or the GID number.
-g
或--gid
选项通过指定组名或GID号来创建属于某个组的用户。
For example, to create the user ‘james’ belonging to a new group called users, execute the command
例如,要创建属于名为用户的新组的用户“ james”,请执行以下命令
$ useradd -g users james
To verify this, run the command below
要验证这一点,请运行以下命令
$ id -gn james
Sample Output
样本输出
-c(–comment)选项 (-c (–comment) option)
The -c (–comment) option allows you to create a user with a brief description of the new user. For example, to create a new user ‘james’ with a string “IT department” execute
-c(–comment)选项允许您创建一个带有新用户简要说明的用户。 例如,创建带有字符串“ IT department”的新用户“ james”,请执行
$ useradd -c "IT departmemt" james
The comment is stored in the /etc/passwd
file. You can use the cat
command to reveal the details of the user a shown
注释存储在/etc/passwd
文件中。 您可以使用cat
命令来显示用户的详细信息,如图所示
Sample Output
样本输出
-e(–expiredate)选项 (-e (–expiredate) option)
If you want to define a date upon which a new user account will expire, then use the -e
option.
如果要定义新用户帐户的到期日期,请使用-e
选项。
For instance, to create a new user called ‘james’ with an expiry date set to June 10 2019
execute
例如,要创建一个名为“ james”的新用户,并将其到期日设置为June 10 2019
执行
$ useradd -e 2019-06-10 username
Next, use the chage
command to verify the user account expiry date:
接下来,使用chage
命令验证用户帐户的到期日期:
sudo chage -l username
Sample Output
样本输出
将用户添加到sudoers组 (Adding users to sudoers group)
Sometimes, you may need to delegate some administrative rights to regular users so that they can perform some administrative tasks. To accomplish this, you need to add them to the sudoers
group.
有时,您可能需要将某些管理权限委派给普通用户,以便他们可以执行某些管理任务。 为此,您需要将它们添加到sudoers
组。
One way of adding users to the sudoers group is by running the command below
将用户添加到sudoers组的一种方法是运行以下命令
$ usermod -a -G sudo username
Where username
is the user that you want to add to the sudoers group. For instance, to add the user ‘james’ to sudoers
group run the command
其中username
是要添加到sudoers组的用户。 例如,要将用户“ james”添加到sudoers
组,请运行以下命令
$ usermod -a -G sudo james
To verify that the user ‘james’ has been added to the sudo group run
验证用户“ james”已添加到sudo组中,运行
groups james
Sample output
样品输出
Alternatively, you can edit your sudoers
file using the visudo
command by running
或者,您可以通过运行以下命令使用visudo
命令编辑sudoers
文件:
visudo
This will open the file as shown below
这将打开文件,如下所示
Next, append the following line
接下来,添加以下行
username ALL = (ALL)ALL
Save and exit the text editor.
保存并退出文本编辑器。
Now you can switch to the regular user and execute an admin command by preceding the username with
sudo
as shown.
现在,您可以切换到普通用户并通过在用户名前面加上来执行管理命令
sudo
如图所示。
删除用户 (Deleting users)
To completely delete a user from the Linux system, use the userdel
command with -r
attribute as shown
要从Linux系统中完全删除用户,请使用带有-r
属性的userdel
命令,如下所示
$ userdel -r username
To delete user ‘james’ run the below command.
要删除用户“ james”,请运行以下命令。
$ userdel -r james
结论 (Conclusion)
In this article, you learned how to create and manage new users in Linux with adduser
and useradd
commands. These commands run across all distributions, both Debian and Redhat distributions.
在本文中,您学习了如何使用adduser
和useradd
命令在Linux中创建和管理新用户。 这些命令在Debian和Redhat发行版的所有发行版上运行。
翻译自: https://www.journaldev.com/28677/create-manage-new-users-linux