sudo access on Linux, manage entries in /etc/sudoers, and apply fine-grained policies for different users and groups.
Using sudo
By default, only the root (superuser) can modify system-critical files and settings. Prefixing a command with sudo elevates it to root privileges:
When running
sudo for the first time, you’ll be prompted for your password—not the root password.Granting sudo via the wheel group
Many Linux distributions allow members of the wheel group to use sudo:
trinity) to wheel:
trinity can execute any command with sudo, which is easy but lacks fine control.
Fine-grained control with /etc/sudoers
Instead of a broad group assignment, define precise policies in /etc/sudoers. Never edit that file directly! Always use visudo, which validates syntax.
A malformed
/etc/sudoers can lock out all sudo access. Always use visudo to edit safely.Breakdown of a sudoers entry
| Part | Description | Example |
|---|---|---|
| User/Group | Rule applies to this user (e.g., trinity) or group (%devs) | trinity%developers |
| Host | Hosts where the rule is valid (ALL for every host) | ALL |
| Run as | User(s) the command may run as (in parentheses) | (ALL), (aaron,john) |
| Commands | Which commands are allowed | /bin/ls, /usr/bin/vim |
Defining custom sudoers policies
Below are sample entries to append near the end of /etc/sudoers via visudo:
Running commands as another user
Beyond root, you can invoke commands as any user:Handling “Permission denied” errors
If a user invokes a disallowed command, sudo reports:Disabling the password prompt
To let a user run commands without entering their password, addNOPASSWD::
Use
NOPASSWD: sparingly; it increases convenience but may reduce auditability.