This guide will walk you through the simple steps of adding, editing, and deleting users in CentOS 7 server.
Adding, modifying, and deleting users on CentOS 7 is a fundamental task for system administrators. Proper user management is critical for maintaining the security and integrity of the system. CentOS 7 provides several tools to manage users, including the command-line interface and graphical user interface. In this article, we will discuss how to add, modify, and delete users on CentOS 7 using both methods.
The command-line interface is the most common way to manage users on CentOS 7. It provides a quick and efficient way to add, modify, and delete users. The command-line interface provides several commands to manage users, including useradd, usermod, and userdel. These commands allow administrators to create new users, modify existing users, and delete users from the system. The command-line interface is ideal for managing users on remote servers or when the graphical user interface is not available.
Prerequisites
Before we begin, make sure you have the following prerequisites:
- A machine running CentOS 7
- Access to a terminal
- User privileges: root or non-root user with sudo privileges
How to Add a New User on CentOS 7
To add a new user to your CentOS system, you'll need to have root or sudo access. Both adduser
and useradd
command will work. Here's the command to add a new user:
sudo adduser <username>
For the sake of this tutorial let's assume we will create a new user named developer
sudo adduser developer
This command will create a new user with the specified username. The user will be asked additional information during the process. You can also add additional options to the command to specify a home directory or user groups.
Next, you’ll need to give your user a password so that they can log in. To do so, use the passwd command:
sudo passwd developer
You will be prompted to enter a new password for the user. Enter the password and press Enter. You will then be prompted to confirm the password by entering it again. Once confirmed, the password will be set for the user.
It is important to note that the password should be strong and not easily guessable. A strong password should contain a mix of uppercase and lowercase letters, numbers, and special characters. Additionally, it should be at least 8 characters long.
Once the user has been created, you can switch to their account using the su command:
sudo su <username>
In our case we can enter following command to switch to developer
's account.
sudo su developer
How to Grant a User Sudo Privileges on CentOS 7
If you want your new user to be able to run commands with root (administrative) capabilities, you must grant them sudo access. Let's look at two approaches to this task: first, adding the user to a pre-defined sudo user group, and second, specifying privileges in sudo's setup on a per-user basis.
Option 1: Adding the New User to the Sudo Group
On CentOS 7 computers, sudo is set to grant full privileges to any user in the sudo group by default.
With the groups
command, you can see which groups your new user is a member of:
groups developer
// Output
developer : developer
A new user is by default only a member of their own group because adduser
creates this in addition to the user profile. Both a user and its own group have the same name. You can use the usermod
command to include the user in a brand-new group. To add user to sudoers enter following command:
sudo usermod -aG wheel developer
Users with wheel
group has sudo
access to all of its members by default.
Option 2: Specifying Explicit User Privileges in /etc/sudoers
Instead of adding your user to the sudo group, you can use the nano /etc/sudoers
command or visudo
command to open the system's default editor and manually set each user's capabilities in the configuration file /etc/sudoers
.
sudo visudo
This will open file /etc/sudoers
and this will contain
// file: /etc/sudoers
root ALL=(ALL:ALL) ALL
Just add developer ALL=(ALL:ALL) ALL
line to grant sudoers privileges to user developer
.
// file: /etc/sudoers
root ALL=(ALL:ALL) ALL
developer ALL=(ALL:ALL) ALL
For each user who needs to have full sudo access, add a new line like this. Once finished, save and close the document by pressing CTRL + X, followed by Y, and ENTER to confirm.
How to Modify a User on CentOS 7
You can modify a user's account information using the usermod command. Here are a few examples:
Change the Username
If you need to change a user's username, you can do so with the following command:
sudo usermod -l <new_username> <old_username>
This command will change the user's username from the old value to the new value. In our case we can change the username developer
to john
sudo usermod -l john developer
Changing Own Password
Type following passwd command command to change your own password:
passwd
The following prompt will appear where you have to enter Current Password and then enter New Passwords to set a new password.
// Output
Changing password for developer.
Current password:
New password:
Retype new password:
The password has been changed successfully.
Changing Password for Other User
If you are a root user or non-root user with sudo privileges, you can change password for other user. The commad is passwd <username>
, sudo privileged user will have to use sudo
before this command:
sudo passwd developer
The following prompt will appear where you have to enter New Passwords to set a new password for the user.
// Output
Changing password for developer.
New password:
Retype new password:
The password has been changed successfully.
Change the Home Directory
To change a user's home directory, use the -d option:
sudo usermod -d /path/to/new/directory <username>
This command will change the user's home directory to the specified path.
Add User to a Group
To add a user to an existing group, use the -aG option:
sudo usermod -aG <group_name> <username>
This command will add the user to the specified group.
Changing the Shell
Most modern systems have a default user bash
though it can vary based on Linux distribution or the user’s environment. To change the shell for a user:
sudo chsh <username>
Changing the User Information
To change the details for a user (for example full name, room number, etc.):
sudo chfn <username>
How to Delete a User on CentOS 7
If you need to delete a user from your CentOS system, you can do so with the following command:
sudo userdel <username>
By default, userdel
will remove the user without removing the home directory, the mail spool or any other files on the system owned by the user. Removing the home directory and mail spool can be achieved using the -r
option.
sudo userdel -r <username>
If you previously configured sudo privileges for the user you deleted, you may want to remove the relevant line again by entering vissudo
or nano /etc/sudoers
.
// file: /etc/sudoers
root ALL=(ALL:ALL) ALL
developer ALL=(ALL:ALL) ALL # Delete this line
This will prevent a new user created with the same name from being accidentally given sudo
privileges.
Conclusion
Adding and deleting users on CentOS 7 is a straightforward process, and can be done quickly using the command line interface. By following the steps outlined in this guide, you can easily manage the users on your system. Remember to always use caution when adding or deleting users, as these actions can have significant consequences if done incorrectly.
We hope you found this guide useful. For more information on CentOS, including tips, tricks, and tutorials, be sure to check out Rexposed CentOS Articles, your go-to source for all things CentOS.