I have a bash script (x11docker) that needs to run some commands as root (docker), and some commands as unprivileged user (X servers like Xephyr). The script prompts for password at some point. It should run on arbitrary linux systems without configuring the system first.
Some systems use su, some use sudo to get root privileges.
How can I recognize which one will work? I tried sudo -l docker. That should tell me if sudo dockeris allowed. Unfortunately, it needs a password even for this information.
The point is, root may or may not have a password (that is needed to use su -c), and sudo may or may not be allowed to run docker. How to decide which one will do the job (=executing a command with root privileges)?
Checking for group sudo may be a good guess, but is not reliable, as it does not tell me if /etc/sudoers is configured to allow group sudo arbitrary root access. Also, docker can be allowed in /etc/sudoers without the user being member of group sudo.
pkexec should be a solution, but is not reliable. The password prompt fails on console, fails on X servers different from DISPLAY=:0, and fails on OpenSuse at all.
Currently, the script defaults to use su, and a switch --sudo allows to use sudo. Possible, but not nifty.
I am working on an update allowing to run the script as root at all and checking for the "real" unprivileged user with logname, SUDO_USER and PKEXEC_UID. Not nifty, too.
Is there a way to know if I should use su or sudo?