Tuesday, June 19, 2012

The Ultimate Guide to Create Users in Linux / Unix with Examples

Creating users in Linux or Unix system is a routine task for system administrators.

 Sometimes you may create a single user with default configuration, or create a single user with custom configuration, or create several users at same time using some bulk user creation method.

 In this article, let us review how to create Linux users in 4 different methods using useradd, adduser and newusers command with practical examples.

Method 1: Linux useradd Command — Create User With Default configurations

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

root@linuxhowto.in:~# useradd test-user

 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.

root@ linuxhowto.in:~# useradd -D








  • 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 test-user with group test-user. Use Linux passwd command to change the password for the user immediately after user creation.

root@linuxhowto.in:~# useradd test-user

root@linuxhowto.in:~# passwd  test-user

Changing password for user test-user

New UNIX password:

Retype new UNIX password:

passwd: all authentication tokens updated successfully.

# grep test-user /etc/passwd

test-user:x:500:500::/home/ test-user:/bin/bash

# grep test-user /etc/group


[Note: default useradd command created test-user as username and group]

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

# useradd -n pankaj

# grep pankaj /etc/passwd


# grep pankaj /etc/group

[Note: No rows returned, as group pankaj was not created]

# grep 100 /etc/group


[Note: useradd -n command created user pankaj with default group id 100]

# passwd pankaj

Changing password for user pankaj.

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.

# useradd -D --shell=<SHELLNAME>

# useradd -D







[Note: The default shell is /bin/bash]

# useradd -D -s /bin/ksh

# useradd -D







[Note: Now the default shell changed to /bin/ksh]

# adduser tanishka

# grep tanishka /etc/passwd


[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.

# 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: Crate Linux User with Custom Configurations Using useradd Command

The following example creates an account (Shourya) with home directory /home/expert, default shell as /bin/csh and with comment “Shourya Pal”.

# useradd -s /bin/csh -m -d /home/expert -c "Shourya Pal" -g root Shourya

# grep Shourya /etc/passwd

Shourya:x:513:0:Shourya Pal:/home/expert:/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.

# adduser USERNAME

Example 5: Creating an User Interactively With adduser Command

# adduser chetna

Adding user `chetna' ...

Adding new group `chetna' (1007) ...

Adding new user ` chetna ' (1007) with group `spidey' ...

Creating home directory `/home/ chetna ' ...

Copying files from `/etc/skel' ...

Enter new UNIX password:

Retype new UNIX password:

passwd: password updated successfully

Changing the user information for chetna

Enter the new value, or press ENTER for the default

Full Name []: Chetna Pankaj

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.


Example 6: Creating Large Number of Users Using newusers Command

If LinuxHowTo 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 linuxhowto-member.txt

sushil :fdfkdfd:1008:1000:Sushil Pal:/home/sushil:/bin/bash

pankaj :fdfdoff:1009:1000:Pankaj Pal:/home/pankaj:/bin/csh

ved :pfdkfovd:1010:1000:Vedprakash Nimbalkar:/home/ved:/bin/ksh

jayesh :ewhdbaf:1011:1000:Jayesh Mohan:/home/jayesh:/bin/sh

maggie:feajfdkg:1012:1000:Medhavi Sharma:/home/maggie:/bin/bash

Note: While specifying passwords for users, please follow the password best practices including the 8-4 password rule that we discussed a while back.

 Now create accounts for LinuxHowTo family together using the newusers command as shown below.

# newusers linuxhowto-member.txt

No comments:

Post a Comment