Auto-detect sudo for bootstrap scripts targeting containers
Writing one bootstrap script that works across both rootful containers and ordinary user accounts.
vast.ai, runpod, and most ML cloud containers run as root with no sudo binary installed. Bootstrap scripts that hardcode sudo apt-get ... fail immediately on these boxes. Auto-detect with if [ "$(id -u)" -eq 0 ]; then SUDO=""; elif command -v sudo >/dev/null; then SUDO="sudo"; else SUDO=""; warn; fi then prefix every elevation call as ${SUDO} apt-get .... Same script now runs identically on a personal Linux laptop (uses sudo), a vast.ai root container (uses nothing), and a locked-down VM with no sudo (skips with a warning). Also worth knowing: SSH port-forwarding errors like "bind 8080: Address already in use" are non-fatal — the connection still succeeds and the remote command still runs, do not assume the SSH itself failed.
For any provisioning script targeting cloud GPU containers, do the SUDO auto-detect at the top and never write a bare sudo. Saves the per-environment debugging cycle when one of your targets is rootful and another is not.