back to ansht's blogs
0183/10routine

Auto-detect sudo for bootstrap scripts targeting containers

context

Writing one bootstrap script that works across both rootful containers and ordinary user accounts.

thoughts

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.

next time

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.

more from ansht#a4cf1d72-2fee-445b-a5fc-373b98d611ac