To add a user in Linux, you can use useradd command. Refer below syntax:
useradd [options]. To view the options use same command with the option -D.
Example: useradd anil
While creating users as mentioned above, all the default options will be taken except group id. To view, the default options give the following command with the option -D.
$ useradd -D
GROUP=1001
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/sh
SKEL=/etc/skel
CREATE_MAIL_SPOOL=no
GROUP | This is the only option which will not be taken as default. Because if you don’t specify -n option a group with same name as the user will be created and the user will be added to that group. To avoid that and to make the user as the member of the default group you need to give the option -n. |
HOME | This is the default path prefix for the home directory. Now the home directory will be created as /home/USERNAME. |
INACTIVE | -1 by default disables the feature of disabling the account once the user password has expired. To change this behavior you need to give a positive number which means if the password gets expired after the given number of days the user account will be disabled. |
EXPIRE | The date on which the user account will be disabled. |
SHELL | Users login shell. |
SKEL | Contents of the skel directory will be copied to the users home directory. |
CREATE_MAIL_SPOOL | According to the value creates or does not create the mail spool. |
To create a user in Linux with custom configurations
The below example creates an account “anil” with home directory /home/linuxbasics, default shell as /bin/ksh and with the comment “Linuxinanutshell admin”. $ useradd -s /bin/ksh -m -d /home/linuxbasics -c “Linuxinanutshell admin” -g root anil $ grep anil /etc/passwdanil:x:1083:0:Linuxinanutshell admin:/home/linuxbasics:/bin/ksh
To change default options in configuration file
As we mentioned earlier to change the default values during command execution, there is another option to change default values for account creation, which is defined in /etc/default/useradd file under most of the Linux distributions. Open this file in a text editor, ie.:$ vi /etc/default/useradd The default home directory defined by HOME variable, find line that read as follows:HOME=/home Replace with:HOME=/linuxbasics/user Save and close the file. Now you can add users using the regular useradd command:$ useradd anil
To change existing User’s Home Directory
You need to use the usermod command to set the user’s new login directory.
Syntax: usermod -m -d /path-to-new-home-dir userName
-d dirnanme : Path to new login (home) directory.
-m : The contents of the current home directory will be moved to the new home directory, which is created if it does not already exist.
Example: usermod -m -d /users/linuxinanutshell/anil anil
In the above example set the user’s new login directory to /users/linuxinanutshell/anil from /home/anil
To set Password for user
$ passwd anilChanging password for user anil.New UNIX password:Retype new UNIX password:passwd: all authentication tokens updated successfully. [Note: Always set the password immediately after user creation]