4

How can I check whether a user is root or not within a BASH script?

I know I can use

[[ $UID -eq 0 ]] || echo "Not root"

or

[[ $EUID -eq 0 ]] || echo "Not root"

but if the script was invoked via fakeroot, UID and EUID are both 0 (of course, as fakeroot fakes root privileges).

But is there any way to check whether the user is root? Without trying to do something only root can do (i.e. creating a file in /)?

Fabian Damken
  • 1,477
  • 1
  • 15
  • 24
  • 1
    Why are you trying to tell the difference between fakeroot and real root? The purpose of fakeroot is to pretend to be root. Trying to distinguish them seems to be defeating that purpose. – Ryan C. Thompson Oct 30 '15 at 23:47
  • Try `[ $(logname) == "root" ] || echo "Sorry not root"`. – alvits Oct 30 '15 at 23:49
  • @RyanThompson - you are right. Functionally, we shouldn't distinguish between real root and fakeroot. However, there are cases when there's a need to know who is runnng the fakeroot. Maybe for logging and auditing purposes. Of course it might not be the same case in OPs question. – alvits Oct 30 '15 at 23:54
  • That is correct. But when I have a script that can be read by any user and they could execute (or at least read) the script, I have to check whether the script is executed by the "real" root. – Fabian Damken Oct 30 '15 at 23:55
  • Yeah, sure. But sometimes... ;) – Fabian Damken Oct 30 '15 at 23:58
  • @alvits Thank you :D. That works. – Fabian Damken Oct 30 '15 at 23:58
  • Your idea of trying to do something that only root can do won't work, because `fakeroot` pretends that it works. – Barmar Oct 30 '15 at 23:58
  • @Barmar Yeah, it pretends. But you do not have real root privileges (i.e. you cannot create files only root can). So I thought it must be checkable. And it is. But not in a nice way ;). But, as mentioned by Ryan it is not cool to try to check the difference. – Fabian Damken Oct 31 '15 at 00:06

2 Answers2

8

Fakeroot sets custom LD_LIBRARY_PATH that contains paths to libfakeroot. For example:

/usr/lib/x86_64-linux-gnu/libfakeroot:/usr/lib64/libfakeroot:/usr/lib32/libfakeroot

You can use this to detect if application is running inside the fakeroot iterating by paths and looking for libfakeroot.

Sample code:

IS_FAKEROOT=false

for path in ${LD_LIBRARY_PATH//:/ }; do
        if [[ "$path" == *libfakeroot ]]; then
                IS_FAKEROOT=true
                break
        fi
done

echo "$IS_FAKEROOT"
Tomasz Kajtoch
  • 632
  • 7
  • 18
  • What a crazy solution :'D. But it works :'D. I think this is the best and safest solution. – Fabian Damken Oct 31 '15 at 00:08
  • 1
    You can simplify this to `[[ "$LD_LIBRARY_PATH:" = *libfakeroot:* ]]` (note: the ":" on the end of "libfakeroot:" makes it only match the end of path entries, the one on "$LD_LIBRARY_PATH:" allows it to match the final entry as well). Bonus: this works safely with path entries that include spaces and/or wildcards. – Gordon Davisson Oct 31 '15 at 06:27
-1

Here, The below script will find the user is root or not.

#!/bin/bash
touch /checkroot 2>/dev/null

uid=`stat -c "%u" /checkroot 2>/dev/null`

if [ "$uid" = "0" ]
then
    echo "Root user"

else
    echo "Not a root user"
fi

rm /checkroot 2>/dev/null

In the above example, I will try to create a file in the root directory, if I am not a root user and I don't have a permission it will give error, I was redirect that error to the /dev/null.

If the user have the permission, the file will be created. Then using the stat to get the user id for that file, and store that into the variable uid.

Using that variable in the if condition I will check.

If the temporary file will be created the rm command will remove that file.

But make sure the file is not already exist in the root directory.