14.4 Privilege Escalation Tools
sudo and doas define privilege rules through configuration files, enabling specific users to execute privileged operations without the root password, and provide operation logging.
Thought Question
Even administrators should restrict their own privileges when privileged operations are not needed.
What do you think are the disadvantages of this privilege restriction design?

doas
doas is a command-line tool ported from OpenBSD that can serve as an alternative to sudo in Unix-like systems.
The author of doas is Ted Unangst, and it was first introduced with OpenBSD 5.8. Users can execute commands with elevated privileges (typically as root) through doas. Unlike sudo, doas emphasizes configuration simplicity and security, focusing on streamlined privilege delegation and avoiding complex configuration options.
When using it, sudo can be directly replaced with doas; the two are functionally equivalent in basic usage scenarios. doas supports the following options:
nopass
Execute without password
keepenv / setenv
Environment variable control (inherit or set)
args
Command argument restriction
nolog
Do not log
persist
Password caching (only supported by Port security/opendoas)
doas does not have sudo's plugin architecture and LDAP integration or other advanced features.
Installing doas
Install using pkg:
Or install using Ports:
Using doas for Shared Privilege Management
User user2 only needs to create the file /usr/local/etc/doas.conf and write:
This allows user user2 to obtain root privileges via doas without entering a password. If password-free doas is not needed, simply remove nopass.
Warning
The
nopassoption, while convenient, poses a security risk: if the user account is compromised, the attacker can gain root privileges without any additional credentials. It is recommended to usenopassonly in trusted single-user desktop environments; multi-user or server environments should require password authentication.
For wheel group users, write the following line:
After installing and configuring doas, privileged commands can be executed:
doas Example Configuration File
The example configuration file is located at /usr/local/etc/doas.conf.sample.
Brief annotations are as follows:
sudo
The original authors of sudo are Bob Coggeshall and Cliff Spencer (original version, circa 1980), and the primary rewriter and maintainer is Todd C. Miller.
sudo's sudoers configuration syntax has a high degree of complexity, with the manual page exceeding 1000 lines, making it prone to configuration errors that can lead to security vulnerabilities; timestamp files are located in /var/run/sudo/ts/, and if the file system is full, they may not work properly; sudo -i may not fully load environment variables in some shell environments.
sudo allows administrators to configure stricter system command access and provides logging functionality.
Installing sudo
Install using pkg:
Or install using Ports:
Using sudo for Shared Administration
The sudo configuration file consists of several sections and supports deep customization.
Configuring Regular Users to Use sudo
Unconfigured regular users executing sudo will receive the error xxx Is Not in the Sudoers File. This Incident Will Be Reported.
A line needs to be added to the sudoers configuration file to resolve this issue. Edit the /usr/local/etc/sudoers file, find the line root ALL=(ALL:ALL) ALL, and add a line below it:
Then save and exit. The user "actual_regular_user" will be able to use sudo; when prompted for a password, enter that user's password, not the root user's password.
Granting Specific Users Permission to Execute Specific Commands
In the following example, web application operations staff user1 needs to start, stop, and restart the web application webservice. To grant this user the relevant permissions, add a new line at the end of the /usr/local/etc/sudoers file:
Then user user1 can start webservice via the following command:
The above configuration only authorizes a single user (user1) for the webservice service, but most organizations have a Web team responsible for related management. Only one line of configuration is needed to grant permissions to an entire group. The following steps will first create a web group, then add users to it, and ultimately enable all group members to have the ability to manage the service:
Then use the pw command to add the required user user1 to the webteam group:
Finally, write the following line in the /usr/local/etc/sudoers file to allow all members of the webteam group to manage webservice:
sudo Password-Free for Regular Users and Groups
Users authorized to use sudo only need to enter their own password to complete authentication. Compared to su, the advantage of sudo is that su requires entering the root password and grants full root privileges, while sudo allows fine-grained privilege control per user or group. To allow webteam group members to manage the service without a password, change to:
Similarly, if you want user user1 to be password-free, change to:
If you want users in the wheel group to also be password-free, then:
The percent sign % indicates a group; without % it indicates a regular user.
sudo-rs
sudo-rs is an implementation of sudo and su written in Rust, security-oriented with memory safety.
Non-coexistence Installation with sudo
Before installing sudo-rs, sudo must be uninstalled first; the two cannot coexist under this approach.
Install using pkg:
Or install using Ports:
Provides sudo, visudo, and sudoedit commands.
visudo edits the sudoers file in a secure manner, similar to vipw(8). It locks the sudoers file to prevent simultaneous editing, performs basic validity checks, and checks for syntax errors before installation. If syntax errors are found, it displays the line number and prompts "What now?", where the user can choose e (re-edit), x (exit without saving), or Q (force save, risky). Editor selection is determined by the SUDO_EDITOR, VISUAL, and EDITOR environment variables, defaulting to /usr/bin/vi.
FreeBSD's visudo comes from the sudo package (not the base system) and shares the same upstream source code as the Linux version, with basic compatibility.
Coexistence Installation with sudo
Both sudo and sudo-rs exist in the system simultaneously.
Install using pkg:
Or install via Ports:
Provides sudo-rs, visudo-rs, and sudoedit-rs commands.
sudo-rs Example Configuration File
The configuration file is located at: /usr/local/etc/sudoers.
Testing
Appendix: Privilege Escalation via mac_do
References:
K Rin. FreeBSD MAC Introduction[EB/OL]. [2026-03-26]. https://sandb0x.tw/a/FreeBSD_MAC_%E7%B0%A1%E5%96%AE%E4%BB%8B%E7%B4%B9. A brief Chinese introduction to the basic concepts and configuration of the FreeBSD Mandatory Access Control framework.
FreeBSD Project. mac_do(4)[EB/OL]. [2026-03-26]. https://man.freebsd.org/cgi/man.cgi?query=mac_do&sektion=4. MAC policy do module manual page.
OpenBSD. doas -- execute commands as another user[EB/OL]. [2026-04-17]. https://man.openbsd.org/doas.1. doas privilege escalation tool manual page.
Privilege Escalation Tool Configuration File Structure
The following is a summary of the configuration file paths for the privilege escalation tools introduced in this section:
Exercises
Review the source code of the
mac_domodule in FreeBSD, analyze its mechanism for implementing privilege escalation through Mandatory Access Control, and outline the fundamental differences in the privilege model compared tosudo/doas.Review the source code implementation of the
doastool in FreeBSD Ports, analyze the design of its configuration parsing, rule matching, and environment variable handling, and evaluate its trade-off strategy between simplicity and security.Modify the
keepenvoption in thedoasconfiguration, record its impact on environment variable inheritance behavior, and analyze the security risks of different environment variables (such asPATH,HOME) in privilege escalation scenarios.
Last updated