Managing Users in Linux

Since linux is open source, we know everything about user management in linux. We even know the files where the necessary information about the user is stored. Managing users involves in from creating, deleting to updating access to every corners to the system. We’ll discuss the most important of them here.

You can manage users in three ways:

  1. The Graphical tools are easy and suitable for new users. As it will make sure, you won’t break anything.
  2. Linux Command line tools includes commands like useradd, userdel, passwd, etc. These are mostly used by the server administrators.
  3. You can directly edit the relevant files, but editing the files directly is not recommended.

The /etc/passwd file

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

To view the contents run

$ cat /etc/passwd

Each line of this file contains seven columns separated by a colon. Meaning of the values, starting from the left columns:

  • username
  • an x
  • user id
  • primary group id
  • a description
  • home directory for the user
  • login shell for the user

useradd command

The useradd commands allows you too add users.

Syntax of useradd command:

$ sudo useradd -m -d <directory> -c "<comment>" <username>  

The -m option is for “create the user’s home directory is no exist. -d option signifies the next argument is the home directory of the user, and -c options signifies a comment/description for the user. And the last argument is the username. See the manual of useradd for more options.

For Example:

$ sudo useradd -m -d /home/sufi -c "New User" sufi

Here, the username is sufi, home directory is /home/sufi, the comment is “New User”, and -m option to automatically create the home directory for the user.

An userId is automatically assigned to the new user, and a new group is automatically created and assigned to it.

The /etc/default/useradd file

Linux file /etc/default/useradd contains default options for useradd and the command useradd -D can be used to display the defaults from the file.

Syntax of useradd -D :

$ useradd -D  

userdel command

To delete an user account userdel command is used.

Syntax of userdel command :

$ sudo userdel -r <userName>  

The -r option is used to clean and delete the user’s home directory.

Example of userdel command :

$ sudo userdel -r sufi

If you see the /etc/passwd file, you’ll see that, the row for the sufi user is gone.

If you want to delete only the user, but don’t his files, then you could do:

$sudo userdel sufi

usermod command

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

Syntax of usermod command :

$ sudo usermod <options> <oldName>  

The usermod command tool has almost same options like the useradd command. But, usermod doesn’t create the user, instead with those options it updates the existing user. For example: If we want to change the comment/description of an user, we can do:

$ sudo usermod -c 'Michelm' michel  

Here, the commend/description of user michel gets update. If we wanted to change the home directory, we could user the -d option. See the manual of usermod for complete list of options.

The /etc/skel/ file

The /etc/skel/ file contains some hidden files which have profile settings and default values for applications.

The word skel comes from skeleton. Because, it contains the basic structure of home directory.

This serves as a template for new home directories. While using useradd -m option, the contents of /etc/skel/ is copied to the newly create home directory.

Changing the Shells for Users:

Unix or Linux /etc/passwd file also tells about the shells for users.

The last column in each row signifies the shell for each user.

To change the shell, we can user the usermod command with the option -s. Syntax:

$ sudo usermod -s <new-shell> <username>  

For example:

$ sudo usermod -s /bin/bash sufian

In this example, we’ve updated the login shell for user sufian, to /bin/bash

The chsh command

With chsh command users can change their login shell.

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

Syntax of chsh command :

$ chsh

Syntax of chsh -s command :

$ chsh -s <newShell>  

Example of chsh -s command :

$ chsh -s /bin/sh

If you want an interactive console, use only chsh, the tool will ask to enter your new shell. If you don’t want any interaction, use chsh -s <new-shell>, of course it will ask for your password, to update.

Leave a Reply