2

I have a bash script that will only work with root privileges, so I want to test whether the user has them. Other posts (see below) ask and answer how to know whether the user is actually running as root, but not whether the script has root privileges. These posts say to test whether $EUID is 0.

To try this idea in the context of sudo, I wrote a bash script /tmp/a.sh:

#!/bin/bash
echo $EUID

The following two commands were run as a non-root user with sudo privileges on Ubuntu 16.04. If the $EUID suggestion worked in the context of sudo, the second command would have printed 0 instead of a blank line.

$ /tmp/a.sh
1000
$ sudo /tmp/a.sh

$ 

FYI, an example of the related posts I am referencing is:

How can a script check if it's being run as root?

3
  • 1
    echo $UID should work. Or according to David F in the comments of your linked answer you should use echo $(id -u)
    – Terrance
    Jan 30, 2019 at 18:32
  • Thanks Terrance, that works too. I posted an answer after noticing a fat-finger that causes $EUID and $UID to resolve properly only for non-root users (still a bit of a mystery). Jan 30, 2019 at 18:43
  • There are multiple other solutions to detect script running as root on the question to which you referred, if that's really what you're asking. If you are asking specifically why $EUID doesn't work, please edit the question to clarify the title and the body of the question, and we can re-open it Jan 31, 2019 at 0:58

2 Answers 2

1

The script /tmp/a.sh only works with #!/bin/bash on the first line. When actually running the example I gave, the ! was accidentally omitted and the only user reported was the non-root user as shown in the question.

4
  • 2
    Probably because without the shebang, the script was interpreted by your current shell (likely bash) when called without sudo, but was interpreted by dash (which doesn't set EUID) when called with sudo Jan 30, 2019 at 18:49
  • @steeldriver Confirmed. On my Ubuntu 18.04 virtual machine the script with ls -l /proc/$$/exe reports the script is run via /bin/dash, however on my other debian-based system root has bash as login shell and that's what is reported there. So sudo checks either $SHELL environment variable or /etc/passwd or other login system such as LDAP Jan 31, 2019 at 1:14
  • @SergiyKolodyazhnyy it's kind of interesting that sudo appears to use the login shell specified in the target user's passwd entry even when not explicitly invoked with the -i (or --login) option - I'd never really thought about that before Jan 31, 2019 at 14:22
  • @steeldriver Correction: it defaults to /bin/sh . I've repeated the experiment couple times again: /proc/19051/exe -> /bin/dash is reported on both systems, so previous conclusion is mistaken. Feb 1, 2019 at 0:34
0

I use the following in some of my scripts:

#!/bin/bash

if [ "$(id -un)" != "root" ]; then
    echo "Need root - sudoing..."
    exec sudo "$0" "$@"
fi
# Now we are root

Not the answer you're looking for? Browse other questions tagged .