A single typo in /etc/sudoers can break sudo for everyone, and if root login is disabled you are then locked out of admin entirely. That is why you never open it in a plain editor. Always use visudo, which checks the syntax before it lets you save:
sudo visudoIf you make a mistake, visudo refuses to write the broken file and offers to re-edit. That safety check is the entire point.
Use drop-in files, not the main file
Modern sudo reads everything in /etc/sudoers.d/, so the clean pattern is one small file per rule rather than editing the monolith:
sudo visudo -f /etc/sudoers.d/deployDrop-ins are easier to manage, survive package upgrades that rewrite /etc/sudoers, and are trivial to remove (just delete the file). Use -f so visudo still syntax-checks the drop-in.
The rule format
# user host=(runas) commands
deploy ALL=(ALL) ALL
deploy ALL=(ALL) /usr/bin/systemctl restart nginx
%webops ALL=(ALL:ALL) /usr/bin/systemctl * nginx
- user / %group who the rule is for (
%marks a group). - host where it applies (
ALL= everywhere; matters only on shared sudoers across machines). - (runas) which identities they may act as (
(ALL)= any user, including root). - commands the exact allowed commands, or
ALL.
Always use absolute paths for commands (/usr/bin/systemctl, not systemctl); a bare name can be satisfied by a malicious binary earlier on PATH.
Validate without editing
To check the whole sudoers configuration (main file plus all drop-ins) for errors, for example after writing a file from configuration management:
sudo visudo -cIt prints parsed OK or names the file and line of the problem. Run it in CI or after any automated change.
Keep a safety net
Before tightening sudo rules, keep a second root-capable session open, exactly as with disabling root login. If a rule change locks you out and root login is off, recovery means single-user mode or a rescue boot. A spare session costs nothing and saves the afternoon.
FAQ
See also
- How to give a user sudo access: the rules you are editing here.
- How to set up passwordless sudo: a common (and risky) drop-in rule.
- How to run a command as another user (sudo -u): the runas field in action.
- How to disable root login: why a sudo mistake can lock you out.
Sources
Authoritative references this article was fact-checked against.





