How to Create Users and Groups in CentOS 7?

Linux is an operating system designed to support multiple users simultaneously, making it a multi-user system. As a result, one of the fundamental tasks in Linux system administration is adding or removing users, which can be achieved by configuring the system settings.

When setting up a new server, the default account is typically the root account, which grants extensive privileges and flexibility. However, relying solely on the root account poses security risks and is not advisable for regular use.

The main concern with using the root account is its increased vulnerability to security exploits. Any command executed under the root account can potentially impact the entire file system of the server. Creating unprivileged user accounts for performing everyday tasks is recommended to mitigate this risk.

Furthermore, it is essential to establish separate accounts for each user who requires access to your server. This practice enables better monitoring and management of individual activities. While granting additional accounts, you can maintain administrative capabilities when necessary by utilizing a "sudo." Sudo allows authorized users to temporarily acquire administrative privileges, providing a more controlled and secure approach to server management.

We will cover steps on creating user accounts, assigning 'sudo' privileges, and creating a group on a CentOS 7 server.

Adding a New User

1. Log in to your server as the root user. 

# ssh root@server_ip_address

2. Use the adduser command to add a new user to your system.

# adduser username

Note – Be sure to replace the username with the user you want to create.

3. Use the passwd command to update the new user's password. 

# passwd username

Note: Set and confirm the new user's password at the prompt.
A strong password is highly recommended.

Linux User Password

Creating a Usergroup

1. To create a group, use the groupadd command.

# sudo groupadd NAME-OF-THE-NEW-GROUP

Adding a User to Group

1. Use the usermod command to add the user to the wheel group.

# usermod -a -G wheel username

Note: On CentOS, by default, the members of the wheel group have sudo privileges. Be sure to change the username with the actual user which you want to add to the wheel group.

Test Sudo Access on a New User Account

1. Use the su command to switch to the new user account.

# su - username

As a new user, verify that you can use sudo by prepending ‘sudo’ to the command you want to run with superuser privileges.

For example, you can list the contents of the /root directory, which is usually only accessible to the root user.

# sudo ls -la /root

Note: The first time you use sudo in a session, you will be prompted for the user account password; enter the password to proceed.

Linux User Password

2. To add a user to multiple groups, use the command given below – 

# usermod -a -G group1,group2,group3 exampleusername

→ Do you want to install the EPEL repo on Centos?
Please refer to Install EPEL for more details.

→ Want to install and configure Dovecot on Centos?
Please refer to Install Dovecot on Centos for more details.

Was this answer helpful?

« Back