8 Different Use Cases for the `doas` Command (with examples)
Run a command as root
Code: doas command
Motivation: Sometimes, certain commands require root privileges to run properly. By using doas
, you can easily execute commands as the superuser.
Explanation: The doas
command allows you to execute a specific command with root privileges. Simply replace command
with the command you intend to run as root.
Example usage: doas apt-get update
Example output: The system will update the package lists as it would when the command is run with sudo
.
Run a command as another user
Code: doas -u user command
Motivation: In some cases, you may need to execute a command as a different user to perform specific tasks that require their permissions, such as accessing files or starting applications.
Explanation: By using the -u
flag followed by the desired username, you can execute a command as that user. Replace user
with the desired username and command
with the command you want to run.
Example usage: doas -u bob touch /home/bob/test.txt
Example output: The file test.txt
will be created in Bob’s home directory.
Launch the default shell as root
Code: doas -s
Motivation: Launching a default shell as root can be handy when you need to perform multiple administrative tasks without repeatedly using doas
.
Explanation: The -s
flag allows you to launch the default shell as root, providing you with a shell with elevated privileges.
Example usage: doas -s
Example output: An interactive shell with root privileges will be launched.
Parse a config file and check if the execution of a command as another user is allowed
Code: doas -C config_file command
Motivation: Parsing a config file helps verify if the execution of a command as another user is permitted, which aids in managing security and access control.
Explanation: By using the -C
flag followed by the path to the config file and command
, you can check if the execution of the command as another user is allowed according to the configuration file.
Example usage: doas -C /etc/doas.conf touch /root/test.txt
Example output: The command will only execute if the doas.conf
file allows “touch” to be run as another user.
Make doas
request a password even after it was supplied earlier
Code: doas -L
Motivation: In certain scenarios, you may want to enforce reauthentication before executing a command, even if a password was entered earlier.
Explanation: The -L
flag causes doas
to request the user’s password even if authentication succeeded earlier. This can be useful when a certain time has passed since the initial authentication, adding an extra layer of security.
Example usage: doas -L apt-get install package
Example output: The system will prompt for password input, even if the user has previously entered a password for authentication.