Skip to content

Instantly share code, notes, and snippets.

@jvarn
Last active March 8, 2026 12:21
Show Gist options
  • Select an option

  • Save jvarn/5ee517575576a7d063c0f4821a96296e to your computer and use it in GitHub Desktop.

Select an option

Save jvarn/5ee517575576a7d063c0f4821a96296e to your computer and use it in GitHub Desktop.
Dummy sudo for proot-distro
#!/bin/bash
# -----------------------------------------------------------------------------
# sudo.sh
#
# Description:
# A lightweight "dummy" sudo wrapper designed for proot-distro, Termux, or
# containerized environments where the standard sudo binary is missing.
#
# Behaviour:
# - If running as root (EUID 0): Executes commands immediately and silently.
# - If running as a regular user:
# 1. Prompts to continue with current user permissions.
# 2. If declined, offers to attempt execution via Termux native sudo.
#
# Rationale:
# Facilitates running scripts that hardcode 'sudo'. Provides a bridge between
# the proot guest environment and the Termux host sudo (for rooted devices).
#
# Installation:
# 1. Save this file to /usr/local/bin/sudo
# 2. Make it executable: chmod +x /usr/local/bin/sudo
# 3. (Optional) Map Termux sudo if available:
# ln -s /data/data/com.termux/files/usr/bin/sudo /usr/local/bin/sudo_termux
#
# Author: Jeremy Varnham https://github.com/jvarn
# License: MIT
# -----------------------------------------------------------------------------
TERMUX_SUDO="/data/data/com.termux/files/usr/bin/sudo"
# 1. If already root, execute silently
if [ "$EUID" -eq 0 ]; then
if [ $# -eq 0 ]; then exit 0; fi
exec "$@"
fi
# 2. First Prompt: Current User
echo "--------------------------------------------------------"
echo "WARNING: 'sudo' called by $(whoami)"
echo "Command: ${*:-[Privilege Check]}"
echo "--------------------------------------------------------"
read -p "Continue with current user permissions? (y/N): " confirm
if [[ "$confirm" =~ ^[yY](es)?$ ]]; then
# Handle flags/empty calls
if [ $# -eq 0 ] || ([[ "$1" == -* ]] && [ $# -eq 1 ]); then
echo "Privilege check validated."
exit 0
fi
exec "$@"
else
# 3. Second Prompt: Alternative methods
echo -e "\nPermission declined for current user."
echo "1) Abort and exit"
echo "2) Attempt via Termux native sudo (Requires Rooted Device)"
read -p "Select an option (1/2): " choice
case "$choice" in
2)
if [ -f "$TERMUX_SUDO" ]; then
echo "Handing over to Termux sudo..."
exec "$TERMUX_SUDO" "$@"
else
echo "Error: Termux sudo not found at $TERMUX_SUDO"
exit 1
fi
;;
*)
echo "Exiting: Sudo command cancelled."
exit 1
;;
esac
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment