To Add a new user account in Linux system use the command ‘useradd‘ and only root user or sudo user had the permission to add a new user account. It is very simple to add a user account in linux system and so many useful optins are available their in useradd command. Follow the below tips which are useful to add a new user account with different options.
useradd [option] user-name
Some Useful Options
-c — comment about the user information
#useradd -c “This is a test user” testuser
Here the user name is mentioned as testuser and the options -c is used to mention the user information that will be added in passwd file.
-D — print the default useradd configuration
This -D option shows the default useradd configuration options that stored in /etc/default/useradd file.
-d — specify the home path on adding a user
#useradd -d /testuser testuser
Here -d option over ride the default home path creation specified in configuration file and place the user home directory in new specified path /testuser .
-e — Specify the Expiry date of the user account while creating, the date specified in the format YYYY-MM-DD
#useradd -e 2013-04-26 testuser
In this example April 4, 2012 is given as the exipry date of the New user and this information is added in shadow file entry.
-f — Mention the Account Inactive date after the password expiry at the time of user creation
#useradd -f 2 testuser
Here 2 is the number of days to inactive the account after password expired and this information is updated in shadow file entry.
-g — specifying primary group while creating user
#useradd -g mygrp testuser
Add the user with a default primary group, the group must be a existing group.
-G — Add the New user to the existing group
#useradd -G mygrp testuser
At the time of user creation add the new user to the existing group, we can also mention more than one group by comma sepration. This group information is added in /etc/group file.
-l — Do not add the user to the lastlog and faillog databases.
#useradd -l testuser
This -l option used to skip the user from lastlog (/var/log/lastlog) and faillog (/var/log/faillog) database file.
-M — To avoid the creation of user home directory
# useradd -M testuser
The option -M is used to avoid the creation of home directory for the new user account.
-s — Assign the Shell while creating user
#useradd -s /bin/ksh testuser
To change the user login shell from default configuration to another one -s option is used.
-u — Force to use the defined user ID
#useradd -u 520 testuser
While adding the new user account -u option force to use the defined user id, here 520 is the defined user id of test-user.