参考资料
练习题 icon lost
交流讨论
笔记
img lost

Linux all operations are done with users. In Linux environment to create a file,  start a service, open a network port we need a user with required privileges. Linux users are stored in plain test database at /etc/passwd . As old times user passwords also stored in this file but on modern time for security reasons user passwords are hold in /etc/shadow . There is also user groups to manage permission better. User groups are stored in plain text database /etc/group . In this tutorial we will look how to add new user to the Linux.

Linux的所有操作均由用户完成。 在Linux环境中创建文件,启动服务,打开网络端口,我们需要具有必需特权的用户。 Linux用户存储在/ etc / passwd的纯测试数据库中 从前,用户密码也存储在此文件中,但是出于安全原因,在现代,用户密码保存在/ etc / shadow中。 也有用户组可以更好地管理权限。 用户组存储在纯文本数据库/ etc / group中。 在本教程中,我们将研究如何向Linux添加新用户。

获取有关useradd命令的帮助 (Getting Help About useradd Command)

useradd command is very useful command for system administrators. Knowing its parameters and options are very crucial for daily operations. To get quick help about useradd command issue following command.

useradd命令对于系统管理员来说是非常有用的命令。 了解其参数和选项对于日常操作至关重要。 要获得有关useradd命令的快速帮助,请发出以下命令。

$ useradd -h
useradd command help
useradd command help
useradd命令帮助

列出Linux系统中的当前用户(List Current Users in A Linux System)

Before adding a Linux user we will list existing users to provide non colliding username.

在添加Linux用户之前,我们将列出现有用户以提供不冲突的用户名。

$ cat /etc/passwd | cut -d : -f 1
List current Users
List current Users
列出当前用户

建立新使用者(Create New User)

We will create new user with name test. This user will be added to the /etc/passwd file with the default shell and other related parameters.

我们将使用名称测试创建新用户。 该用户将使用默认的shell和其他相关参数添加到/ etc / passwd文件中。

$ useradd test

We can list newly created user with the following command.

我们可以使用以下命令列出新创建的用户。

$ cat /etc/passwd | cut -d : -f 1 | grep test

创建具有不同主目录的用户 (Create A User With Different Home Directory)

By default useradd command will create a home directory in /home path with the same name as username.  For example if the username is test the home directory of the user will be /home/test 

默认情况下, useradd命令将在/ home路径中创建一个与用户名同名的主目录。 例如,如果用户名是test ,则用户的主目录将是/ home / test

$ useradd -d /home/mytest test5

This command will create test user with a home directory like /home/mytest 

此命令将使用主目录(例如/ home / mytest)创建测试用户

创建具有特定用户ID的用户(Create A User With Specific User ID)

Linux operating system gives users User ID in order to separate them. User ID’s starts from 500 and increments upon new user addition. But we can change this default behavior by setting specific UID without getting default one. -u will be provided to the useradd command to specify new users UID.

Linux操作系统为用户提供用户ID以便将他们分开。 用户ID从500开始,并在添加新用户后递增。 但是我们可以通过设置特定的UID来更改此默认行为,而无需获取默认UID。 -u将提供给useradd命令以指定新用户的UID。

$ useradd -u 567 nick
Create A User With Specific User ID
Create A User With Specific User ID
创建具有特定用户ID的用户

After user creation we check new users UID with grep command by providing user name. As we can see user nick is created with UID 567

创建用户后,我们通过提供用户名使用grep命令检查新用户的UID。 如我们所见,用户昵称是使用UID 567创建的

创建具有特定组ID的用户(Create A User With Specific Group ID)

While creating user in Linux operating system new group is created too. This group is dedicated to the new user and the new user is automatically assigned to this user group. The new user group ID is assigned by default and starts from 1000. This value can be changed with -g option.

在Linux操作系统中创建用户时,也会创建新的组。 该组专用于新用户,并且新用户自动分配给该用户组。 默认情况下,将分配新的用户组ID,该ID从1000开始。可以使用-g选项更改此值。

$ useradd -g 0 nick
Create A User With Specific Group ID
Create A User With Specific Group ID
创建具有特定组ID的用户

As we can see newly created user group assigned to the 0 which is root group.

如我们所见,新创建的用户组分配给0(即根组)。

LEARN MORE  How To Change User Password with passwd Command In Linux?
了解更多如何在Linux中使用passwd命令更改用户密码?

将用户添加到多个组 (Add A User To Multiple Groups)

In previous example we have added new user with the specified group ID. We have just provided only single group ID. We may also need to add user to the multiple groups by providing multiple group ID’s in a single command. This can be done with -G options like below.

在前面的示例中,我们添加了具有指定组ID的新用户。 我们仅提供了单个组ID。 我们可能还需要通过在单个命令中提供多个组ID将用户添加到多个组中。 可以使用-G选项(如下所示)来完成此操作。

$ useradd -G root,vboxusers,kvm nick
Add A User To Multiple Groups
Add A User To Multiple Groups
将用户添加到多个组

While creating new user nick we added nick to the groups named root,vboxusers,kvm with this example.

在创建新用户nick我们通过此示例将昵称添加到名为root,vboxusers,kvm的组中。

添加没有主目录的用户 (Add A User Without Home Directory)

Linux users are great way to login and use Linux systems. Being a Linux system user means having a home directory. But this is not always true. Should it be? Should all Linux users have a home directory? No. For example services users those used for Linux daemons and services do not needs home directories. Also there may be some situations we do not need home directories. Another example is using Linux users just for authentication and providing VPN service. With -M options new user will be created without a home directory.

Linux用户是登录和使用Linux系统的好方法。 成为Linux系统用户意味着拥有主目录。 但这并非总是如此。 应该是吗? 所有Linux用户都应该有一个主目录吗? 否。例如,用于Linux守护程序和服务的服务用户不需要主目录。 在某些情况下,我们也不需要主目录。 另一个示例是仅将Linux用户用于身份验证并提供VPN服务。 使用-M选项,将创建没有主目录的新用户。

$ useradd -M nick

创建一个具有帐户到期日期的用户 (Create A User With Account Expire Date)

In a busy system creating a lot of user without any restriction and future notification will create security problems. Some added users will be permanent for the Linux system but some others will be temporary and should be removed or at least locked after some time. While adding new users we can set account expire date for the new user. Just issue -e option to the useradd command like below. In this example we set 28 February 2017 as account expire time for user nick. The date format is YYYY-MM-DD .

在繁忙的系统中,创建大量用户而没有任何限制,将来的通知会造成安全问题。 一些添加的用户对于Linux系统将是永久的,而其他一些用户将是临时的,应在一段时间后将其删除或至少锁定。 在添加新用户时,我们可以设置新用户的帐户过期日期。 只需向useradd命令发出-e选项,如下所示。 在此示例中,我们将昵称设置为2017年2月28日作为帐户过期时间。 日期格式为YYYY-MM-DD

$ useradd -e 2017-02-28 nick

After adding user with a account expire date we can check this date with chage command like below.

在添加了具有帐户过期日期的用户后,我们可以使用如下的chage命令检查该日期。

$ chage -l nick
Create A User With Account Expire Date
Create A User With Account Expire Date
创建一个具有帐户到期日期的用户

创建一个密码过期日期的用户(Create A User With Password Expire Date)

Another way to secure newly created user is specifying password expire  date. Specifying password expire date will force the user to change his/her password at the specified date. This will make the user credentials more secure by changing the passwords regularly. To set password expire date use -f option with number of days . If we provide value -1 the password will never expire. We need to provide also a start day for this operation

保护新创建用户的另一种方法是指定密码过期日期。 指定密码到期日期将迫使用户在指定日期更改其密码。 通过定期更改密码,这将使用户凭据更安全。 要设置密码过期日期,请使用-f选项(天数)。 如果我们提供值-1 ,密码将永不过期。 我们还需要为此操作提供开始日期

$ useradd -e 2017-02-28 -f 60 nick
Create A User With Password Expire Date
Create A User With Password Expire Date
创建一个密码过期日期的用户

添加具有自定义注释的用户(Add User With Custom Comments)

While adding user we may need to add some extra informal information. This informal information can be the full name, phone number etc. The comment can be added with -c option like below. We will add the phone number of the new user named nick in this example.

在添加用户时,我们可能需要添加一些额外的非正式信息。 该非正式信息可以是全名,电话号码等。可以使用-c选项(如下所示)添加注释。 在此示例中,我们将添加名为nick的新用户的电话号码。

$ useradd -c "1212121" nick
Add User With Custom Comments
Add User With Custom Comments
添加具有自定义注释的用户

更改用户登录外壳(Change User Login Shell)

Linux users use shell to issue commands from command line interface. There are alternative command line interfaces. Default and most popular command line interface or shell is bash . The newly create user will have bash as default shell. The default shell can be changed while creating user with -s option like below. In this example we will set sh as default shell.

Linux用户使用Shell从命令行界面发出命令。 还有其他命令行界面。 默认和最受欢迎的命令行界面或外壳是bash 。 新创建的用户将使用bash作为默认外壳。 使用-s选项创建用户时,可以更改默认外壳程序,如下所示。 在此示例中,我们将sh设置为默认外壳。

$ useradd -s /bin/sh nick
Change User Login Shell
Change User Login Shell
更改用户登录外壳

禁用登录外壳(Disable Login Shell)

In previous example we have set shell preferences for the newly created user. There is also an option where user will have no shell. This will prevent user to login to the Linux system. This can be used also a security step for the system. We will set the created users default shell /bin/nologin which is not a shell.

在前面的示例中,我们为新创建的用户设置了shell首选项。 还有一个选项,用户将没有外壳。 这将防止用户登录Linux系统。 这也可以用作系统的安全步骤。 我们将为创建的用户设置默认外壳程序/bin/nologin ,它不是外壳程序。

$ useradd -s /bin/nologin nick
Disable Login Shell
Disable Login Shell
禁用登录外壳

如何将新用户帐户添加到Linux Infografic(How To Add New User Account To Linux Infografic)

How To Add New User Account To Linux Infografic
How To Add New User Account To Linux Infografic
如何将新用户帐户添加到Linux Infografic
LEARN MORE  How To Install and Use Cygwin With Terminal and SSH Examples?
了解更多如何通过终端和SSH示例安装和使用Cygwin?

翻译自: https://www.poftut.com/add-new-user-account-linux/

资料来源 如何向Linux添加新用户帐户
博客作者 cunjiu9486
前往答题
我的笔记