user and group

User Management Commands

Creating a New User

  • Command Used:

    • user add is the command used to create a new user.

  • Example: To add a user named test001:

    • Enter sudo user add test001.

    • Note: The command must be prefixed with sudo to gain permission and management level access.

  • Password Prompt:

    • After entering the command, you will be prompted to enter your sudo password.

  • Result:

    • Once executed, the user is created if no errors are encountered.

Viewing the List of Users

  • Command Used: Viewing the user list can be done by checking a specific file.

    • File Path: The user list is typically stored in /etc/passwd.

    • Command: cat /etc/passwd displays the list of all users, including any newly created users.

Extracting Specific User Information

  • To display only usernames from the user file, the cut command can be used.

    • Command Format:

    • cat /etc/passwd | cut -f1 -d':'

    • Explanation: This utilizes the cut command to filter by delimiter : and retrieves the first field (username).

Checking User Existence

  • Algorithm for Checking: To determine if a user exists:

    1. Prompt user for input of the username.

    2. Utilize cat /etc/passwd with grep to verify existence.

    3. Use an if statement to compare the input with existing usernames.

  • Command:

    • The command flow could look like:

    • if grep -q 'username' /etc/passwd

    • If it returns a result, the user exists.

User ID Command

  • Purpose: The id command provides information about a specific user including user ID and group ID.

    • Command Format: id <username>

    • Output: Displays user ID, primary group, and any supplementary groups associated with the user.

Handling Command Outputs

  • Null Device:

    • The null device can be used to discard output from a command.

    • Path: /dev/null acts as a garbage destination to redirect unwanted output.

  • Exit Status:

    • An exit status of 0 indicates successful execution. A non-zero exit status indicates an error.

Deleting a User

  • Command Format: To remove a user:

    • Use sudo userdel <username>

    • Note: If the user does not have a home directory, you may see a warning message.

    • To create a user with a home directory, the command would be sudo useradd -m <username>.

Group Management

Creating a New Group
  • Command:

    • sudo groupadd <groupname> (e.g., sudo groupadd tester)

Viewing Groups
  • File Path for Groups:

    • Use cat /etc/group to view existing groups.

  • Extracting Group Names:

    • Similar to extracting usernames from /etc/passwd, use:

    • cut -f1 -d':' /etc/group to get just the group names.

Adding a User to a Group
  • Command:

    • Use sudo gpasswd -a <username> <groupname> to add the user to a group.

Deleting a User from a Group
  • Command Format:

    • Use sudo gpasswd -d <username> <groupname> to remove a user from a group.

Checking Group Existence

  • Command:

    • Use getent group <groupname> to check if a specific group exists.