How to create and use a Sudo user on CentOS 8/CentOS Stream?

16.12.2020 1,136 0

In Linux OS there is a distinction between a regular user and a superuser. The superuser has extra privileges and root access which let him or her do more in comparison to the regular one. If you want the same privileges, you have to create a superuser and use the Sudo command. Sudo is the short variant for “superuser do”. We will see how to create, test and use a sudo user on Cent OS 8.

You can use the same instructions if you are using older versions such as CentOS 7 or CentOS 6. 

What is CentOS 8 and CentOS Stream?

CentOS Linux is a very popular distribution for web servers. CentOS started as a fork of RHEL version 2.1AS. It is free with community support. CentOS became a part of the Red Hat in 2014, while remaining independent. CentOS 8 works on the x86-64 architecture, ARM (64bit), and POWER8. It uses the common GNOME interface and Bash shell.

The current version of CentOS is 8, being released on 24.09.2019.

There is news about CentOS 8! The last update has been changed to 31.12.2021! CentOS 8 will be completely replaced by CentOS Stream. Follow our blog for further information.

There are still servers with CentOS 7 that got their last full update on 06.08.2020 and will still get maintenance updates until 30.06.2024. CentOS 6 is already outdated. Its last full update was on 10.05.2017 and its last maintenance update was on 30.11.2020. If you are using this version, it is time to upgrade.  

You can find more about the CentOS  in our article What’s new in CentOS Linux 8 or on the CentOS web site.

If you are using CentOS you might be interested in the following article, “How to configure and manage the firewall on CentOS 8?”.

This article will show you how to create a Sudo user under CentOS 8 and CentOS Stream, but it will work the same way on CentOS 7 or CentOS 6.  

How to create a Sudo user on CentOS 8 and CentOS Stream? Works on CentOS 7 and CentOS 6 too.

This is a step-by-step guide on how to create a Sudo user on CentOS 8. We will start by logging in to the CentOS server with the original root user. Then we will create a new user, set its password and we will add it to the “wheel” group to get sudo privileges. 

  1. Log into CentOS server

We will log in via ssh so we need to type the following: 

$ ssh root@server_ip_address

You will need to replace the “server_ip_address” part with its actual IP address. The “$” sign appears automatically in the Terminal. You don’t need 2 of them.

2. Create a new user account

We need to use the useradd command:

$ sudo useradd username

Change “username” to the name that you want to give to that user. 

You can also use the following text with the same result:

$ sudo adduser username

Both commands work, “useradd” is a low-level binary and “adduser” is a Perl script that looks more natural to an English speaker. 

3. Set user’s password

We will need the password command to set a password for the newly created user. Type in:

$ passwd username

Again, change “username” with the one you have just created in the second step. 

After pressing enter you will get the following message: 

Changing password for user username.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.

Now you will need to create a password and retype the new password. Make sure you have a strong enough password.

4. Add the new user to the sudo group (wheel)

Inside CentOS, there is the default group called “wheel”. The users inside it have sudo (root) privileges. To add our newly created user we will use the following command for modifying the user:

$ usermod –aG wheel username

Replace the “username” with the already created one. “usermod” is a command for modification of users. “-aG” is “add to a group”.

How to test the sudo user on CentOS 8 and CentOS Stream? CentOS 7, and CentOS 6 too. 

  1. Switch to the sudo user that you want to test. Type in the following:
$ su – username

Replace this “username” with the one you want to test.

2. Now let’s take a look at some information about the user with this command:

sudo whoami

The output will be this:

$ whoami
username
$ sudo whoami
[sudo] password for username:
Root

You will need a sudo access to perform this operation and input the sudo password for the user. In your output, “username” will be replaced by your user name. If  “root” is exhibited at the end of the output, then you are a sudo user.  

How to use Sudo on CentOS 8 and CentOS Stream? CentOS 7, and CentOS 6 too.

  1. Switch to a sudo user. 

First, let’s switch to the freshly created user. We will use the switch command in the Terminal and type in: 

$ su – username

As usual, replace the “username” with the new user.

2. Use a command with sudo.

You can now use root privileges with the user you have. All you have to do is just add “sudo” before the command and verify it with the user’s password. 

$ sudo command

Change “command” with the command you want. 

For example, you can get a list of the content (ls) of the “/root” directory. 

$ sudo ls -l /root

After pressing enter, you will get a message to input your password. This is the way CentOS verifies that it is actually the sudo user and not somebody else changing a delicate setting on the CentOS server. 

The first time when you do it, you will get a nice welcome note:

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.

[sudo] password for username:

Managing Sudo users on CentOS 8, CentOS Stream, CentOS 7, and CentOS 6.

The default Sudo group in CentOS 8 is called a “wheel”. We can see the member of the group with the lid command and an option –g (list of user ids). Enter the following text in the Terminal:

sudo lid -g wheel

Now you can see all the sudo users. You can verify if a particular user has root access.

Output
User1(uid=1000)
User2(uid=1001)
User3(uid=1002)

In your output, you will see the actual users’ names, not like in this example User1, User2, etc. We need to be a superuser (sudo) to perform this operation.

Deleting a sudo user on CentOS 8, CentOS Stream, CentOS 7, and CentOS 6.

To delete a user on CentOS 8, you will need to use the userdel command and type the following text in the Terminal: 

$ sudo userdel username

Replace the “username” part in the example with the name of the user you want to delete. We need to be a superuser to be able to delete other users.

Delete the home directory of the user and its account with the “–r” option. 

$ sudo userdel -r username

This command will delete not only the user but its files and mails. Be sure that you don’t need them before using the command. 

Conclusion

There are a lot of options with CentOS. You can create and a sudo user and use it with different commands. You can also see the users inside the sudo groups called “wheel”. You can use the sudo user to do different administrative tasks and fully manage your CentOS server. 

If you want to try CentOS on some of our excellent configurations, you can check out our Cloud servers or Dedicated server. Choose the plan that best serves your need or contact us and we would help you create a custom solution, tailored to your exact business needs.

You might also want to check the following related articles:

Leave a Reply

Your email address will not be published.