How to Add and Delete Users on CentOS / Ubuntu: A Comprehensive Guide

Add and Delete Users on CentOS / Ubuntu Debian Redhat RHEL Alma Linux

Introduction

Managing user accounts is an essential task for system administrators, whether you’re using CentOS / RedHat or Ubuntu / Debian as your operating system. Adding and deleting users is a routine part of system administration and helps ensure the security and proper access control on your server. In this article, we will provide you with a step-by-step guide on how to add and delete users on both CentOS and Debian.

I. Adding Users

1 – Adding users on CentOS

  • Open a terminal and log in to your CentOS system as the root user or a user with sudo privileges.
  • To add a new user, use the useradd command followed by the username. For example, to add a user named “john,” type the following command:
$ sudo useradd john
  • Set a password for the new user using the passwd command:
$ sudo passwd john

You will be prompted to enter and confirm the password.

  • By default, the user will be added to a group with the same name as the username. To add the user to additional groups, use the usermod command:
$ sudo usermod -aG groupname john

Replace “groupname” with the desired group name and “john” with the username.

  • Once you’ve added the user, you can switch to the newly created account using the su command:
$ sudo su - john

You will be prompted to enter the password for the user.

2 – Adding users on Debian

  • Open a terminal and log in to your Debian system as the root user or a user with sudo privileges.
  • To add a new user, use the adduser command followed by the username. For example, to add a user named “john,” type the following command:
$ sudo adduser john

You will be prompted to set a password and enter additional user information.

  • If you want to add the user to a specific group, specify the group name during the user creation process.
  • Once the user is created, you can switch to the new account using the su command:
$ sudo su - john

Enter the password for the user to access the account.

II. Granting Sudo Privileges to a Users

1 – Granting Sudo Privileges to a User on CentOS

If you want to give your new user the ability to execute commands with root (administrative) privileges, you need to grant them access to sudo.

One way to do this is by adding the user to the wheel group, which automatically provides sudo access to all its members.

To add the user to the wheel group, use the usermod command:

$ sudo usermod -aG wheel john

Now, your new user can execute commands with administrative privileges. Simply prepend sudo before the command you want to run as an administrator:

$ sudo some_command

You will be prompted to enter your user account password (not the root password). After entering the correct password, the command will be executed with root privileges.

2 – Granting Sudo Privileges to a User on Debian

If you want to enable your new user to execute commands with root (administrative) privileges, you can grant them access to sudo. There are two approaches to accomplish this: adding the user to the pre-defined sudo group or specifying privileges on a per-user basis in the sudo configuration.

Adding the User to the Sudo Group

By default, sudo on Debian systems is configured to grant full privileges to any user in the sudo group.

You can check which groups your new user belongs to using the groups command:

$ sudo groups john
Output:
john : john

By default, a new user is only in their own group, which is created by adduser and shares the same name as the user. To add the user to a different group, you can utilize the usermod command:

$ sudo usermod -aG sudo john

The -aG option tells usermod to add the user to the specified groups.

Keep in mind that the usermod command itself requires sudo privileges. Therefore, you can only add users to the sudo group if you are logged in as the root user or as another user who is already a member of the sudo group. In the latter case, you will need to precede the command with sudo, as shown in the example:

$ sudo sudo usermod -aG sudo john

III. Deleting Users

1 – Deleting users on CentOS

  • Open a terminal and log in to your CentOS system as the root user or a user with sudo privileges.
  • To delete a user, use the userdel command followed by the username. For example, to delete the user “john,” type the following command:
$ sudo userdel john
  • By default, the user’s home directory and files will not be removed. If you want to delete the user’s home directory and files, use the userdel command with the -r option:
$ sudo userdel -r john

2 – Deleting users on Debian

  • Open a terminal and log in to your Debian system as the root user or a user with sudo privileges.
  • To delete a user, use the deluser command followed by the username. For example, to delete the user “john,” type the following command:
$ sudo deluser john
  • By default, the user’s home directory and files will not be removed. If you want to delete the user’s home directory and files, use the deluser command with the --remove-home option:
$ sudo deluser --remove-home john

Conclusion

Managing user accounts is crucial for maintaining a secure and well-organized system. In this article, we have provided you with a detailed guide on how to add and delete users on both CentOS and Debian. By following the step-by-step instructions, you can efficiently manage user accounts, assign proper access privileges, and ensure the security of your server. Remember to exercise caution when deleting users, as it can result in the loss of their associated files and data. Always have a backup and double-check your actions before proceeding.

By understanding these user management procedures, you’ll be able to handle user accounts effectively, maintaining a secure and organized server environment.

LEAVE A COMMENT