The Ultimate Guide to Create Users in Linux / Unix

by Ramesh Natarajan on June 24, 2009

Tutorial Guide For User Creation in LinuxCreating 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.

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: Crate 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

 
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 Simpsons family together using the newusers command as shown below.

# newusers homer-family.txt

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 26 comments… read them below or add one }

1 Renich June 24, 2009 at 12:11 pm

Nice article! But, please, include some security on the newusers section! Tell them how to figure out which encryption they’re using and how to generate those passwords

marge:marge is not near secure!

2 uzisuicide June 24, 2009 at 1:32 pm

cool

3 Tapas Mallick June 24, 2009 at 11:24 pm

### Encrypt password to use in “useradd -p ” ###
# openssl passwd -crypt

# useradd -p

### On RHEL based system, it is found that /usr/sbin/adduser is actually a Sym link of useradd and not working as demonstrated in tutorial ###

### newusers command is creating bulk user but not copying SHELL initialization files from /etc/skel directory to user’s home in my RHEL 4 AS system###

4 Tapas Mallick June 24, 2009 at 11:29 pm

In my previous post, I found few words are truncated/shreded in the webpage, posting once again :

## Encrypt password to use in “useradd -p ” ###

# openssl passwd -crypt

# useradd -p

5 Tapas Mallick June 24, 2009 at 11:32 pm

Hi Ramesh,

is there any blacklisted word policy in this blog ? I found few words are shred in the webpage.

6 hans berger June 25, 2009 at 3:43 am

Sry, but isn’t the 1009 of bart in conflict with the uid of marge?
marge:marge:1009:1000:Marge Simpson:/home/marge:/bin/csh
bart:bart:1009:1000:Bart Simpson:/home/bart:/bin/ksh

7 vinodh June 25, 2009 at 4:37 am

hai,what are all the ways to encrypt and decrypt a file using gpg,command,could u plz define the step by step procedure in redhat…

8 Daniel Reimann June 25, 2009 at 6:11 am

Thanks for reviewing this important tool with us.

9 Flynets June 25, 2009 at 6:56 am

hi, a few years later i write a little shell script for create many users with same configuration using useradd in 2 variants:

for debian-like: http://snippets.dzone.com/posts/show/4980
for red-hat-like: http://snippets.dzone.com/posts/show/4979

the only differerence is the command (gpw/passwdgen) for generate random password.
it take 2 arguments:
1- name of file that contains usernames (it’s better one per line)
2- name of the log where will be write the couple username-password.

instead for blocking the users you ca use http://snippets.dzone.com/posts/show/5191

bye

10 Ramesh Natarajan June 25, 2009 at 10:34 pm

@Renich,
I agree that “marge” is not a secure password. I’ve updated the article with reference to the article I wrote earlier about password best practices. I’ve also updated the homer-family.txt file with relatively stronger password. Thanks for your feedback.

 
@Tapas,
The comment field is not supposed to truncate anything automatically. Can you please do one of the following and I’ll research about this further:

- Reply to the email I sent you with the words that you typed in the comment section that got wiped out.
- Try to enter the comment again. But this time, make sure you put those between <pre></pre>

 
@Hans,

That is a copy/paste mistake. Thanks for pointing it out. I’ve corrected it.

 
@Vinodh,

On a very high level, following is the command. I’ll try to write a detailed post later on this topic.

Encrypt: gpg --encrypt Recipient [Data]

Decrypt: gpg [--decrypt] [Data]

 
@Daniel,
Thanks for your comment. I’m glad you found this article very helpful.

 
@Flynets,
Thanks for sharing the scripts your wrote to add many users. It looks great.

11 S.RAGHU June 25, 2009 at 11:42 pm

As reported in Rhel 4 – adduser & useradd are same. Similarly /etc/skel files are not copied . How to overcome this.

12 Otto Teixeira August 21, 2009 at 7:23 am

Is is possible to create something like a symlink for a user? Like to be able to reference a user my more than a name? Example: ‘otto’ and ‘xpto’ are the same user.

Thanks.

13 Aliza November 17, 2009 at 12:45 pm

Otto: You can either set an email alias (mail to “otto” goes to “xpto”) using the methods for your MTA, which will just send mail sent to one account to the other, or you can create two accounts with the same UID and home directory.

14 Liberty January 17, 2010 at 5:18 am

Here’s a command I use all the time
useradd -d /home/kevin -s /bin/bash -m kevin

This adds a user ‘kevin’, makes the home directory and sets the default shell to bash.

I can’t ever remember the syntax, so here’s a link -> https://www.aplacetocode.com/tutorials/first-time-server-tutorial/

15 Big May 5, 2010 at 3:18 am

If I’m requested to create 1000 users on Linux server, what would be the easiest way to do it?

16 amit May 8, 2010 at 11:23 pm

how to get home directory by using newusers command

17 rishabh August 11, 2010 at 9:33 am

Hi
can someone please tell me what file are get copied while creating a user through useradd command which are not copied with newusers command,and how to overcome this issue.
my concern is with rhel 5

thanks
rishabh

18 Matthew September 12, 2010 at 2:08 pm

This is great! I used this on Ubuntu10.04.
The only problem I’m finding is that users I add from the CLI can’t ssh to the box, but users add via the gui can.
What am I missing?! I have googled like crazy but nothing is obvious.
any ideas anybody?

19 sid April 13, 2012 at 1:17 am

@rishabh

/etc/skel gets copied and this is not copied when using newusers command – well its important to have these files copied to the users home directory when users are created as …If you don’t want the skel directory to be copied with the useradd command as well you can use the following command

#useradd -m –skel SKEL_DIR
–skel SKEL_DIR is used in conjuntion with -m

20 Hem Kumar September 3, 2012 at 6:32 am

Hi All,
can anybody will tell me when we create a user in RHEL5 how many files it create at the time of user creation.

Regards
Hem

21 Darshan February 12, 2013 at 7:11 am

Hello Ramesh,

Before using command useradd -D -e /2014/01/01 output of useradd -D was

[root@localhost ~]# useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=no

after executing above command useradd -D -e /2014/01/01 output is

[root@localhost ~]# useradd -D
GROUP=100
HOME=/home
INACTIVE=-1
EXPIRE=2014/01/01
SHELL=/bin/bash
SKEL=/etc/skel
CREATE_MAIL_SPOOL=no
[root@localhost ~]#

How can i set useradd defaults so that i would get 1st output. Please suggest

Many Thanks
Darshan

22 prescilla February 24, 2013 at 10:02 am

useradd without options does not create a home directory in Ubuntu.

23 Prash May 13, 2013 at 4:29 am

Hi,
When creating user in Linux, certain commands are not working, eg: ifconfig.
when this is executed, the result is
-bash: ifconfig: command not found.
How to add this commandes to the user

24 Kingpin June 25, 2013 at 1:00 pm

@Prash
I’m guessing you are creating a standard account.
If so then a standard user cannot use the commands located in the /sbin/ directory.
You must add that user to the sudoers file.
Then you can execute sudo ifconfig
(enter password)
then receive output.

25 Gurdeep November 18, 2013 at 1:36 am

hai sir, I m beginner in linux, i followed you given steps to create a new user. I facing problem in creating user with password. I m using RHEL 6. I used following command.

[root@locahost ~]# useradd -p 123456 user1

This command creates the user but not providing the same password. When i m trying to login user1 with password 123456 it is allowed me to login.

i have another question. Will you please tell me what is the difference between useradd and adduser ?

I m waiting for your response. Thanks

26 Ganesh Prabu December 18, 2013 at 9:50 pm

Hi,
I am a great follower of your site.I always wanted to ask this one question “Neenga linuxla eluthatha topicae illaya??” :D :D
(I hope you have never left a topic in linux)

Regards,
Ganesh Prabu Ravi
III CSE
SASTRA

Leave a Comment

Previous post:

Next post: