Run as root on Linux

Written by on .

Run a script or application as root (privileged). This is a neat way to do it, including a graphical user interface depending on which desktop environment is installed, and whether a DISPLAY is available or not.

if [ "$(whoami)" != "root" ] then if [ -n "$DISPLAY" ] then command -v gksu >/dev/null 2>/dev/null && exec gksu "$0" "$@" command -v kdesu >/dev/null 2>/dev/null && exec kdesu "$0" "$@" command -v sux >/dev/null 2>/dev/null && exec sux "$0" "$@" fi exec sudo "$0" "$@" fi