Creating/Deleting Users

Absolutely! Let’s dive into user management in Ubuntu, breaking down each command used for viewing, creating, modifying, and deleting users. Here’s everything you need as a newcomer to Linux and Ubuntu.

1. Commands to View Existing Users in Ubuntu

  • /etc/passwd File:

    • The /etc/passwd file contains a list of all users on the system. Each line in this file represents a single user, including the root user and any system or application users created during system setup.

    • To view this file, use the command:

      cat /etc/passwd
    • Each entry in /etc/passwd has several fields, separated by colons (:), with information such as the username, user ID (UID), group ID (GID), user description, home directory, and default shell.

  • cut Command to Display Only Usernames:

    • To focus only on usernames, you can extract the first field of each line in /etc/passwd with the cut command:

      cut -d: -f1 /etc/passwd
    • This command lists only usernames, making it quick to scan the system for existing users.

  • getent Command:

    • The getent command retrieves entries from system databases configured in /etc/nsswitch.conf, including the passwd database for users.

    • Run this command to list all users:

      getent passwd
    • The getent command is useful for querying network-based user databases as well if you’re in a corporate environment with directory services like LDAP.

2. Creating a New User in Ubuntu

  • adduser Command:

    • The adduser command is the most user-friendly way to create new users. It prompts you to enter details like password, full name, room number, and more.

    • To create a user, use:

    • Example:

    • Explanation of Steps:

      • This command will:

        • Create a new entry for johndoe in /etc/passwd and /etc/shadow.

        • Create a home directory at /home/johndoe.

        • Copy default configuration files from /etc/skel to the new home directory.

        • Set up user information and prompt for a password.

  • useradd Command:

    • useradd is a low-level command for creating users, and it doesn’t prompt for additional information by default.

    • To create a user with useradd, use:

    • Example:

    • After creating the user, set the password manually:

    • Difference Between useradd and adduser:

      • useradd only creates a user account, without creating a home directory or copying files from /etc/skel.

      • adduser is more comprehensive and typically recommended for new users.

3. Modifying Existing Users

  • usermod Command:

    • The usermod command modifies user account settings, like username, home directory, or shell.

    • Common options:

      • Change Username:

      • Change Home Directory:

      • Add User to a Group:

      • Set Default Shell:

    • Example:

    • This command adds johndoe to the sudo group, granting administrative privileges.

4. Deleting a User in Ubuntu

  • deluser Command:

    • The deluser command is a simple way to remove a user account.

    • Basic syntax:

    • Example:

    • This removes johndoe from /etc/passwd and /etc/shadow but retains the home directory by default.

    • Remove User and Home Directory:

      • To delete both the user and their home directory, use:

      • Example:

      • This command deletes johndoe and removes /home/johndoe.

  • userdel Command:

    • Similar to deluser, the userdel command deletes a user account.

    • Basic syntax:

    • Example:

    • Remove Home Directory:

      • To remove the home directory as well, use:

      • Example:

5. Additional Commands and Tips

  • passwd Command:

    • The passwd command sets or changes a user’s password.

    • Basic syntax:

    • Example:

    • It prompts you to enter a new password for the user and updates it in /etc/shadow.

  • id Command:

    • The id command displays user identity information, including UID, GID, and groups.

    • Basic syntax:

    • Example:

    • This command shows details like UID, primary GID, and supplementary groups.

  • finger Command:

    • The finger command (if installed) provides detailed information about a user.

    • Install it with:

    • Example:

Summary

  • Viewing Users: Use cat /etc/passwd, cut -d: -f1 /etc/passwd, or getent passwd.

  • Creating Users: Use adduser for guided setup or useradd for a low-level approach.

  • Modifying Users: Use usermod to update usernames, home directories, groups, or default shells.

  • Deleting Users: Use deluser or userdel to remove user accounts, with options to delete home directories as well.

This guide should provide a solid foundation for managing users in Ubuntu. Let me know if you want more insights on any specific aspect!

Last updated