useradd创建用户
If more than one person is using your Linux machine at home, or you are managing a server that provides access to multiple users, the useradd
command is essential for creating users.
如果有多个人在家中使用Linux机器,或者您正在管理一台可以访问多个用户的服务器,则useradd
命令对于创建用户至关重要。
Also, many of the services you use as a developer may require their own user accounts to function. So even as a solo dev on your own machine, you may find yourself reaching for these commands when you install MySQL or something similar.
此外,您作为开发人员使用的许多服务可能都需要其自己的用户帐户才能运行。 因此,即使作为自己计算机上的单独开发人员,您也可能会在安装MySQL或类似工具时发现需要使用这些命令。
You can get a full overview of the various options available to you by viewing the man page for the utility: man useradd
通过查看实用程序的手册页,可以全面了解各种可用选项: man useradd
But if that is overwhelming, here's a breakdown of some of the common options you might use when creating a user.
但是,如果您不知所措,那么这里是您创建用户时可能使用的一些常用选项的细分。
创建一个用户 (Create a user)
The simple format for this command is useradd [options] USERNAME
.
此命令的简单格式为useradd [options] USERNAME
。
For example useradd test
(as the root user - prefix with sudo
if you are not logged in as root).
例如, useradd test
(作为root用户-如果您未以root用户身份登录,则以sudo
)。
This will create a user called test, but it's a limited operation and will not create other useful things like their home directory or password!
这将创建一个名为test的用户,但这是一项有限的操作,并且不会创建其他有用的内容,例如其主目录或密码!
新增密码 (Add a password)
You then add a password for the test user by using the passwd
command: passwd test
. This will prompt you to enter a password for the user.
然后,您可以使用passwd
命令为测试用户添加密码: passwd test
。 这将提示您输入用户密码。
There is an option for adding an encrypted password via the -p
option on useradd
, but this is not recommended for security purposes.
有一个选项可以通过useradd
上的-p
选项添加加密的密码,但是出于安全考虑,不建议这样做。
Note that the -p
option doesn't allow you to input a plaintext password, it expects you to encrypt it first. This is intentionally difficult, because you should not do it! Just use the passwd
command.
请注意, -p
选项不允许您输入纯文本密码,它希望您先对其进行加密。 这是故意造成的,因为您不应该这样做! 只需使用passwd
命令。
其他常见选项 (Other common options)
主目录 (Home directories)
In order to create a user with the default home directory use the following option:
为了使用默认主目录创建用户,请使用以下选项:
useradd -m test
useradd -m test
This user now has a /home/test directory.
现在,该用户具有/ home / test目录。
To change the home directory, you can pass an extra option to modify this, for example:
要更改主目录,您可以传递一个附加选项来修改它,例如:
useradd -m -d /alternate test
useradd -m -d /alternate test
贝壳 (Shell)
By default, your created users will likely have the default login shell bin/bash or bin/sh, which will be defined in /etc/default/useradd
.
默认情况下,您创建的用户将可能具有默认的登录shell bin / bash或bin / sh,它们将在/etc/default/useradd
定义。
You can override this default with the -s
option:
您可以使用-s
选项覆盖此默认值:
useradd -s usr/bin/zsh test
useradd -s usr/bin/zsh test
全部放在一起 (Putting it all together)
To construct the whole command, you put the options in one after another - the order does not matter - and end with the username you wish to create.
要构建整个命令,可以将选项一个接一个地放置-顺序无关紧要-并以要创建的用户名结尾。
So creating a user with a home directory and a customized shell would look like this:
因此,创建具有主目录和自定义外壳程序的用户将如下所示:
useradd -m -s /usr/bin/zsh user
useradd -m -s /usr/bin/zsh user
And then you would add a password for the user: passwd user
然后,您需要为该用户添加一个密码: passwd user
阅读精美手册 (Read the Fine Manual)
Now that you've seen the basics of what this tool can do, hopefully the man page is a little more navigable.
既然您已经了解了该工具的基本功能,则希望手册页更具导航性。
man useradd
will show you how to add things like expiry dates on the account, assign groups, and so on.
man useradd
将会向您展示如何在帐户中添加诸如到期日期,分配组等内容。
翻译自: https://www.freecodecamp.org/news/linux-how-to-add-users-and-create-users-with-useradd/
useradd创建用户