RHCSA Exam Study — Pt. 2 Setting Up a User & Understanding Visudo

This piece builds on Pt. 1 linked here, and I’m working with RHEL 7.4 at the time of writing.

TL;DR:
$ sudo su -
# useradd granular
# passwd granular
# usermod -aG wheel granular

As you install your virtual machine, you’ll be prompted to create a user and set the root password. There is a box to check to make that user and admin. For RHCSA, you’ll also need to be comfortable with creating and managing users on your system. The basic commands that you should have memorized are the following:

$ sudo su -
// this switches to you the become the root user
# useradd granular
// we just created a user named granular
# passwd granular
// after following the prompts, this will set the password for the user granular

Now we need to decide if we are going to let this user have root (or sudo) access. To give this user root, we will add this user to the wheel group. In order to gain a solid understanding of why we’re adding this user to the wheel group, let’s first have a look at the sudoers file. We can edit and view the file at /etc/sudoers, and we can use the visudo command to directly edit this file with Vi or Vim.
Vi and Vim are both text editors you can use directly from the terminal. Vi is from the early Unix days, and it is still popular and ubiquitous today. Vi wasn’t freely available until the early aughts, thus Vim (Vi iMproved) emerged as a free Vi-like editor.

You will really want to make sure that you learn how Vi or Vim work so the rest of your RHCSA study can go more smoothly. At the very least, you can be sure that Vi will be on the system that you’ll test on, and it is the default editor for visudo.

// to edit or view the /etc/sudoers file you can either use:
# vim /etc/sudoers
// or you can simply use:
# visudo
// either of these will open the file

Once the sudoers file is open, we can type “/” on the keyboard, then type “wheel” and hit enter to search this word. This will bring us to the section of the file dealing with the wheel group.
You’ll see this:

## Uncomment to allow members of group wheel to execute any command
%wheel ALL=(ALL) ALL

This should be the default state on a fresh install. There’s a section below this text that you can un-comment if you want to be able to run sudo commands with users in the wheel group without requiring that these users enter their password every time they use sudo in the terminal.
After un-commenting, that section should look like this:

## Same thing without a password
%wheel ALL=(ALL) NOPASSWD: ALL

However, un-commenting this section is up to you. The argument goes something like, “If they already logged in as themselves, and they are sudoers, then requiring their password be typed every time they want to use sudo is not necessary.” If you want to un-comment this section, you’ll need to learn about the two main modes of Vim: Insert Mode and Command Mode. You’ll need Insert Mode to actually delete the “#,” and Command Mode to exit and save. You always start out in Command Mode. Type “i” to enter Insert Mode and hit “Esc” to get back to Command Mode. To save, from Command Mode, type “:wq!”
The “w” is for write and the “q” for quit.
Now that we understand more about visudo, let’s actually add our user granular to the group “wheel,” like this:

# usermod -aG wheel granular
// it's important you use "-aG" option so you append this group to the user's group memberships rather than overwrite the current group affiliations
// you can verify that your command worked by switching to the user granular with:
# su granular
// then make sure you have root by using:
$ sudo whoami
// if all went well, you'll see: "root" after you execute the above command

So, figure out a way to memorize how to do this. Practice this one every day or every week or whatever is appropriate for your timeline as we’re considering preparing for the RHCSA.
In review, so far, we’re just created a user called granular, assigned the user a password and gave the user sudo privileges.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store