Editing /etc/sudoers directly with nano or vim works, until the day a syntax error breaks sudo for everyone, including the person who just made the mistake: without a working sudo, fixing the file that broke sudo requires root access already available through some other means, a situation that doesn’t always exist.
What visudo does differently
visudo opens the sudoers file in an editor, exactly like any regular editor, but validates its syntax before writing the actual file: a syntax error blocks the save and offers to fix it, never letting an invalid sudoers file replace the working one.
# Validates syntax before writing,
# unlike a regular editor on the file directly
visudo
# >>> /etc/sudoers: syntax error near line 42 <<<
# What now? Options are:
# (e)dit sudoers file again
# e(x)it without saving changes to sudoers file
This validation eliminates the most dangerous incident class for this particular file: a broken sudo for the entire system, potentially unrecoverable without physical or out-of-band console access to the machine.
The trap of an overly broad NOPASSWD
NOPASSWD in a sudoers entry allows a command to run without re-prompting for a password, a convenience that becomes a real security risk the moment it applies to too broad a scope.
# Broad scope: any command, no password,
# defeats the entire point of requiring sudo authentication
myuser ALL=(ALL) NOPASSWD: ALL
# Narrow scope: one specific command, no password,
# everything else still requires normal authentication
myuser ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart my-app
A NOPASSWD: ALL granted for convenience (often for an automation script that only needs to run one command) removes the authentication barrier for absolutely everything, far beyond the original need, a drift that rarely survives a security audit once discovered — the same least-privilege principle that applies to Kubernetes RBAC applies just as much to a sudoers file.
What Defaults directives silently change
Defaults lines in the sudoers file change sudo’s global behavior, often without their effect being obvious from a plain reading.
# Preserves certain environment variables across sudo,
# a behavior not guaranteed by default depending on distro
Defaults env_keep += "HOME EDITOR"
# Forces a specific binary search path,
# independent of the invoking user's own PATH
Defaults secure_path = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
secure_path in particular prevents a user with a modified personal PATH (pointing to a malicious binary named after a common system command) from getting that binary executed with sudo privileges, a silent but real protection against a PATH-hijacking attack class.
Takeaway
visudo validates the sudoers file’s syntax before writing it, eliminating the most dangerous risk for this particular file: a broken sudo for the entire system with no fallback access to repair it. A NOPASSWD granted at too broad a scope (ALL rather than a specific command) defeats the entire point of sudo authentication, a common drift born from a one-off automation need. Defaults directives like secure_path silently change sudo’s behavior in ways that close entire attack classes, settings that stay relevant regardless of how much infrastructure abstraction sits above the machines actually running this file.