Linux User and Group Management


User and Group Management in Linux involves creating, managing, and assigning permissions to users and groups. This process is essential for controlling access to resources and ensuring a secure environment. Here’s a breakdown:

1. Users in Linux

In Linux, a "user" represents an individual who can log into the system and interact with files and applications. Each user has a unique User ID (UID) and a home directory where personal files are stored.

Types of Users

  • Root User: The superuser or administrator with unrestricted access. Has a UID of 0.
  • System Users: Used by system processes (e.g., www-data for Apache).
  • Regular Users: Created for individual people who access the system.

User Management Commands

  • Create a user: sudo useradd <username>
  • Delete a user: sudo userdel <username>
  • Change a user's password: sudo passwd <username>
  • Modify a user: sudo usermod <options> <username>

2. Groups in Linux

Groups allow users to share permissions and access to files or resources collectively. Each user can belong to multiple groups, but one primary group.

Types of Groups

  • Primary Group: Assigned to a user upon creation and owns the user's files by default.
  • Secondary Group: Additional groups that a user can belong to for shared permissions.

Group Management Commands

  • Create a group: sudo groupadd <groupname>
  • Delete a group: sudo groupdel <groupname>
  • Add a user to a group: sudo usermod -aG <groupname> <username>
  • List groups: cat /etc/group

3. File Permissions and Ownership

File permissions are set for the owner (user), group, and others. Permissions include:

  • Read (r): Allows viewing file contents.
  • Write (w): Allows modifying file contents.
  • Execute (x): Allows executing a file (if it’s a script/program).

Changing File Permissions

  • Change ownership: sudo chown <user>:<group> <filename>
  • Change permissions: chmod <mode> <filename>

4. Important Files

  • /etc/passwd: Contains user account information.
  • /etc/shadow: Stores encrypted passwords and related data.
  • /etc/group: Lists all system groups.

5. Practical Examples

  • Create a user with a home directory.
    sudo useradd -m -s /bin/bash <username>
  • Create a new group and add a user to it:
    sudo groupadd developers sudo usermod -aG developers <username>

By managing users and groups effectively, Linux administrators can control access to resources, implement security policies, and ensure that users have the appropriate permissions for their tasks.