How to Manage Users and Groups in Linux? The Comprehensive Guide

Managing users and groups in Linux involves creating, modifying, and deleting accounts and setting permissions to control access to system resources. Key tasks include using useradd to create users, groupadd to create groups, and commands like chmod, chown, and chgrp to manage file permissions. Administrators should follow best practices such as limiting root access, implementing strong password policies, and regularly auditing accounts to maintain system security and efficiency.

Jan 17, 2025 - 10:22
Updated: 2 days ago
102.1k
How to Manage Users and Groups in Linux? The Comprehensive Guide

Quick answer: In Linux, users are individual accounts with a unique UID, and groups collect users who share permissions. Create a user with useradd and set a password with passwd, change accounts with usermod and manage groups with groupadd. Use groups to give several users the same access instead of setting rights one by one.

Key takeaways

  • useradd creates a user, passwd sets a password, and usermod changes accounts.
  • Groups let several users share file access.
  • Use userdel -r carefully, since it also removes the home directory.

Managing users and groups in Linux is a fundamental task for system administrators to control access and maintain security. By effectively managing users and their permissions, you can ensure that resources are only accessible to authorized personnel. In this guide, we’ll explore the tools and commands required to manage users and groups in Linux.

Understanding Users and Groups in Linux

In Linux, users represent individual accounts that can access the system, while groups are collections of users that share common permissions.

  • Users:
    Each user has a unique identifier called a UID and is associated with specific files, processes, and permissions.

  • Groups:
    Groups help manage permissions for multiple users at once. A user can belong to one or more groups.

How to Manage Users in Linux

1. Create a New User

To add a new user, use the useradd command.

  • Example: sudo useradd username sudo passwd username # Set a password for the user

2. Modify User Accounts

You can modify user details like home directory or shell using the usermod command.

  • Change Home Directory:

    sudo usermod -d /new/home/dir username
  • Add a User to a Group:

    sudo usermod -aG groupname username

3. Delete a User

To remove a user and their home directory:

sudo userdel -r username 

How to Manage Groups in Linux

1. Create a New Group

Use the groupadd command to create a group.

  • Example: sudo groupadd groupname

2. Add Users to a Group

To add a user to an existing group:

sudo usermod -aG groupname username

3. Remove a User from a Group

Edit the group membership manually using the gpasswd command or modify the group file:

  • Example: sudo gpasswd -d username groupname

File Permissions with Users and Groups

File and directory permissions are defined using three attributes:

  • Owner (User): The individual user who owns the file.
  • Group: A group of users that can access the file.
  • Others: All other users.

Use the chmod, chown, and chgrp commands to manage permissions:

1. Change Ownership

To change the owner of a file:

sudo chown username filename

2. Change Group Ownership

To change the group associated with a file:

sudo chgrp groupname filename

3. Modify File Permissions

File permissions are represented as read (r), write (w), and execute (x). Use chmod to modify these permissions.

  • Example: chmod 755 filename # Sets permissions to rwxr-xr-x

Default User and Group Settings

1. /etc/passwd

The file /etc/passwd contains user account information such as usernames, UIDs, home directories, and default shells.

2. /etc/group

The file /etc/group lists all system groups and their members.

3. /etc/shadow

The /etc/shadow file stores encrypted user passwords and password expiration policies.

Advanced User and Group Management

1. Set Password Policies

Use chage to set password expiration rules for users.

  • Example: sudo chage -m 7 -M 90 -W 7 username
    • -m: Minimum days between password changes.
    • -M: Maximum days before a password must be changed.
    • -W: Days before expiration to warn the user.

2. Create Sudo Users

Grant administrative privileges to a user by adding them to the sudo group.

  • Example: sudo usermod -aG sudo username

Common Use Cases for Managing Users and Groups

  1. Shared Project Directories:
    Create a group for a project team, and assign directory permissions for collaborative work.

  2. Restricting Access:
    Use groups to control access to specific files or applications, limiting usage to authorized users.

  3. Administrative Roles:
    Assign sudo privileges to trusted users for system maintenance tasks.

  4. Automated User Management:
    Use scripts to add, modify, or remove users in bulk, especially in large environments.

Best Practices for Managing Users and Groups

  • Limit Root Access: Avoid using the root account directly; use sudo instead.
  • Implement Strong Password Policies: Enforce complex passwords and regular expiration.
  • Regularly Audit Accounts: Periodically review user and group memberships to remove inactive accounts.
  • Restrict Permissions: Apply the principle of least privilege, granting users only the permissions they need.

To take this further with guided labs and an instructor, see our Red Hat Certified System Administrator course.

Related reading

Reference

For the authoritative details, see Red Hat product documentation.

Frequently Asked Questions

Run sudo useradd username to create the account, then sudo passwd username to set a password. Add the -m option if you want a home directory created automatically.

A user is an individual account with a unique UID. A group is a collection of users that share permissions, so you can give the same access to many people at once.

Use sudo usermod -aG groupname username. The -a option appends the group instead of replacing the user's existing groups, which is a common mistake when the option is left out.

Basic user account details are stored in /etc/passwd, password hashes in /etc/shadow and group details in /etc/group. Use the standard commands instead of editing these files by hand.

What's Your Reaction?

Like Like 0
Dislike Dislike 0
Love Love 0
Funny Funny 0
Wow Wow 0
Sad Sad 0
Angry Angry 0
Vaishnavi

Vaishnavi is a skilled tech professional at the Ethical Hacking Training Institute in Pune, responsible for managing and optimizing the technical infrastructure that supports advanced cybersecurity education. With deep expertise in network security, backend operations, and system performance, she ensures that practical labs, online modules, and assessments run smoothly and securely. Her behind-the-scenes contributions play a vital role in delivering a seamless and secure learning experience for aspiring ethical hackers.