Preface
Introduction
Practice Setup
Linux Architecture
Basic Commands
Basic file management or operations on file
User administration
Networking
Linux Interview Questions

 

 

Create a user in linux - useradd command:

Create-Modify-Delete-users-in-Linux-Unix

One of the day to day activity for any Linux/UNIX admin is to create new user and modify their attributes as per the end user requirements.

 

Below is the list of Linux commands that we use for user administration.

useradd ---- For adding a new user
usermod---- For modifying user attributes
userdel------ For removing user from Linux machine.


We focus here on useradd command with examples for adding a user in linux.

Note: 

For add/modify/remove users, we should have root level privileges or someone having sudo root user credentials.

 A non-root user, should raise request or ticket following ITIL standards, for any changes in user credentials on the machine.

 

Create a new user:

In general, Non root users won't have credentails to create a new user. Instead they have to request the Linux team following ITIL standards to create a new user.

 

Command Syntax:

Below given are some of the most commonly used options with useradd commands. for more options, please refer to man pages or info of command.

#useradd u -g -G -c -d <user’s working directory> -m –s

 

Options:

g--------Primary group
G------ Secondary group
C------- Comment at type of user
m------make directory
s--------Shell
d--------Path of user’s home directory.

 

Note: 

u -----user id’s in RHEL 6 0- 499 are reserved
                                                   0-99 are system users
                                                   100-499 are daemon users

500-2147483647 are minimum user id and maximum user ids available to be assigned by linux administrators.

 

For example,

Create a new user named "john" for database admin team, assigning Bash shell.

Before creating any new user, we should gather few details from end user,

Userid =501
Primary group = dba
Secondry group = sales
Comment = system admin
Directory = /home/john
Shell=/bin/bash
Username = john

 

Syntax

[root@sys2 ~]# useradd -u 501 -g dba -G sales -c systemadmin -d /home/john -m -s /bin/bash john

 

Note: 

  • username & user id both are not same. username is generic name for human understanding, whereas userid is unique number assigned to each user by OS for its reference.
  • In Linux, we can create usernames with capital letters also, but best practice is to follow lower cases.

 

To check whether a user is created or not, we can use any one of the below ways,

  • Using "id" command,
[root@sys1 ~]# id john
uid=501(john) gid=501(john) groups=501(john)

 

  • Checking from /etc/passwd file,
[root@sys2 ~]# grep john /etc/passwd
john:x:501:502:systemadmin:/home/john:/bin/bash

 

Note: 

  • usernames are case sensitive in Linux and UNIX Flavors. Type, id JOHN and see the output.
  • If the group doesn’t exist already, then they are created by default with the user name.

 

Default attributes for new user:


The default attributes for a normal user are user id and group id given by the system.

  • Default working directory is ‘/home/
  • The default shell is bash shell.

For example,

Creating a user in Linux machine with default attributes.

 

The syntax is,  #useradd <sampleusername>

[root@sys2 ~]# useradd steve

 

Checking the details in /etc/passwd file,

[root@sys2 ~]# grep steve /etc/passwd
[root@sys2 ~]# grep steve /etc/passwd
steve:x:503:503::/home/steve:/bin/bash

 

Topic review through questions: