A POSIX-compliant shell script that provides sudo-like functionality by mapping commands to doas when available, or falling back to plain shell execution when doas is not installed.
This script serves as a compatibility layer that:
- With doas: Maps
sudooptions to theirdoasequivalents - Without doas: Executes commands directly using the shell (no privilege escalation)
The --help output adapts based on whether doas is available:
Mode: Using doas backend (privilege escalation enabled)
Fully supported options:
-u, -n, -s, -i, -b, -a, -C, -L, -l
Limited support with warnings:
-v, -k, -K, -E, -g, -H
Mode: Using shell backend (NO privilege escalation)
Working options:
-s, -i, -b (execute as current user)
Ignored options (warnings shown):
-u, -n, -g, -a, -C, -L, -l, -v, -k, -K, -E, -H
-h, --help- Display context-aware help message-V, --version- Display version information-s, --shell- Run shell (as target user with doas, as current user without)-i, --login- Run login shell (as target user with doas, as current user without)-b, --background- Run command in background--- End of options delimiter
-u user, --user=user- Run command as specified user (default: root)-n, --non-interactive- Non-interactive mode, fail if password required-a style- Use specified authentication style-C config- Check configuration file-L, --clear-persist- Clear persisted authentication
-v, --validate- Update cached credentials (limited support with doas)-k, --reset-timestamp- Invalidate cached credentials (limited support with doas)-K, --remove-timestamp- Remove all cached credentials (limited support with doas)-E, --preserve-env- Preserve user environment (limited support with doas)-g group, --group=group- Run with specified group (not supported by doas)-H, --set-home- Set HOME to target user's home (not implemented)-l, --list- List privileges (not implemented)
- Commands are executed through
doaswith proper privilege escalation - Options are translated to their
doasequivalents where possible - Warnings are shown for unsupported options
- Commands are executed directly using the shell without privilege escalation
- User switching options (
-u) are ignored with a warning message - The script acts as a pass-through wrapper
- Useful for development/testing environments without privilege requirements
-
Copy the
sudoscript to a directory in your PATH:cp sudo /usr/local/bin/sudo chmod +x /usr/local/bin/sudo
-
Optionally, ensure this directory appears before the system
sudo:export PATH="/usr/local/bin:$PATH"
# With doas: executes as root via doas
# Without doas: executes as current user
sudo ls /root# With doas: executes as user 'www'
# Without doas: warning shown, executes as current user
sudo -u www whoami# With doas: spawns root shell via doas
# Without doas: spawns regular shell as current user
sudo -s# With doas: fails if password required
# Without doas: warning shown, command executed
sudo -n reboot# Both modes: runs command in background
sudo -b long-running-command# Requires doas
sudo -C /etc/doas.conf ls /rootWhen using doas as the backend, configure /etc/doas.conf:
# Allow user to run commands as root without password
permit nopass username as root
# Allow user to run specific commands
permit username cmd /usr/bin/reboot
# Keep environment variables
permit keepenv username
See doas.conf(5) for complete configuration details.
When doas is not installed, no configuration is needed. Commands run with the current user's privileges.
- Not all
sudooptions are supported (this is a minimal compatibility layer) - Complex
sudofeatures like plugin architecture are not available - Credential caching is handled by
doas(limited compared tosudo)
- No privilege escalation - commands run as the current user
- User switching (
-u) is ignored - Group switching (
-g) is ignored - Authentication is bypassed
- Non-interactive mode (
-n) has no effect
| Option | With doas | Without doas |
|---|---|---|
-u user |
✅ Full support | |
-g group |
||
-s |
✅ Full support | ✅ Current user shell |
-i |
✅ Full support | ✅ Current user shell |
-n |
✅ Full support | |
-E |
✅ Environment preserved | |
-b |
✅ Full support | ✅ Full support |
-a style |
✅ Full support | |
-C config |
✅ Full support | |
-L |
✅ Full support | |
-v/-k/-K |
||
-l |
❌ Not implemented | ❌ Not implemented |
-
Shell Fallback is NOT Secure: When
doasis not available, this script provides no security - it merely executes commands as the current user. This is intentional for development/testing scenarios. -
Install doas for Production: For any system requiring privilege escalation, install and configure
doasproperly:- OpenBSD: Built-in
- NetBSD: Available as package (
pkgin install doas) - FreeBSD: Available as package (
pkg install doas) - DragonFly BSD: Available as package (
pkg install doas) - Alpine Linux: Built-in (native doas support)
- Other Linux: Available in most package managers (
apt install doas,yum install doas, etc.) - macOS: Available via Homebrew (
brew install doas)
-
Configuration Validation: Always validate your
doas.confconfiguration:doas -C /etc/doas.conf
-
Audit Trail: The
doasbackend provides logging. The shell fallback does not.
Test the script's behavior:
# Test help
./sudo --help
# Test version
./sudo --version
# Test command execution
./sudo echo "Hello World"
# Test with user specification
./sudo -u nobody id
# Test shell invocation
./sudo -sThis script is provided as-is for compatibility purposes. Use at your own risk.