Understanding Users And Groups In Linux/Unix


Gaming Laptop Series

Linux/Unix are multi-user operating systems that means different users can access the system at a time and can use system resources simultaneously. To access the resources of a system you must have a user account on that system. Ahead in this article, we will see more about users and groups in a Linux or Unix system.

Users

A system can have multiple users. Broadly you can categorize them as normal users and system users. Normal users are humans who are accessing the system by logging into their respective accounts. While system users are used to starting non-interactive services in the background. All the information regarding the users including normal and system users is stored in /etc/passwd file. You can see the list of users in your terminal by using –

less /etc/passwd

Or

more /etc/passwd

How to Add/Remove Users?

To create users in a system adduser or useradd command is used. You need to have superuser privileges to add a user to your system. Execute the following command to add a user –

sudo adduser user_name

and then enter the required information it asks.

If you want to delete a user from your system then use –

sudo deluser user_name

Don’t forget to replace the user_name and group_name used in this article with the actual one. You can also read how to add/remove a user or grant the required permissions to the created user in Linux.

Groups

In Unix based system a group is a collection of users. A group makes it easier to define the permissions such as read, write, and execute for a system resource that is to be shared among the users of that group. You can set permission to ensure that files are accessible to users of a particular group like accounts, hr, marketing, etc.

Types of groups in Linux/Unix

There are two types of groups that a user can belong to –

Primary Group

A primary group is created whenever you add a new user. Usually, the name of a primary group is the same as the user and the user automatically becomes a member of this group. Each user must belong to exactly one primary group. These groups are also called login groups.

Secondary Group

A secondary group is used to grant certain privileges to a set of users. And a user could be a member of zero or more secondary groups. These groups are also known as supplementary groups.

Adding Group

You can add a new group with the groupadd command. Use the following command to create a group –

sudo groupadd group_name

The information about groups is stored in /etc/group file. To display the list of groups in your system you can use the following command –

less /etc/group

Or

more /etc/group

If you want to delete a group use the following command in your terminal –

sudo groupdel group_name

Adding a user to a group –

To add a user into a secondary group usermod command is used. Use the following command in your terminal to add a user into a group –

sudo usermod -a -G group_name user_name

If you want to add a user in multiple groups then use it as given below –

sudo usermod -a -G group_name, group_name1, group_name2 user_name

Removing a user from a group –

To remove a user from a group we will use gpasswd command with -d option. Now use the following command to remove a user from a group –

sudo gpasswd -d user_name group_name

Now I hope the information provided here is useful to you. If you have any questions regarding this topic you can write to us in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.