Sunday, 22 March 2015

How to add user in linux

To add user, you can use useradd command in linux. Refer below syntax:

useradd [options] <user-name>

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
GROUPThis 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.
HOMEThis 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.
EXPIREThe date on which the user account will be disabled.
SHELLUsers login shell.
SKELContents of the skel directory will be copied to the users home directory.
CREATE_MAIL_SPOOLAccording to the value creates or does not create the mail spool.

To create user with custom configurations using useradd Command:
The below example creates an account "anil" with home directory /home/linuxbasics, default shell as /bin/ksh and with comment “Linuxinanutshell admin”.

$ useradd -s /bin/ksh -m -d /home/linuxbasics -c “Linuxinanutshell admin” -g root anil

$ grep anil /etc/passwd
anil: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 user using 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 above example set the user's new login directory to /users/linuxinanutshell/anil from /home/anil

To set Password for user:
$ passwd anil
Changing 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]

1 comment: