EP7: Users and Groups in RHEL

🎯 Introduction

In this tutorial, we’ll learn how to create users and groups in RHEL, a key topic in RHCSA (Red Hat Certified System Administrator) certification. You’ll understand not just the commands, but also the real-world purpose of users and groups β€” using simple examples.

Let’s get started!


πŸ‘€ Why Do We Create Users and Groups?

Imagine Linux as an office building:

  • Users are the employees β€” each person needs their own login and workspace.

  • Groups are departments β€” like β€œSales” or β€œIT”, which control shared access to resources.

So, in RHEL, users and groups help us manage:

  • Who can access what

  • Who owns which files

  • Who can run specific commands


πŸ§‘β€πŸ’» 1. Creating a User

βœ… Basic Command:

sudo useradd <username>

βœ… Example:

sudo useradd alice

This creates a new user named alice.

πŸ”Ž Behind the scenes: This command updates /etc/passwd, creates a home directory (if default settings apply), and assigns a UID.

πŸ”’ Set a Password:

sudo passwd alice

The system will prompt you to enter and confirm a password.


πŸ‘¨β€πŸ‘©β€πŸ‘§ 2. Creating a Group

βœ… Basic Command:

sudo groupadd <groupname>

βœ… Example:

sudo groupadd developers

This creates a new group named developers.


πŸ‘₯ 3. Add a User to a Group

There are two types of group membership:

  • Primary group (only one per user)

  • Secondary group(s) (can be multiple)

βœ… Change Primary Group:

sudo usermod -g <groupname> <username>

Example:

sudo usermod -g developers alice

Now, developers is the main group for alice.

βœ… Add to Secondary Group:

sudo usermod -aG <groupname> <username>

Example:

sudo usermod -aG wheel alice

Now alice is also a member of the wheel group (used for sudo/root access).

⚠️ Important: Always use the -a (append) option with -G, or you’ll overwrite all existing secondary groups.


πŸ“ 4. Create User with Group in One Command

sudo useradd -g developers -G wheel alice

This:

  • Sets developers as the primary group

  • Adds alice to the wheel group as secondary

Set password as usual:

sudo passwd alice

πŸ” 5. View User & Group Information

Check user's group membership:

groups alice

View user account info:

id alice

View group details:

getent group developers

🧹 6. Delete User or Group

Delete user (keep files):

sudo userdel alice

Delete user (and home directory):

sudo userdel -r alice

Delete group:

sudo groupdel developers

⚠️ Make sure no user is using the group before deleting.


βœ… Summary / Key Takeaways

Task

Command

Create user

sudo useradd <username>

Set password

sudo passwd <username>

Create group

sudo groupadd <groupname>

Set primary group

sudo usermod -g <group> <user>

Add to secondary group

sudo usermod -aG <group> <user>

View groups

groups <user>

Delete user

sudo userdel [-r] <user>

Delete group

sudo groupdel <group>


🎬 Final Thoughts

That’s it! You’ve just learned the essential commands and concepts for managing users and groups in RHEL β€” a skill you’ll use daily as a Linux sysadmin.

πŸ—‚ Think of users and groups as your access control system, helping you organize and secure your Linux environment.

Updated on