Course contentsShow
← Security
Lesson 1426 of 3,05229. Operating System Security - HardeningFree lesson

Sudo Configuration and Security

Properly configuring sudoers file, command restrictions, and avoiding common sudo misconfigurations.

Sudo Configuration and Security

You'll learn how to securely configure sudo to grant elevated privileges without creating unnecessary risks or misconfigurations that attackers can exploit.

What Is Sudo Configuration?

The sudo command lets authorized users execute commands as another user (typically root). Think of sudo as a security checkpoint: instead of handing everyone the master key (root password), you give specific people a temporary pass for specific actions. The /etc/sudoers file controls who gets what access.

Core Security Principles

Never edit sudoers directly — always use visudo, which validates syntax before saving. A broken sudoers file can lock you out completely.

Restrict commands explicitly. Instead of granting broad access like ALL=(ALL) ALL, specify exactly which commands a user needs:

analyst ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart nginx

This allows only the nginx restart command, nothing else.

Avoid dangerous wildcards. Configurations like /bin/* seem convenient but let users run any binary in that directory, including shells or editors that can spawn root shells.

Require passwords unless there's a strong operational reason. NOPASSWD removes a critical authentication layer.

Use aliases for maintainability. Group similar commands or users together:

Cmnd_Alias BACKUP_CMDS = /usr/bin/rsync, /usr/bin/tar
User_Alias BACKUP_TEAM = alice, bob
BACKUP_TEAM ALL=(ALL) BACKUP_CMDS

Common Misconfigurations to Avoid

  • Allowing text editors (vim, nano) with sudo — users can escape to a root shell
  • Permitting shell commands or interpreters (bash, python)
  • Using !authenticate or !root incorrectly
  • Overly broad file paths or wildcards

Audit and Monitor

Regularly review your sudoers configuration and check /var/log/auth.log for sudo usage patterns and potential abuse.

Key Takeaway: Properly configured sudo balances operational needs with security by granting minimal necessary privileges through explicit command restrictions, validated with visudo, while avoiding wildcards and dangerous commands that enable privilege escalation.