Add user to sudo group on CentOS linux

Photo of author

By rasho

The sudo command is designed to allow users to run programs with the security privileges of another user, by default the root user.
The configuration file offers detailed access permissions, including enabling commands only from the invoking terminal; requiring a password per user or group; requiring re-entry of a password every time or never requiring a password at all for a particular command line. It can also be configured to permit passing arguments or multiple commands.
In this guide we will show you how to create a new user with sudo privileges on CentOS. You can use the sudo user to perform administrative tasks on your CentOS machine without a need to logging in as the root user.

1. Connect via SSH

First of all, connect to your server via SSH. Once you are logged in, you need to add a new system user.

# ssh root@server_ip_address

2. Add new system user in CentOS

Create a new user account using the useradd command.

# useradd newuser

You need to replace newuser with the name of the user you want to add. Also, you need to set up a password for the newly added user.

3. Create a strong password

Use the passwd command to set a password for the new user.

# passwd newuser

You will be prompted to confirm the password. Make sure you use a strong password.

Set strong password to new user
Set strong password to new user

[ads]

4. Add user to the wheel group in CentOS

The wheel group is a special user group that allows all members in the group to run all commands. Therefore, you need to add the new user to this group so it can run commands as superuser. You can do that by using the following command:

# usermod -aG wheel newuser

Again, make sure you are using the name of the actual user instead of newuser.
Now, use visudo to open and edit the /etc/sudoers file. Make sure that the line that starts with %wheel is not commented. It should look exactly like this:

Now that your new user is set up you can switch to that user and test if everything is OK.

5. Switch to the sudo user

Switch to the newly created user:

# su newuser

Now run a command that usually doesn’t work for regular users like the one below:

# ls -la /root/

You will get the following error message:

ls: cannot open directory /root/: Permission denied

Try to run the same command, now with using sudo:

# sudo ls -ls /root

You will need to enter the password for the new user to proceed. If everything is OK, the command will list all the content in the /root directory.

Conclusion

That’s all. You have successfully created a sudo user on your CentOS system. You can now use this user to perform administrative tasks on your server.

Feel free to leave a comment if you have any questions.

Leave a Comment