Skip to content

Beginner · Permissions, users and sudo · topic 12 of 57 · 25 min

sudo and the sudoers file

Explanation

sudo lets an allowed user run one command as root, asks for the user's own password, and logs what was run. Rules live in /etc/sudoers and /etc/sudoers.d/, and you edit them only with visudo, which checks the syntax. On RHEL, members of the wheel group may run any command with sudo.

Why not just log in as root?

If five admins share the root password, you cannot tell who did what, and changing the password means telling five people. sudo fixes this. Each admin logs in as themselves and puts sudo before a command that needs root. sudo checks a rules file, asks for the admin's OWN password, runs the command, and records it in /var/log/secure. Root logins can then be turned off.

bash
[asha@web01 ~]$ dnf install -y httpd
Error: This command has to be run with superuser privileges (under the root user on most systems).
[asha@web01 ~]$ sudo dnf install -y httpd
[sudo] password for asha:
...
Complete!
[asha@web01 ~]$ sudo -i        # a full root shell, when you must do many root tasks
[root@web01 ~]# exit

The wheel group

RHEL ships /etc/sudoers with this line already active: %wheel ALL=(ALL) ALL. % means a group. So making a user an admin is one command: usermod -aG wheel asha. She needs to log in again to get the group.

Reading a sudoers rule

output
# who   where = (as whom)   what
asha    ALL   = (ALL)       ALL
%wheel  ALL   = (ALL)       ALL
ravi    ALL   = (root)      /usr/bin/systemctl restart httpd, /usr/bin/systemctl status httpd
%ops    ALL   = (ALL)       NOPASSWD: /usr/bin/dnf
Fields: user or %group, host, the users they may run as, and the commands (full paths).

ravi above may only restart and check httpd. NOPASSWD: skips the password prompt, which is convenient for automation accounts (Ansible uses this) but should be used carefully for people.

Editing safely: visudo and sudoers.d

bash
# visudo                                  # edit /etc/sudoers with a syntax check
# visudo -f /etc/sudoers.d/ops             # edit a drop-in file
# echo '%ops ALL=(ALL) NOPASSWD: /usr/bin/dnf' > /etc/sudoers.d/ops
# chmod 440 /etc/sudoers.d/ops
# visudo -c
/etc/sudoers: parsed OK
/etc/sudoers.d/ops: parsed OK
$ sudo -l                                  # as a user: what am I allowed to run?

Prefer a small file in /etc/sudoers.d/ for each rule set instead of editing the main file. Package updates may replace /etc/sudoers, and separate files are easy to add and remove with Ansible. The main file reads the directory through a line #includedir /etc/sudoers.d (newer sudo versions also accept @includedir). The # in #includedir is NOT a comment.

Careful: A syntax error in sudoers can stop sudo working for everyone. If root logins are disabled, you have locked yourself out of admin access. Always use visudo (or visudo -c after writing a file), and keep a second root session open while you test.

Common mistakes

  • Editing /etc/sudoers with plain vim and saving a typo. Use visudo.
  • Giving a user sudo for an editor (sudo vim) or a shell-capable tool. From vim they can run :!bash and get a root shell. Limit commands carefully, or use sudoedit for editing.
  • Writing command names without full paths in rules. Always use /usr/bin/systemctl, not systemctl.
  • Making a drop-in file with a dot or ~ in its name (like ops.conf~ or ops.bak with a dot): sudo skips files whose names contain a dot or end in ~. Name it simply ops.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

sudo and the sudoers file · 1/7

sudo <command>

sudo lets an allowed user run one command as root, with their own password, and logs it.

Prefer a video by a teacher? Find free videos on “sudoers visudo wheel group tutorial rhel” on YouTube ↗

In real life: On-call support at a telecom company

A telecom company in Gurugram has a 24x7 L1 support team. They must restart the billing API when it hangs, but must not get full root. The admin writes a sudoers drop-in letting the l1support group run only systemctl restart and status for that one service. Every restart is logged in /var/log/secure with the person's name, which the team lead reviews each morning.

bash
# cat /etc/sudoers.d/l1support
%l1support ALL=(root) /usr/bin/systemctl restart billing-api, /usr/bin/systemctl status billing-api
# grep COMMAND /var/log/secure | tail -1
Oct 19 03:12:44 app01 sudo[41522]:  kiran : TTY=pts/1 ; PWD=/home/kiran ; USER=root ; COMMAND=/usr/bin/systemctl restart billing-api

Practice (0/3 done)

Task 1 · Question

Which line in sudoers lets every member of the group "admins" run any command as any user?

Task 2 · Question

Which line has the bug? File /etc/sudoers.d/web: 1: %web ALL=(root) /usr/bin/systemctl restart httpd 2: %web ALL=(root) systemctl status httpd

Task 3 · Practice

Give the group "sysadmins" (from the earlier task) full sudo rights WITHOUT a password, using a drop-in file, and check it as harry.

Quick quiz

Question 1

1.Whose password does sudo ask for by default?

Question 2

2.Why use visudo instead of vim /etc/sudoers?

Question 3

3.Where does RHEL log sudo commands?

Question 4

4.Which command shows what you are allowed to run with sudo?

Question 5

5.Why is "user ALL=(root) /usr/bin/vim" risky?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

Whose password does sudo ask for by default?

Answer: The user's own

sudo asks for the calling user's own password.

Why use visudo instead of vim /etc/sudoers?

Answer: It checks the syntax and locks the file

A broken sudoers file can lock out all admins; visudo prevents saving errors.

Where does RHEL log sudo commands?

Answer: /var/log/secure (and the journal)

Authentication and sudo events go to /var/log/secure.

Which command shows what you are allowed to run with sudo?

Answer: sudo -l

sudo -l lists your rules.

Why is "user ALL=(root) /usr/bin/vim" risky?

Answer: From vim the user can start a root shell

Editors can run shell commands, so this is close to full root. Use sudoedit instead.

Why use sudo instead of logging in as root?Related

Answer: Each admin action is tied to a named user and logged

Named users plus logging gives accountability and fewer mistakes.

From Linux security basics

Which command adds user ravi to the docker group without removing his other groups?Related

Answer: sudo usermod -aG docker ravi

-a appends. Without it, -G replaces all extra groups.

From Users, groups and permissions

Which file can only root read?Related

Answer: /etc/shadow

/etc/shadow holds the password hashes, so it is readable by root only.

From Users, groups and the files behind them

vim says "E45: readonly option is set" on /etc/hosts. Most likely cause?Related

Answer: You opened a root-owned file as a normal user without sudo

Open it again with sudo vim /etc/hosts.

From Editing files with vim

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. This is the last topic of “Permissions, users and sudo”. The module exam comes next.
  2. Module 2 exam: Permissions, users and sudo · 14 questions · 25 min, without help. 6 topics of this module still to finish first.
  3. Then the next module, Processes, services and logs. 57 topics left in the course before the final exam.

← Previous: Access control lists (ACLs)