How do you keep NOPASSWD sudo rules narrow for automation?

November 18, 2026 5 answers
Asked by _d69~socketmaraca.4

I want automation to run a few privileged tasks without granting it unrestricted sudo. Argument wildcards and overlapping rules seem easy to get wrong. What approaches work well for keeping the access limited?

Answers

c.861~plovergauge

For a small set of fixed tasks, list the exact command paths and arguments, then test both the intended calls and calls that should be denied. Review the whole matching policy, not just the rule you added. If multiple entries match, the last match wins, even when an earlier entry is more specific.

Be especially careful with argument wildcards. A star can match spaces, so a rule allowing /bin/cat /var/log/messages* can also allow an extra file argument. Recent sudo versions support regular expressions, which can give you tighter matching, but check the version and test the expression on the target system.

_d69~socketmaraca.4:

Good point about testing denied calls too. A rule that looks specific isn't much help if another matching entry overrides it.

Reply to this answer
_m79!zavlino.3

A root-owned wrapper can be easier to review than a complicated sudoers expression. Have it perform one specific task, accept no arguments if possible, and grant sudo access only to that wrapper.

The wrapper becomes part of your security boundary, though. Make sure the automation account cannot modify it or the files it relies on, and be cautious about inputs and other commands it invokes. A wrapper doesn't make a broad operation safe by itself.

Reply to this answer
Trelvani+8x

For log reading, skip sudo if you can. Give a dedicated log-reading group access through file permissions or ACLs, and check that rotated logs get the intended permissions too. That grants access to the files rather than to a root command.

Reply to this answer
5z~tumblevisor_738

Sometimes the cleanest sudo rule is no rule at all. Centralized logs and telemetry can remove a lot of read-only access needs. For changes, configuration management or a carefully scoped systemd unit may be a better fit, depending on the task and who can change its configuration.

Reply to this answer
Rovdexa-6n

There is also the pragmatic option of giving a dedicated automation account broad sudo when the automation is already trusted to administer the whole machine. That is a different security decision, not a narrow rule. Compromise of that account can mean full control of the host, so test changes outside production and be explicit about that tradeoff.

6v~gimbalwrench_529:

That makes sense for a fully privileged management account, but it doesn't meet the goal when the account only needs a few tasks.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions