How to use the Linux useradd command
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 distributions such as Ubuntu use this command because it’s not interactive and tends to experience fewer problems, the command adduser is often more secure. Alternatively, you can also add new users via a graphical program. Read on for an explanation 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 privileges. The values for the command are set in the file /etc/default/useradd. Based on these parameters, the system creates a new user with a selected name. Additionally 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]
shellIf you don’t add any additional 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 –expiredate: 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
shellCreates the new user testuser.
$ sudo useradd -m testuser
shellYou can use this command to create a new home directory for testuser.
$ sudo useradd -e 2023-12-31 testuser
shellThis command creates an account for testuser which will be deactivated on December 31st, 2023.