The Linux useradd command does exactly what its name suggests, it adds users. Linux also has other commands to carry out this task meaning you have several options available to you.

What is the Linux useradd command?

The Linux command useradd is one of your many options if you want to add a new user via the terminal. While various Linux dis­tri­bu­tions such as Ubuntu use this command because it’s not in­ter­act­ive and tends to ex­per­i­ence fewer problems, the command adduser is often more secure. Al­tern­at­ively, you can also add new users via a graphical program. Read on for an ex­plan­a­tion of how to use the useradd command without any issues.

How does the useradd command work?

Linux useradd can only be used by those who have root rights or Linux sudo command priv­ileges. The values for the command are set in the file /etc/default/useradd. Based on these para­met­ers, the system creates a new user with a selected name. Ad­di­tion­ally you can use the useradd command to set passwords for new users. We explain how to do it below.

What does the useradd syntax look like?

The syntax for useradd looks as follows:

$ useradd [Options] [Username]
shell

If you don’t add any ad­di­tion­al options, the system will simply create a user with the name you enter.

What options does the useradd command have?

Linux useradd has numerous options. These are the most important:

  • -d: Here you set the start directory of the new user.
  • -e or –ex­pired­ate: Sets when the new account will expire, if you don’t enter anything it will never expire.
  • -f: Sets when an account will expire when the password belonging to it expires.
  • -g: Sets the user group the new user primarily belongs to.
  • -m: Creates the home directory of the new account if one currently doesn’t exist.
  • -p or –password: Set the password for the new account. However, for security reasons, the passwd command is better suited for this.
  • -s: Sets the standard login shell for all new accounts.

Examples of a useradd command

Finally you can see some examples of how to use the Linux useradd command:

$ sudo useradd testuser
shell

Creates the new user testuser.

$ sudo useradd -m testuser
shell

You can use this command to create a new home directory for testuser.

$ sudo useradd -e 2023-12-31 testuser
shell

This command creates an account for testuser which will be de­ac­tiv­ated on December 31st, 2023.

Go to Main Menu