How to Create or delete Users in Linux – With examples

How to Create or delete Users in Linux – With examples

Managing user accounts is an essential part of Linux system administration. Whether setting up a multi-user environment, removing old accounts, or configuring permissions, knowing how to create and delete users is fundamental. This article guides readers through the process of adding and removing users in Linux, with practical examples and explanations.

Creating a User in Linux

All Heading

There are two primary commands used to create users: useradd and adduser. While adduser is more user-friendly and interactive, useradd offers more control to advanced users.

1. Using useradd

To create a new user, use the following syntax:

sudo useradd username

This command will create a user named username, but it will not set up a home directory or password by default. To do that, add options:

sudo useradd -m -s /bin/bash username
  • -m: Creates a home directory at /home/username
  • -s: Sets the default shell (in this case, bash)

Next, set a password:

sudo passwd username

You will be prompted to enter and confirm the new user’s password.

2. Using adduser

The adduser command is more interactive and simplifies the process:

sudo adduser username

This command will:

  • Create the user’s home directory
  • Assign a default shell
  • Prompt for a password and user information

Assigning a User to a Group

Groups are used to manage permissions across multiple users. After creating a user, it’s often necessary to assign them to groups.

sudo usermod -aG groupname username
  • -aG adds the user to the group without removing them from existing groups.

For example, to add a user to the sudo group:

sudo usermod -aG sudo username

You can verify group membership using:

groups username

Deleting a User in Linux

When a user account is no longer needed, it can be removed using the userdel command.

Basic User Deletion

sudo userdel username

This command deletes the user from the system but retains their home directory and files.

Deleting User Along with Home Directory

sudo userdel -r username

The -r flag removes the user’s home directory and mail spool, effectively cleaning up all personal data.

Managing User Files and Permissions

After deleting a user, system administrators may want to reassign or archive the user’s files. If you did not use the -r option, files may still reside under /home/username.

To change file ownership, use:

sudo chown newuser:newgroup /home/olduser -R

This will transfer ownership of all files recursively to another user.

Conclusion

Creating and deleting users in Linux is a straightforward task for those familiar with the command line. Whether using useradd for granular control or adduser for ease of use, administrators have powerful tools at their disposal to manage user accounts effectively. Always ensure that proper backups and checks are performed before deleting users, especially in production environments.

Frequently Asked Questions (FAQ)

  • Q: What’s the difference between useradd and adduser?
    A: adduser is a Perl script that acts as a friendly frontend to useradd, making it easier for interactive use. useradd is a more basic, low-level command.
  • Q: Can I create multiple users at once?
    A: Yes, by scripting or using loops in the shell, you can automate the creation of multiple users.
  • Q: What happens if I delete a user without the -r flag?
    A: The user account will be removed, but their home directory and files will remain on the system.
  • Q: How do I lock a user account instead of deleting it?
    A: Use sudo usermod -L username to lock the account, and sudo usermod -U username to unlock it.
  • Q: Is it safe to remove the root user?
    A: No. The root user is essential for system operations and cannot be removed.