Skip to main content
Privilege escalation allows a non-root user to perform tasks requiring superuser rights. Instead of enabling direct root logins—which poses security risks—you can delegate specific commands to trusted users via sudo. This approach enforces the principle of least privilege and keeps your system secure.

Why Use sudo?

  • Grants temporary elevated rights without sharing the root password
  • Provides an audit trail of executed commands
  • Limits users to only the commands they need

Attempting a Restricted Operation

Without sudo, installing packages fails:

Elevate with sudo

Prepend sudo, authenticate with your own password, and the command succeeds:
If you see User michael is not in the sudoers file, add your user to the sudo group or update /etc/sudoers accordingly.

Configuring sudo: /etc/sudoers

All sudo policies live in /etc/sudoers and included files under /etc/sudoers.d/. Always edit with visudo to prevent syntax errors:
Here’s a sample excerpt:
Never edit /etc/sudoers with a regular text editor. Syntax errors can lock out all sudo access. Always use visudo.

Best Practices for sudo Configuration

  • Grant only the commands necessary for a task
  • Use group-based rules to simplify management
  • Avoid NOPASSWD unless automation requires it
  • Keep custom rules in /etc/sudoers.d/ for modularity

Hands-On Exercises

  1. Create a test user:
  2. Add the user to the sudo group:
  3. Switch to bob and install a package:
  4. Customize a rule in /etc/sudoers.d/custom_rules to allow bob to restart services without a password.

Watch Video