Skip to content

Users and Groups

Users

Linux is a multi-user system. Becauses of this, each person who uses Linux should have their own account in order to implement permission management and access control.

There are two types of users:

  • Regular Users - Created by the administrator to represent each person who uses the system.
  • System Users - Created by the system and applications to run services.

Groups

Linux has user groups which are used to group different users. The permissions and access control rules can be set at group level instead of at user level to simplify permission management in Linux.

There are two types of groups:

  • Primary Groups - When a user creates a file, the file’s group is set to the user’s primary group. Usually, the name of the group is the same as the name of the user.
  • Secondary or supplementary group - Useful when administrator wants to grant certain file permissions to a set of users who are members of the group.

Common Tasks

Creating a New User

To create a new user called john:

# useradd john

Info

When useradd is not specified with any options, it will infer default settings from files like: /etc/default/useradd and /etc/login.defs

By default, useradd will set the primary group of the newly created user to be a group with same name as the user. We can use id to check details of the user.

# id john
uid=1000(john) gid=1000(john) groups=1000(john)

Create a New User with its Home Directories

useradd has a -m flag which can be used to create new user's home directory with files and directories contained in the skeleton directory.

# useradd -m john
# ls -l /home/
total 4
drwxr-x--- 2 john john 4096 Sep 19 20:35 john

Create a New User with a Specific Shell

useradd has a -s flag which can be used to specify the name of the user's login shell. The default is to leave this field blank, which causes the system to select the default login shell specified by the SHELL variable in /etc/default/useradd.

# useradd -s /bin/sh john
# su - john
$ echo $SHELL
/bin/sh

Create a New User with Specific Supplementary Groups

useradd has a -G flag which can be used to specify the name of the supplementary groups seprated by ,.

# useradd -G sudo,video john
# id john
uid=1000(john) gid=1000(john) groups=1000(john),27(sudo),44(video)

Delete a User

# userdel john

userdel has a -r flag to remove files in the user's home directory along with the home directory itself and the user's mail spool.

# userdel -r john

Add the User to the Supplementary Groups

-aG means append supplementary groups

# usermod -aG sudo john

Remove a User From a Group

Assume username is john and the group to be removed is sudo

# gpasswd -d john sudo

Creating a New Group

To create a new group called gamers:

# groupadd gamers

Delete a New Group

# groupdel gamers

Reference