doas

On this page 7

doas is a minimal privilege escalation utility that lets an authorized user run a command as another user, most often as root, according to rules in the file /etc/doas.conf. It originated in OpenBSD in 2015 as a deliberately small replacement for sudo, and on Linux it is available mainly through the OpenDoas port, which Alpine Linux and several other distributions package.

In terms of Linux permissions, doas fills the same role as sudo: it is a setuid root program that checks who is calling, consults its rules, authenticates the user and then executes the requested command with the target user identifier. Its defining property is simplicity by design, which reduces the code that runs with full privileges and therefore the attack surface.

How doas grants elevated privileges

doas works by being installed with the setuid bit owned by root, so it starts with root privileges regardless of who runs it. It then reads /etc/doas.conf, finds the rule that applies to the calling user, the target user and the requested command, and either refuses, asks for a password or executes the command directly.

Authentication uses the caller’s own password, not the root password, which is the same model as sudo and the opposite of su. On Linux, OpenDoas can authenticate through PAM or through the shadow password file, depending on how it was built. After success, doas sets the user and group identifiers, prepares a reduced environment and replaces itself with the target command.

The basic invocation is doas command, while doas -u postgres psql runs a command as a non-root user and doas -s starts a shell. The option -n makes doas fail instead of prompting, which is useful in scripts, and -L clears a remembered authentication before the timeout expires.

The doas.conf rule syntax

Every line of doas.conf is a rule in a fixed grammar: an action, optional options, an identity, an optional target user and an optional command with arguments. Identities are user names or group names prefixed with a colon, such as :wheel. The most important evaluation rule is that the last matching rule wins, so specific rules belong after general ones.

The following table explains the elements of a rule.

ElementMeaningExample
permit or denyAllows or forbids the matching requestpermit :wheel
IdentityA user name or a group prefixed with a colonalice or :admin
as targetRestricts which user may be assumedpermit bob as postgres
cmd commandRestricts the rule to one commandcmd systemctl
args listRestricts exact arguments; empty means noneargs restart nginx
Elements of a doas.conf rule

Options modify how a permitted rule behaves. The table lists the options supported by OpenBSD doas and OpenDoas.

OptionEffectSecurity consideration
nopassRuns without asking for a passwordUse only for narrowly restricted commands
persistRemembers authentication for a short periodConvenient, but extends the window of misuse
keepenvKeeps the caller’s environment variablesCan pass unsafe variables to root
setenv { }Sets or removes specific variablesSafer than keeping the full environment
nologSuppresses logging of the executionReduces the audit trail
Rule options and their effect

A typical workstation configuration is a single line, permit persist :wheel, while a server may add permit nopass backup as root cmd /usr/local/bin/backup.sh for an automated task. A configuration file can be validated with doas -C /etc/doas.conf, which reports syntax errors and, given a command, shows which rule would match.

doas compared with sudo and su

doas differs from sudo mainly in scope and complexity: sudo supports host aliases, command aliases, LDAP-backed policies, plugins, session recording and detailed per-command defaults, whereas doas supports only the small rule set described above. Its source code is a small fraction of the size of sudo, which makes it easier to audit.

Propertydoassudosu
Password requestedCaller’s passwordCaller’s passwordTarget user’s password
Configuration file/etc/doas.conf/etc/sudoers, edited with visudoNone, uses PAM
Per-command rulesYes, with exact argumentsYes, with wildcards and aliasesNo
Central policy and pluginsNoYes, including LDAP and pluginsNo
Default on major distributionsAlpine and some minimal systemsMost Debian, Ubuntu and Fedora systemsUniversally available
Comparison of privilege escalation tools on Linux

polkit addresses a different problem: it authorizes specific actions requested over D-Bus by desktop services, not arbitrary command execution, so it complements doas rather than replacing it.

Choosing and deploying doas

doas is a good choice for small, single-administrator systems, containers, embedded devices and minimal distributions where sudo’s features are unused. It is a weaker fit for organizations that need centralized policy, detailed audit logs or integration with directory services.

The decision table summarizes typical situations.

SituationRecommended toolReason
Personal Alpine or Arch workstationdoasSimple rules cover all needs
Minimal container image needing root stepsdoas or no escalationSmaller code and fewer dependencies
Enterprise fleet with LDAP accountssudoCentral policy and session recording
Desktop actions such as mounting drivespolkitFine-grained authorization for services
When doas is an appropriate choice

When migrating, administrators should keep a working root path until the new configuration is tested, for example an open root session or console access. The config file should be owned by root and set to mode 0400 or 0600, because a world-writable rule file would let any user grant themselves root.

Limitations and common mistakes

  • Rule order errors: a broad deny placed after a specific permit silently overrides it, because the last match wins.
  • Overusing nopass: a passwordless rule without cmd effectively gives the account unrestricted root access.
  • Assuming sudo compatibility: scripts that call sudo options such as -E or rely on sudoers defaults need adjustment.
  • Persist differences on Linux: OpenDoas implements persist with timestamp files, and some builds disable it, so behavior can differ from OpenBSD.
  • Limited logging: doas logs to syslog but offers no session recording, which may not meet compliance requirements.

Put simply

doas is like a short guest list at the door of the administrator’s room. The list says who may go in and, if needed, exactly what they may do there. A person on the list confirms their identity with their own password, and the door opens.

The list is short and easy to read, which is the point. Fewer rules mean fewer mistakes, and a smaller program means fewer places where something can go wrong.

In practice

A developer runs a small home server on Alpine Linux with two accounts: her own and a service account called backup. She installs doas and writes a two-line configuration. The first rule, permit persist :wheel, lets her administer the system after entering her password once every few minutes.

The second rule, permit nopass backup as root cmd /usr/local/bin/snapshot.sh, lets the nightly backup job run exactly one script as root without a password. She checks both rules with doas -C /etc/doas.conf before logging out. The result is a configuration she can read in five seconds, and a compromised backup account still cannot open a root shell, because no rule permits anything except that single script.