In Linux, a ‘useradd‘ command is a low-level utility that is used for adding/creating user accounts in Linux and other Unix-like operating systems. The ‘adduser‘ is much similar to useradd command, because it is just a symbolic link to it.
Basic syntax of command is:
useradd [options] username
1. How to Add a New User in Linux
For example, to add a new user called ‘tecmint‘, use the following command.
[[email protected] ~]# useradd zetuser
When we add a new user in Linux with ‘useradd‘ command it gets created in locked state and to unlock that user account, we need to set a password for that account with ‘passwd‘ command.
[[email protected]~]# passwd zetuser Changing password for user zetuser. New UNIX password: Retype new UNIX password: passwd: all authentication tokens updated successfully.
Once a new user created, it’s entry automatically added to the ‘/etc/passwd‘ file. The file is used to store users information and the entry should be.
The above entry contains a set of seven colon-separated fields, each field has it’s own meaning. Let’s see what are these fields:
2. Create a User with Different Home Directory
[[email protected]~]# useradd -d /project/step1 newuser
You can see the user home directory and other user related information like user id, group id, shell and comments.
[[email protected] ~]# cat /etc/passwd | grep newuser newuser:x:505:505::/project/step1:/bin/bash
3. Create a User with Specific User ID
In Linux, every user has its own UID (Unique Identification Number). By default, whenever we create a new user accounts in Linux, it assigns userid 500, 501, 502 and so on…
But, we can create user’s with custom userid with ‘-u‘ option. For example, the following command will create a user ‘navin‘ with custom userid ‘999‘.
[[email protected] ~]# useradd -u 999 david
Now, let’s verify that the user created with a defined userid (999) using following command.
[[email protected] ~]# cat /etc/passwd | grep david david:x:999:999::/home/david:/bin/bash
4. Add a User without Home Directory
To create user’s without their home directories, ‘-M‘ is used. For example, the following command will create a user ‘marcel‘ without a home directory.
[[email protected] ~]# useradd -M marcel
Now, let’s verify that the user is created without home directory, using ls command.
[[email protected] ~]# ls -l /home/marcel ls: cannot access /home/marcel: No such file or directory
5. Create a User with Account Expiry Date
By default, when we add user’s with ‘useradd‘ command user account never get expires i.e their expiry date is set to 0 (means never expired).
However, we can set the expiry date using ‘-e‘ option, that sets date in YYYY-MM-DD format. This is helpful for creating temporary accounts for a specific period of time.
Here in this example, we create a user ‘laur‘ with account expiry date i.e. 30th October 2014 in YYYY-MM-DDformat.
[[email protected] ~]# useradd -e 2016-10-30 laur
Next, verify the age of account and password with ‘chage‘ command for user ‘laur‘ after setting account expiry date.
[[email protected] ~]# chage -l laur Last password change : Oct 21, 2016 Password expires : never Password inactive : never Account expires : Oct 30, 2016 Minimum number of days between password change : 0 Maximum number of days between password change : 99999 Number of days of warning before password expires : 7
6. Create a User with Password Expiry Date
The ‘-f‘ argument is used to define the number of days after a password expires. A value of 0 inactive the user account as soon as the password has expired. By default, the password expiry value set to -1 means never expire.
Here in this example, we will set a account password expiry date i.e. 20 days on a user ‘laurentiu’ using ‘-e‘ and ‘-f‘ options.
[[email protected] ~]# useradd -e 2016-10-30 -f 20 laurentiu