Mastering User and Group Management in Linux: A Comprehensive Guide

Mastering User and Group Management in Linux: A Comprehensive Guide

Welcome to our in-depth guide on user and group management in Linux! Whether you're an experienced sysadmin or just starting with Linux administration, knowing how to manage users and groups effectively is essential. Let’s explore the key aspects of account management, monitoring users, and some useful commands that will streamline your workflow.

Account Management Basics

Important Files

Before we get into the nitty-gritty, let’s understand the crucial files that hold user information:

  • /etc/passwd: Contains user account details. Format: username:x:uid:gid:comment:home_directory:login_shell.

  • /etc/shadow: Stores users' hashed passwords.

  • /etc/group: Lists all groups and their members.

Creating a User Account

useradd [OPTIONS] username

Options:

  • -m: Create home directory.

  • -d directory: Specify a custom home directory.

  • -c "comment": Add a comment to the user.

  • -s shell: Specify the login shell.

  • -G: Add the user to secondary groups.

  • -g: Set the primary group.

Example:

useradd -m -d /home/vishnu -c "DevOps Engineer" -s /bin/bash -G sudo,adm,mail vishnu

Changing a User Account

To modify an existing user, use usermod with similar options as useradd:

usermod [OPTIONS] username

Example:

usermod -aG devops,managers vishnu

This command adds the user to the developers and managers groups without affecting other group memberships.

Deleting a User Account

To remove a user along with their home directory, use:

userdel -r username

Group Management

Creating a Group

Create a new group with groupadd:

groupadd group_name

Deleting a Group

Remove a group with groupdel:

groupdel group_name

Viewing Groups

  • Display all groups:

      cat /etc/group
    
  • Display groups a user belongs to:

      groups
    

Admin Users

To grant sudo privileges in Ubuntu or wheel group access in CentOS, add the user to the appropriate group:

usermod -aG sudo vishnu

Monitoring Users

Monitoring user activity is crucial for maintaining system security and performance, and here are some essential commands.

  • List logged-in users:

      who -H
    
  • Display current user and their groups:

      id
    
  • Show the current user’s identity:

      whoami
    
  • List logged-in users and their processes:

      w
    
  • Check system uptime:

      uptime
    
  • View login/logout history:

      last
    
  • Display a specific user’s login history:

      last -u username
    

Conclusion

Mastering user and group management in Linux is essential for any sysadmin. Whether you’re creating, modifying, or deleting user accounts, or monitoring user activity, these commands will help you keep your system secure and efficient. Happy managing!

Feel free to share your thoughts or ask any questions in the comments below. Happy managing!