Skip to content

Configure Least-Privilege sudo Access Safely

Admin
October 19, 2026
14 min read
Beginner
💻 Target OS: Ubuntu 24.04+ / Debian 12+ / RHEL 9+

Before you begin

  • Applies to:A disposable Ubuntu or RHEL-family VM that runs systemd and has a patched, supported sudo package.
  • Where to start:Start in the VM with an administrator account that can already use sudo.
  • What you need:Have a separate standard local account with a usable password and no other admin grants. The example uses ccoperator. Replace the name consistently if needed.
Guide tools
Guide tools
0/0 Steps (0%)

This lab grants an existing standard account permission to restart exactly one service and inspect its state. It uses a root-owned service that runs /usr/bin/true, so the restart has no application workload to disrupt. You will validate the complete sudo policy, test permitted and denied requests, and remove the lab rule.

Use a disposable Ubuntu or RHEL-family VM running systemd, a patched supported sudo package, an administrator account with working sudo access, and a separate standard local account named ccoperator. Create that account using your approved onboarding procedure or the users and permissions guide. It needs a usable login password for the password-authenticated tests and must have no other administrative grants. Replace the username consistently if you use another approved account.

A standard account requests an exact command; sudo policy authorizes it, then a root-owned lab service runs.

01

Check the Host and Existing Privileges

Preflight

Run these checks from the administrator session. Confirm systemd is process 1, locate the actual command path, and review the test account’s existing rules. The examples require /usr/bin/systemctl and the normal /etc/sudoers policy path. Stop if local paths differ until every rule and test has been adjusted.

Terminal window
ps -p 1 -o comm=
sudo --version | head -n 1
command -v systemctl
getent passwd ccoperator
id ccoperator
sudo -l
sudo -l -U ccoperator
❯ Illustrative Output (Results Vary)
systemd
Sudo version 1.9.x
/usr/bin/systemctl
02

Confirm the Policy Include and Baseline Syntax

Validate First

Check the full policy before adding anything. Inspect the main file in a privileged editor or pager and confirm that it includes /etc/sudoers.d. Both modern @includedir and legacy #includedir forms may appear; the latter is an active directive. Do not modify an organization-managed include structure without its change process.

Terminal window
sudo visudo -c
sudo grep -nE '^[[:space:]]*(@includedir|#includedir)[[:space:]]' /etc/sudoers
sudo ls -ld /etc/sudoers.d
03

Check That the Lab Names Are Unused

Avoid Collisions

Inspect both the unit search result and intended policy filename. If a unit, alias, mask, drop-in or rule already exists with these names, stop and choose different names throughout the guide. A failed systemctl cat for a nonexistent unit is expected at this stage.

Terminal window
systemctl cat configcorner-sudo-lab.service
sudo ls -l /etc/systemd/system/configcorner-sudo-lab.service \
/etc/sudoers.d/configcorner-lab
04

Create the Root-Owned Harmless Service

Lab Service

Using a privileged editor, create the unit below at the checked path. Set root ownership and mode, reload the unit definitions, and start it once. The service remains active after its no-op command finishes. It is not enabled for boot.

Terminal window
sudoedit /etc/systemd/system/configcorner-sudo-lab.service
/etc/systemd/system/configcorner-sudo-lab.service
[Unit]
Description=ConfigCorner sudo delegation lab
[Service]
Type=oneshot
ExecStart=/usr/bin/true
RemainAfterExit=yes
Terminal window
sudo chown root:root /etc/systemd/system/configcorner-sudo-lab.service
sudo chmod 0644 /etc/systemd/system/configcorner-sudo-lab.service
sudo systemd-analyze verify /etc/systemd/system/configcorner-sudo-lab.service
sudo systemctl daemon-reload
sudo systemctl start configcorner-sudo-lab.service
systemctl --no-pager show --property=ActiveState configcorner-sudo-lab.service
❯ Illustrative Output (Results Vary)
ActiveState=active
05

Add an Exact Command Rule with visudo

Policy Editing

Open the new extensionless filename with visudo and enter the rule shown. The first ALL matches hosts; the parenthesized root is the run-as account. PASSWD keeps password authentication. Each command includes fixed arguments. Do not add wildcards, NOPASSWD or additional command paths during this exercise.

Terminal window
sudo visudo -f /etc/sudoers.d/configcorner-lab
Terminal window
ccoperator ALL=(root) PASSWD: /usr/bin/systemctl restart configcorner-sudo-lab.service, /usr/bin/systemctl --no-pager show --property=ActiveState configcorner-sudo-lab.service
06

Validate the Complete Effective Policy

Syntax and Permissions

Set the expected ownership and mode for these distributions. Check the include and then the entire policy; a standalone include parse does not prove that the combined rules are valid. Stop and repair the edit if either check fails. Syntax validation cannot determine whether a grant is appropriate for the organization.

Terminal window
sudo chown root:root /etc/sudoers.d/configcorner-lab
sudo chmod 0440 /etc/sudoers.d/configcorner-lab
sudo visudo -cf /etc/sudoers.d/configcorner-lab
sudo visudo -c
sudo -l -U ccoperator
07

Test the Allowed Request in a Separate Login

Positive Test

Log in as ccoperator through the console or your approved login method. Confirm identity before testing. Clear any cached sudo authentication, enter that account’s password when prompted, then run exactly the allowed commands. The restart should exit successfully and the state query should report active.

Terminal window
whoami
sudo -k
sudo -l
sudo /usr/bin/systemctl restart configcorner-sudo-lab.service
printf 'restart exit status: %s\n' "$?"
sudo /usr/bin/systemctl --no-pager show --property=ActiveState configcorner-sudo-lab.service
❯ Illustrative Output (Results Vary)
ccoperator
restart exit status: 0
ActiveState=active
08

Check Denials Without Executing Extra Commands

Negative Test

Use sudo’s command-listing mode to ask whether different requests are authorized. These checks do not run the service stop or id command. With only the lab grant, both checks should be denied with a nonzero exit status. Any unexpected permission indicates another rule or a mistake in the grant; investigate before proceeding.

Terminal window
sudo -l /usr/bin/systemctl stop configcorner-sudo-lab.service
printf 'stop authorization exit status: %s\n' "$?"
sudo -l /usr/bin/id
printf 'id authorization exit status: %s\n' "$?"
09

Review Authentication and Authorization Evidence

Audit

Return to the administrator session. Review recent sudo records in the journal and match the test user, command and result. Logging destinations depend on the distribution and local policy; some hosts use auth.log, secure, remote syslog or a sudo log server. A permitted command listing is an authorization check, not proof that the command executed.

Terminal window
sudo journalctl --since '-15 minutes' _COMM=sudo --no-pager
sudo -l -U ccoperator
10

Remove the Lab Grant and Service

Rollback

Verify these are still the two files created for this exercise, then remove only those files. Keep the administrator session open, validate policy again, and repeat the authorization query as ccoperator. Existing grants remain in effect; do not delete the account or unrelated rules.

Terminal window
sudo cat /etc/sudoers.d/configcorner-lab
sudo systemctl cat configcorner-sudo-lab.service
sudo rm -- /etc/sudoers.d/configcorner-lab
sudo visudo -c
sudo systemctl stop configcorner-sudo-lab.service
sudo rm -- /etc/systemd/system/configcorner-sudo-lab.service
sudo systemctl daemon-reload
sudo -l -U ccoperator

Use the installed sudoers(5), sudo(8) and visudo(8) manuals for the deployed version. Upstream references: sudoers policy syntax, safe editing with visudo, and sudo invocation. For domain identities, see RHEL and Active Directory integration.

Comments