Beginner · Permissions, users and sudo · topic 12 of 57 · 25 min
sudo and the sudoers file
Explanation
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.
[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 ~]# exitThe 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
# 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/dnfravi 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
# 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.
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 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.
# 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-apiPractice (0/3 done)
Which line in sudoers lets every member of the group "admins" run any command as any user?
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
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
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- umask and the special bits: SUID, SGID and stickyPermissions, users and sudo · same module
- File permissions: rwx, chmod and chownPermissions, users and sudo · same module
- Linux security basicsCyber Security: from zero to SOC analyst · Systems and cryptography
- Users, groups and permissionsDevOps: Git, Docker, Kubernetes and CI/CD · Linux for DevOps
- Scheduling with cron and crontabAutomation basics: scheduling, archives and shell scripts
- Inventory and ansible.cfgAnsible fundamentals
Questions people ask about this
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.
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.
Which file can only root read?Related
Answer: /etc/shadow
/etc/shadow holds the password hashes, so it is readable by root only.
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.
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
- This is the last topic of “Permissions, users and sudo”. The module exam comes next.
- Module 2 exam: Permissions, users and sudo · 14 questions · 25 min, without help. 6 topics of this module still to finish first.
- Then the next module, Processes, services and logs. 57 topics left in the course before the final exam.