Familiarization of basic Linux Commands-useradd, userdel , groupadd, groupdel, passwd

 useradd- adding new users

The useradd command in Linux is used to create new user accounts. It is a low-level utility that provides options for configuring user properties such as home directories, shell access, user groups, and more.

Syntax

useradd [OPTIONS] USERNAME

  • USERNAME: The name of the new user to be created.
  • OPTIONS: Parameters to customize the user account.

Features of useradd

  1. Creates a New User
    It adds the user information to the system's user account files, such as /etc/passwd, /etc/shadow, and /etc/group.

  2. Configures User Environment
    By default, it sets up a home directory, user ID (UID), group ID (GID), and a default shell.

Options

OptionDescription
-c "COMMENT"Sets a description or comment for the user, typically the full name.
-d HOME_DIRSpecifies the home directory for the user. If not provided, it defaults to /home/USERNAME.
-e EXPIRE_DATESets an expiration date for the user account (in YYYY-MM-DD format).
-f INACTIVE_DAYSDefines the number of days after the password expires before the account is disabled.
-g GROUPAssigns a primary group for the user.
-G GROUP1,GROUP2Adds the user to additional (supplementary) groups.
-mCreates the user's home directory if it doesn’t already exist.
-MPrevents the creation of the home directory.
-p PASSWORDSets an encrypted password for the user. (Not recommended for security reasons—use passwd instead.)
-s SHELLSpecifies the login shell for the user. Default is /bin/bash (depending on the system configuration).
-u UIDSets a specific user ID (UID) for the account.
-rCreates a system account instead of a regular user account.

How to Use useradd 

1. Add a User with Default Settings
    sudo useradd alice

This creates:

  • A user alice.
  • A home directory /home/alice.
  • A unique UID.
  • A default shell (e.g., /bin/bash).
2. Set a Comment for the User
    sudo useradd -c "Alice Johnson" alice
    The comment (user description) is stored in /etc/passwd.

3. Specify a Custom Home Directory
    sudo useradd -d /data/alice alice

4. Assign a Specific UID
    sudo useradd -u 1001 alice

5. Set an Expiration Date
    sudo useradd -e 2025-12-31 alice

6. Add the User to Groups
    sudo useradd -G sudo,developers alice
    This adds alice to the sudo and developers groups.

7. Specify a Login Shell
    sudo useradd -s /bin/zsh alice
    The default shell for alice is set to /bin/zsh.

8. Create a System Account
    sudo useradd -r system_user
This creates a system account with no shell and no home directory by default.

passwd -User Password Setup

The useradd command itself does not automatically set a password for the user. To set a password, use the passwd command:

sudo passwd alice

You will be prompted to enter a password for alice.

Files Modified by useradd

  1. /etc/passwd
    Stores user account details such as username, UID, GID, home directory, and shell.

  2. /etc/shadow
    Contains encrypted passwords and password-related settings.

  3. /etc/group
    Defines group memberships.

Default Configuration for New Users

The default settings for new users are stored in /etc/default/useradd and /etc/skel/:

  1. /etc/default/useradd
    Configures default values such as shell, home directory, and account expiration.

  2. /etc/skel/
    Contains default files copied to the user's home directory (e.g., .bashrc, .profile).

Viewing User Details

You can verify the details of a created user:

    grep alice /etc/passwd

Best Practices

  1. Set Secure Passwords
    Use passwd to set user passwords securely.

  2. Use -m Option
    Ensure home directories are created by default using -m.

  3. Avoid Plaintext Passwords
    Avoid using -p for passwords; instead, set them interactively.

  4. Assign Groups Wisely
    Carefully assign users to administrative groups like sudo.

Summary

The useradd command is a powerful utility for adding users in Linux. With various options, it provides flexibility for customizing user accounts. While simple to use, understanding its options and default behavior ensures secure and efficient user management.

userdel - user deletion

The userdel command in Linux is used to delete a user account and its associated files from the system. It is an essential command for user management, typically used by system administrators.

userdel [OPTIONS] USERNAME
  • USERNAME: The name of the user account to be deleted.
  • OPTIONS: Parameters that control how the deletion is performed.

  • Features of userdel

    1. Removes User Account
      Deletes the user entry from system files like /etc/passwd, /etc/shadow, and /etc/group.

    2. Manages User Data
      Can optionally delete the user’s home directory and mail spool.

    3. Handles Logged-In Users
      Will not delete accounts of users who are currently logged in without force.

    Options

    OptionDescription
    -fForce deletion of the user account, even if the user is currently logged in.
    -rRemoves the user's home directory and mail spool along with the account.

    How to Use userdel

    1. Delete a User Account

        sudo userdel alice
    This removes the alice user but leaves her home directory and files intact.

    2. Delete a User and Their Files
        sudo userdel -r bob

    This removes the bob user and deletes:

    • The user’s home directory (/home/bob).
    • The user’s mail spool (if it exists).
    3. Force Delete a Logged-In User
        sudo userdel -f charlie
    This forces the deletion of the charlie account, even if the user is logged in.

    Best Practices

    1. Check User’s Activity Ensure the user is not running any critical processes before deletion:

            who | grep USERNAME

          2.Backup Important Data If unsure, back up the user’s data before deletion:
            tar -czvf alice_backup.tar.gz /home/alice

           3.Review Files Owned by the User Identify files owned by the user in the system:
            find / -user USERNAME

            4.Avoid Forced Deletion (-f) Use force deletion sparingly, as it can disrupt active sessions 
                or processes.

    Summary

    The userdel command is a powerful tool for managing and removing user accounts in Linux. Its options allow for flexible deletion, including managing user data. By following best practices and verifying actions before execution, system administrators can ensure a smooth and error-free user removal process.

    groupadd - adding new groups

    The groupadd command in Linux is used to create new groups. Groups are a way to organize and manage user permissions by associating users with specific roles or resource access.

    groupadd [OPTIONS] GROUPNAME

  • GROUPNAME: The name of the new group to be created.
  • OPTIONS: Parameters to customize the group.

  • Features of groupadd

    1. Creates New Groups
      It adds a group entry to the system files such as /etc/group and /etc/gshadow.

    2. Configures Group Properties
      You can set custom Group IDs (GID) or configure it as a system group.

    Options

    OptionDescription
    -fDoes not return an error if the group already exists.
    -g GIDSpecifies a Group ID (GID) for the new group.
    -K KEY=VALUEOverrides /etc/login.defs defaults for this group creation.
    -oAllows the creation of a group with a duplicate GID (use cautiously).
    -rCreates a system group (with a GID typically below 1000).

    How to Use groupadd 

    1. Create a Group with Default Settings
        sudo groupadd developers

    This creates a group named developers with:

    • A unique GID automatically assigned.
    • Entries in /etc/group and /etc/gshadow.
    2. Create a Group with a Specific GID

        sudo groupadd -g 2000 admins
        This creates a group admins with GID 2000.

    3. Create a System Group

    System groups are typically used for system-level services and processes.

        sudo groupadd -r sysadmins

    Configuration Files Affected

    1. /etc/group

    Stores information about groups, such as group name, GID, and associated users.
    Example:

    developers:x:1001:

    2. /etc/gshadow

    Stores secure group information like passwords and administrators.

    Best Practices

    1. Choose GIDs Carefully
      Avoid duplicate GIDs unless absolutely necessary.

    2. Use System Groups for Services
      Reserve system groups for non-user-specific tasks.

    3. Check for Existing Groups
      Use getent to check if a group already exists:

            getent group developers

           4.Pair with User Management
            After creating a group, assign users to it:

            sudo usermod -aG developers alice

    Viewing Groups

    To list all groups on the system:
    cat /etc/group

    To check details for a specific group:
    getent group developers

    Deleting a Group

    To remove a group, use the groupdel command:

    sudo groupdel developers

    Summary

    The groupadd command is an essential tool for managing groups in Linux. Groups simplify permission management by enabling role-based access control. By understanding its options and configuration, you can create and manage groups effectively to enhance system organization and security.

    Comments

    Popular posts from this blog

    IT Workshop GXESL208 KTU BTech 2024 Scheme - Dr Binu V P

    Familiarization of basic Linux Commands- ls, mkdir, rmdir , rm, cat, cp, mv , chmod