user add Linux

Subject:
http://www.thegeekstuff.com/2009/06/useradd-adduser-newuser-how-to-create-linux-users/
2014-05-19 22:41:44gstlouis

This is a fundamental low level tool for user creation. To create user with default configurations use useradd as shown below.

Syntax: # useradd LOGIN-NAME

 
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.

Example 1: Creating user with all the default options, and with his own group.

Following example creates user ramesh with group ramesh. Use Linux passwd command to change the password for the user immediately after user creation.

# useradd ramesh # passwd ramesh Changing password for user ramesh. New UNIX password: Retype new UNIX password: passwd: all authentication tokens updated successfully. # grep ramesh /etc/passwd ramesh:x:500:500::/home/ramesh:/bin/bash # grep ramesh /etc/group ramesh:x:500: [Note: default useradd command created ramesh as username and group]

Example 2: Creating an user with all the default options, and with the default group.

# useradd -n sathiya # grep sathiya /etc/passwd sathiya:x:511:100::/home/sathiya:/bin/bash # grep sathiya /etc/group [Note: No rows returned, as group sathiya was not created] # grep 100 /etc/group users:x:100: [Note: useradd -n command created user sathiya with default group id 100] # passwd sathiya Changing password for user sathiya. New UNIX password: Retype new UNIX password: passwd: all authentication tokens updated successfully. [Note: Always set the password immediately after user creation]

Example 3: Editing the default options used by useradd.

The following example shows how to change the default shell from /bin/bash to /bin/ksh during user creation.

Syntax: # useradd -D --shell=<SHELLNAME> # useradd -D GROUP=100 HOME=/home INACTIVE=-1 EXPIRE= SHELL=/bin/bash SKEL=/etc/skel [Note: The default shell is /bin/bash] # useradd -D -s /bin/ksh # useradd -D GROUP=100 HOME=/home INACTIVE=-1 EXPIRE= SHELL=/bin/ksh SKEL=/etc/skel [Note: Now the default shell changed to /bin/ksh] # adduser priya # grep priya /etc/passwd priya:x:512:512::/home/priya:/bin/ksh [Note: New users are getting created with /bin/ksh] # useradd -D -s /bin/bash [Note: Set it back to /bin/bash, as the above is only for testing purpose]

Method 2: Linux useradd Command — Create Users With Custom Configurations

Instead of accepting the default values (for example, group, shell etc.) that is given by the useradd command as shown in the above method, you can specify custom values in the command line as parameters to the useradd command.

Syntax: # useradd -s <SHELL> -m -d <HomeDir> -g <Group> UserName

 

  • -s SHELL : Login shell for the user.
  • -m : Create user’s home directory if it does not exist.
  • -d HomeDir : Home directory of the user.
  • -g Group : Group name or number of the user.
  • UserName : Login id of the user.

Example 4: Create Linux User with Custom Configurations Using useradd Command

The following example creates an account (lebron) with home directory /home/king, default shell as /bin/csh and with comment “LeBron James”.

# useradd -s /bin/csh -m -d /home/king -c "LeBron James" -g root lebron # grep lebron /etc/passwd lebron:x:513:0:LeBron James:/home/king:/bin/csh

 
Note: You can give the password using -p option, which should be encrypted password. Or you can use the passwd command to change the password of the user.

Method 3: Linux adduser Command – Create Users Interactively

These are the friendlier tools to the low level useradd. By default it chooses the Debian policy format for UID and GID. A very simple way of creating user in the command line interactively is using adduser command.

Syntax: # adduser USERNAME

Example 5: Creating an User Interactively With adduser Command

# adduser spidey Adding user `spidey' ... Adding new group `spidey' (1007) ... Adding new user `spidey' (1007) with group `spidey' ... Creating home directory `/home/spidey' ... Copying files from `/etc/skel' ... Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully Changing the user information for spidey Enter the new value, or press ENTER for the default Full Name []: Peter Parker Room Number []: Work Phone []: Home Phone []: Other []: Is the information correct? [y/N] y

Method 4: Linux newusers Command — Creating bulk users

Sometimes you may want to to create multiple users at the same time. Using any one of the above 3 methods for bulk user creation can be very tedious and time consuming. Fortunately, Linux offers a way to upload users using newusers command. This can also be executed in batch mode as it cannot ask any input.

# newusers FILENAME

 
This file format is same as the password file.

loginname:password:uid:gid:comment:home_dir:shell

Example 6: Creating Large Number of Users Using newusers Command

If Simpson family decides to join your organization and need access to your Linux server, you can create account for all of them together using newusers command as shown below.

# cat homer-family.txt homer:HcZ600a9:1008:1000:Homer Simpson:/home/homer:/bin/bash marge:1enz733N:1009:1000:Marge Simpson:/home/marge:/bin/csh bart:1y5eJr8K:1010:1000:Bart Simpson:/home/bart:/bin/ksh lisa:VGz638i9:1011:1000:Lisa Simpson:/home/lisa:/bin/sh maggie:5lj3YGQo:1012:1000:Maggie Simpson:/home/maggie:/bin/bash

gstlouis
vote
2015-01-29 16:42:56

Method #1: getent command to lookup username and group name

The syntax is as follows to find out if user named foo exists in system:

getent passwd userNameHere getent passwd foo

gstlouis
vote
2016-07-09 15:51:05

if you are able to see ! in the second field starting that indicates that password is disabled, you have to enable it back by using passwd with -u option

passwd -u username

Example:

passwd -u surendra

Unlocking password for user temp.

passwd: Success

Example2: Check if the user expiry date is reached or not by using chage command

chage -l username

Example

chage -l surendra
Last password change : Jan 05, 2012
Password expires : never
Password inactive : never
Account expires : Jan 01, 2012
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

If you see that the account expires use usermod or chage command to extend the user expiry time.

usermod -e yyyy-mm-dd username

usermod -e 2012-05-10 surendra

or

chage -E yyyy-mm-dd username

chage -E 2012-05-10 surendra

this will extend user expiry time to 5 more months.

Example3: Check if the user shell is set to a valid shell or not, if it’s not set it to a valid one.

grep ‘username’ /etc/passwd

Example:

grep ‘surendra’ /etc/passwd

If the user shell in seventh feild is set to /sbin/nologin or /bin/false set it back to /bin/bash or /bin/ksh

usermod -s /bin/bash usrename

usermod -s /bin/bash surendra

Share your thoughts on this and let us know if you have other ideas to unlock user accounts in Linux.

gstlouis
vote
2016-07-11 08:52:36