How to Create / Delete user in Linux

In the expansive world of Linux, the concept of user account management plays an indispensable role. Whether you’re administering a multi-user system or just maintaining a personal computer, understanding the basics of user creation and management is essential.

Why? Well, at its core, Linux is a multi-user operating system. This means it’s built to accommodate multiple users, each with their own preferences, settings, and permissions. Ensuring that each user has the appropriate access rights and restrictions not only optimizes the user experience but also enhances the system’s security.

Prerequisites

Before diving into the depths of user account creation in Linux, let’s ensure you’re adequately prepared:

  • Required Knowledge: A basic understanding of Linux command-line operations is essential. Familiarity with fundamental concepts like files, directories, and permissions can be very helpful.
  • Necessary Tools and Software: Access to a Linux-based system, whether a physical machine, virtual machine, or even a cloud instance. Ensure you have the necessary permissions to execute administrative tasks. A text editor, like Vim or Nano, will also be helpful for editing configuration files.

The Basics of Linux Users

Linux differentiates its users to ensure that the system operates seamlessly and securely. The categorization can be broadly understood as follows:

  • Types of Users:
    • Root User: The superuser or the administrative user. It has unrestricted access to all commands, files, and resources. Think of it as the master key to the mansion.
    • Regular User: Ordinary users created by the system administrator or root. They have permissions to access and modify their files but have restricted access to system files and settings.
    • System Users: These are users that are created for running system processes and services. They don’t usually have login capabilities, and their primary role is to ensure smooth operation of services.
  • Understanding /etc/passwd and /etc/group: These are essential configuration files in Linux that contain user and group information, respectively. The /etc/passwd file lists every user on the system along with crucial data like user ID, home directory, and default shell. Similarly, the /etc/group file contains group-related information.
  • User IDs and Group IDs: Every user and group in Linux is assigned a unique identification number called User ID (UID) and Group ID (GID), respectively. The root user always has a UID of 0, signifying its supreme stature in the Linux hierarchy.

Adding a New User

Creating a new user is one of the most fundamental tasks in Linux system administration. Here’s how you can achieve it:

  • Using the useradd Command: The useradd command is the primary tool for adding new users.
useradd [options] username

Some commonly used options are:

  • -c: To add a comment (often used for user’s full name).
  • -d: To specify the home directory.
  • -e: To set an expiration date for the user account.
  • -g: To specify the primary group.
  • -m: To create the user’s home directory if it doesn’t exist.

Example:

useradd -c "John Doe" -m johndoe

Setting a Password with passwd: After creating a user, it’s essential to set a password to secure the account.

passwd username

Customizing the User Environment

Once you’ve created a user, you may want to customize their environment to enhance their experience or ensure specific configurations:

  • User Home Directories: By default, when you create a user, Linux establishes a home directory for them in /home/username. This directory houses user-specific files, configurations, and personal data.
  • Modifying Default Shell using chsh: If you want to change the default shell for a user, you can use the chsh command:
chsh -s /path/to/shell username

For example, to set the bash shell for user ‘johndoe’:

chsh -s /bin/bash johndoe

Role of Skeleton Directories (/etc/skel): When you create a new user, the system often copies files from /etc/skel to the user’s home directory. This provides a basic setup and can be modified to ensure every new user gets a specific set of starter files.

Assigning Users to Groups

Groups help in managing permissions and access rights for multiple users collectively. Here’s how you can manage group assignments:

  • Understanding Primary and Supplementary Groups: When you create a user, they are automatically assigned a primary group (usually the same as their username). Additionally, a user can be a part of multiple supplementary groups.
  • Using usermod to Modify Group Memberships: If you wish to add a user to additional groups, you can use the usermod command:
usermod -aG groupname username

The -aG option ensures that the user is appended to the group without removing them from any existing group.

Advanced User Creation Techniques

While the basic user management commands suffice for many tasks, sometimes you need more advanced techniques for specific scenarios:

  • Using adduser (Interactive Approach): While useradd is a basic command, some distributions come with a more interactive script called adduser. This utility walks you through the user creation process, prompting you for necessary details.
adduser username

Batch Creation of Users: There might be situations where you need to add multiple users simultaneously. This can be accomplished using a script that reads a list of usernames from a file and uses the useradd command to create each one.

Example script:

while read user; do
    useradd -m $user
done < users_list.txt

User Templates and Configurations: Customize the user creation process further by adjusting settings in /etc/login.defs. This file defines defaults, like the range for user IDs, password expiration settings, etc.

Setting Account Expirations and Restrictions

User account control also involves defining how long an account remains active and setting certain restrictions:

  • Understanding /etc/shadow and Account Aging: The /etc/shadow file stores encrypted password information and other details related to user passwords, including their aging. Fields in this file can be used to enforce password change policies.
  • Using chage for Password Expiry Settings: The chage command helps set password expiration policies for users. For instance, to set a user’s password to expire after 60 days, you’d use:
chage -M 60 username

Limiting User Logins: You can restrict or permit user logins using the nologin shell. Setting a user’s shell to /sbin/nologin effectively prevents them from logging into the system.

User Account Modification and Deletion

Over time, user details might change, or there could be a need to remove a user altogether:

  • Modifying User Details with usermod: The usermod command is versatile. Besides modifying group memberships, you can change a user’s home directory, login name, shell, and more.For instance, to change a user’s home directory:
usermod -d /new/home/directory username

Deleting Users with userdel: Removing a user is straightforward with the userdel command. But exercise caution; this is irreversible!

To delete a user and their home directory:

userdel -r username

Important Considerations When Removing Users: Always back up critical data before deleting users. Furthermore, ensure that processes or services aren’t running under the user you’re about to delete.

Best Practices for User Management

Effectively managing user accounts isn’t just about knowing commands; it’s about understanding best practices to maintain a secure and efficient system.

  • Setting Strong Passwords and Enforcing Policies: Encourage users to use robust passwords combining letters, numbers, and special characters. Implement password policies using tools like PAM (Pluggable Authentication Modules) to enforce password complexity and periodic changes.
  • Regular Audits of User Accounts: Periodically review the user accounts on your system. Check for inactive accounts, unauthorized access, or users with elevated permissions that might not require them.
  • The Principle of Least Privilege: Only grant users the permissions they absolutely need. Avoid giving broad permissions unless it’s necessary. The fewer privileges a user has, the less potential damage they can cause intentionally or unintentionally.

Troubleshooting Common Issues

Even with the best practices in place, you may encounter challenges. Here are some common issues and how to address them:

  • Common Errors During User Creation or Modification: Errors can occur if you try to create a user with a name that already exists, or if there’s an issue with the /etc/passwd or /etc/shadow files. Always read error messages carefully; they usually contain clues to resolve the issue.
  • Diagnosing Login Issues: If a user cannot log in, check their password in /etc/shadow, ensure their account hasn’t expired (chage -l username), and verify their shell in /etc/passwd isn’t set to /sbin/nologin.
  • Recovering from Accidental Deletions: Always backup user data and important configuration files. If you accidentally remove a user, while you can’t undo the userdel command, having backups can simplify the process of recreating their account and restoring their data.

Conclusion

User account management is a fundamental pillar of Linux system administration. It’s a balance of providing users the access they need while maintaining the security and efficiency of the system. By understanding the concepts we’ve discussed and consistently applying best practices, you’ll ensure a robust environment for both users and administrators alike.

As with any technical skill, continuous learning and hands-on practice are crucial. The Linux ecosystem is vast and ever-evolving. Stay curious, keep experimenting, and always prioritize the security and integrity of your systems.

Atiqur Rahman

I am MD. Atiqur Rahman graduated from BUET and is an AWS-certified solutions architect. I have successfully achieved 6 certifications from AWS including Cloud Practitioner, Solutions Architect, SysOps Administrator, and Developer Associate. I have more than 8 years of working experience as a DevOps engineer designing complex SAAS applications.

Leave a Reply