The Linux useradd command is used to add new users. However, Linux also offers a few other commands for this purpose, these giving you more possibilities overall.
Linux useradd command: what is it?¶
The Linux useradd command is one of many options available to you to create a new user through the terminal. Although many Linux distributions, including Ubuntu, use this command, it is not interactive and comes with its own set of pitfalls; the adduser command is often a safer solution. To add new users, you can also go through a graphical utility. Discover with us how to use the useradd command wisely and without encountering the slightest problem.
How does the useradd command work?¶
The Linux useradd command can only be used by people with root rights or privileges for the Linux sudo command. The command values are indicated in the file /etc/default/useradd. Based on these settings, the system creates a new user with the name you have chosen. The useradd command also gives you the possibility to directly define a password for this new user. We’ll show you how to do it below.
What does the Linux useradd command syntax look like?¶
The useradd command syntax is as follows:
$ useradd [Options] [Nom d’utilisateur]
shell
If you don’t specify any additional options, the system will just create a new user with the registered name.
What are the useradd command options?¶
You have different options when it comes to the Linux useradd command. Below are the most important of them.
- -d: it allows you to define a starting directory for the new user.
- -e or –expiredate: it allows you to define the date on which this new account should be deactivated; if you don’t use it, there is no expiration date associated with it.
- -f: it allows you to define the number of days after which an account is deactivated after the expiration of the password associated with it.
- -g: it allows you to define the group of users to which the new account belongs in priority.
- -m: it allows you to create the home directory of the new account, provided that it does not yet exist.
- -p or –password: it allows you to define a password for the new account. In terms of security, however, the passwd command is still more appropriate.
- -s: it allows you to define the default login shell for all new accounts.
useradd Command: Examples¶
Finally, here are some examples of using the Linux useradd command:
$ sudo useradd testuser
shell
This command creates the new user testuser.
$ sudo useradd -m testuser
shell
This command creates a new home directory for testuser.
$ sudo useradd -e 2023-12-31 testuser
shell
This command allows you to create an account for testuserwhich will be deactivated on December 31, 2023.