Linux User Management

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Linux User Management

User management includes everything from creating a user to deleting a user on your system. User management can be done in three ways on a Linux system.

Graphical tools are easy and suitable for new users, as it makes sure you'll not run into any trouble.

Command line tools includes commands like useradd, userdel, passwd, etc. These are mostly used by the server administrators.

Third and very rare tool is to edit the local configuration files directly using vi.

  1. /etc/passwd  

The local user database in Linux is /etc/passwd directory.

Linux Local User Management1

Look at the above snapshot, it has seven columns separated by a colon. Starting from the left columns denotes username, an x, user id, primary group id, a description, name of home directory and a login shell.


root

The root user is the superuser and have all the powers for creating a user, deleting a user and can even login with the other user's account. The root user always has userid 0.

Linux Local User Management2


useradd

With useradd commands you can add a user.

Syntax

useradd -m -d /home/<userName> -c "<userName><userName>

Example:

  1. useradd -m -d /home/xyz -c "xyz" xyz  

Linux Local User Management3

Look at the above snapshot, we have created a user xyz along with creating a home directory (-m), setting the name of home directory (-d), and a description (-c).

The 'xyz' received userid as 1004 and primary group id as 1004.

/etc/default/useradd

File /etc/default/useradd contains some user default options. The command useradd -D can be used to display this file.

Syntax:

useradd -D 

Linux Local User Management4


userdel

To delete a user account userdel command is used.

Syntax:

userdel -r <userName> 

Linux Local User Management5

Example:

  1. userdel -r xyz  

Look at the above snapshot, first we have shown the xyz user account with 'tail' command. To delete it, command "userdel -r xyz" is passed.

To recheck, again 'tail' command is passed and as you can see no xyz user account is displayed.

Hence, it is deleted.


usermod

The command usermod is used to modify the properties of an existing user.

Syntax:

usermod -c <'newName'> <oldName> 

Example:

usermod -c 'jhonny' john

Linux Local User Management6

Look at the above snapshot, user name john is replaced by the new user name jhonny


/etc/skel/

The /etc/skel/ contains some hidden files which have profile settings and default values for applications. Hence, it serves as a default home directory and user profile. While using useradd -m option, the /etc/skel/ is copied to the newly created directory.

Linux Local User Management7

Look at the above snapshot, files of /etc/skel/ is listed.


Deleting Home Directories

By using userdel -r option, you can delete home directory along with user account.

Syntax:

userdel -r <userName>

Example:

userdel -r john 

Linux Local User Management8

Look at the above snapshot, both home directory as well as user account john is deleted.


Login Shell

The /etc/passwd file also tells about the login shell for the user.

Linux Local User Management9

Look at the above snapshot, user guest will log in with /bin/bash shell and user jtp will log in with /bin/ksh shell.

You can change the shell mode with usermod command for a user.

Syntax:

usermod -s <newShell> <userName>

Example:

usermod -s /bin/bash jtp 

Linux Local User Management10

Look at the above snapshot, shell of jtp is changed to /bin/bash from /bin/ksh.


chsh

Users can change their login shell with chsh command.

Both the command chsh and chsh -s will work to change the shell.

Syntax:

chsh

Linux Local User Management11

Look at the above snapshot, command chsh has changed the sssit login shell from /bin/sh to /bin/bash.

Syntax:

chsh -s <newShell>

Example:

chsh -s /bin/sh 

Linux Local User Management12

Look at the above snapshot, login shell is changed into /bin/s.