doas Configurationdoas is a utility for executing commands as another user, primarily designed as a simpler
alternative to sudo. It is the default privilege escalation tool in MidnightBSD and is
included in the base system. doas allows authorized users to run commands as root or other
users without requiring the root password.
The doas command is particularly useful for system administration tasks where regular users
need to perform specific administrative functions without full root access.
doas is the OpenBSD approach to privilege escalation and has several advantages over
sudo:
Unlike sudo, doas does not have extensive logging capabilities by default, but it
provides the essential functionality needed for most use cases in a BSD environment.
doas is included in the MidnightBSD base system, so no additional installation is required.
It is available as a command and does not require any service to be started.
To verify that doas is available:
$ which doas
This should return /usr/bin/doas.
If doas is not found, ensure that the base system is installed correctly. doas is part of
the standard MidnightBSD installation.
The main configuration file for doas is /etc/doas.conf. If this file does not
exist, you need to create it. The configuration file uses a simple syntax:
# permit|deny [options] identity [as targetuser] [cmd command [args...]]
Where:
permit - Allows the specified actiondeny - Explicitly denies the specified actionidentity - The user or group who is allowed to execute the commandtargetuser - The user as whom the command should be executed (defaults to root)cmd - The command and arguments that are allowedCreate the configuration file if it doesn't exist:
# touch /etc/doas.conf
Always set secure permissions on the configuration file:
# chmod 640 /etc/doas.conf
# chown root:wheel /etc/doas.conf
To allow user john to run any command as root:
permit john as root
To allow user john to run only specific commands as root:
permit john as root cmd service permit john as root cmd ifconfig permit john as root cmd mport
By default, doas will prompt for the user's password. To allow a user to run commands without
a password prompt:
permit nopass john as root cmd service
To allow all users in the wheel group to run any command as root:
permit :wheel as root
To allow user john to run service with specific arguments:
permit john as root cmd service args start permit john as root cmd service args stop permit john as root cmd service args restart
To allow user john to run commands as user backup:
permit john as backup cmd rsync
You can explicitly deny certain commands even if other rules would permit them:
deny john as root cmd shutdown
You can preserve or modify the environment when using doas:
permit john as root cmd ee env HOME permit john as root cmd mport env PATH
Use wildcards to match multiple commands or paths:
permit john as root cmd service * permit john as root cmd mport *
Combine multiple rules for different users and commands:
permit john as root cmd service permit john as root cmd mport permit jane as root cmd git permit :wheel as root cmd ifconfig
After editing the configuration file, test it by having the user attempt to run a permitted command:
$ doas ls /root
The command should execute if the configuration is correct.
Always follow the principle of least privilege - only grant the minimum permissions necessary for users to perform their tasks.
/etc/doas.conf is only readable and writable by rootdoas permissions and what they can executeImportant security notes:
vi or ee as root, they may be able to edit system files and gain elevated privilegessh, bash, tcsh) as root, as this effectively gives the user a root shellfind -exec, xargs)This error typically occurs when:
doas.conf fileSolution: Verify the configuration file exists, has correct permissions, and contains the appropriate rules.
This indicates that doas cannot find its configuration file. Ensure that /etc/doas.conf exists.
This error can occur if the user's password is incorrect or if the command being attempted is not explicitly permitted.
You can test doas configuration by running commands as the target user:
$ su - john $ doas whoami
Check that the configuration file syntax is correct and that the file permissions are set properly.
If doas is not working at all, verify it's installed:
# which doas # doas -V # Check version