Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm writing a simple bash script, but I need it to check whether it's being run as root or not. I know there's probably a very simple way to do that, but I have no idea how.

Just to be clear:
What's a simple way to write a script foo.sh, so that the command ./foo.sh outputs 0, and the command sudo ./foo.sh outputs 1?

share|improve this question

7 Answers 7

up vote 23 down vote accepted
#!/bin/bash
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 
   exit 1
fi
share|improve this answer
3  
I'm accepting this one cause (after some testing) using the system variable EUID turned out to be (approx 100 times) faster than running a command (id -u), but all of the answers worked great. –  Malabarba Dec 2 '10 at 11:59
    
Thanks aneeshep. I didn't even know there was an environment variable for the effective UID. –  Delan Azabani Dec 2 '10 at 12:31
    
Be careful though! A program can temporarily drop its superuser privileges in which case $UID and id -ru are still 0, but $EUID and id -u aren't. See getuid(2) and decide what you need. –  David Foerster Oct 16 at 9:26
1  
Also, $UID and $EUID are bashisms. Merely POSIX compliant shells don't have those variables and you need to use id(1) instead. –  David Foerster Oct 16 at 9:28
    
in a bash script, you could use if ((EUID)); then, see @geirha's comment –  J.F. Sebastian Oct 17 at 19:38

A root user does not have to be named "root". whoami returns the first username with user ID 0. $USER contains the name of the logged in user, which can have user ID 0, but have a different name.

The only reliable program to check whether the account is logged in as root, or not:

id -u

I use -u for the effective user ID, not -r for the real user ID. Permissions are determined by the effective user ID, not the real one.

Tests

/etc/passwd contains the following usernames with user ID 0 in the given order:

rootx
root2

Logged in as root2, gives the next results:

  • whoami: rootx
  • echo $USER: root2 (this returns an empty string if the program was started in an empty environment, e.g. env -i sh -c 'echo $USER')
  • id -u: 0 As you can see, the other programs failed in this check, only id -u passed.

The updated script would looks like this:

#!/bin/bash
if ! [ $(id -u) = 0 ]; then
   echo "I am not root!"
   exit 1
fi
share|improve this answer
1  
Good analysis. +1 for informative bonus –  lazyPower Mar 13 '11 at 9:21
3  
You don't need id -u in bash askubuntu.com/questions/30148/… –  J.F. Sebastian Mar 13 '11 at 10:27
1  
I always try to stay as POSIX compliant as possible, using sh (dash) as interpreter instead of bash. But good shot, saves another fork :) –  Lekensteyn Mar 13 '11 at 10:34
    
+1 for giving the correct answer. –  Sam Hocevar Mar 13 '11 at 16:03
1  
I've seen shorter versions, [ -w / ]. The idea remains the same. Note: in certain chroots, [ -w /etc/shadow ] will fail because /etc/shadow is non-existent, therefore the approach with / is preferred. A way better would checking the actual need for root permissions. If the script needs to write to /etc/someconfig, just check if that file is writable OR the file does not exist and /etc is writable. –  Lekensteyn Mar 14 '11 at 22:47

As @Lekensteyn said you should use effective user ID. You don't need to call id -u in bash:

#!/bin/bash

if [[ $EUID -ne 0 ]]; then
   echo "You must be root to do this." 1>&2
   exit 100
fi

@geirha's suggestion from the comments uses arithmetic evaluation:

#!/bin/bash

if (( EUID != 0 )); then
   echo "You must be root to do this." 1>&2
   exit 100
fi
share|improve this answer
3  
In general one should use ((..)) for testing numbers, and [[..]] for testing strings and files, so I'd do if (( EUID != 0 )); then instead, or just if ((EUID)); then –  geirha Mar 13 '11 at 12:41
    
@geirha: I've added your suggestion. –  J.F. Sebastian Mar 13 '11 at 13:11
2  
EUID should become $EUID. Instead of using (( $EUID != 0 )), use [ $EUID != 0 ]. It will work with dash too. –  Lekensteyn Mar 13 '11 at 13:23
1  
@Lekensteyn: EUID works just fine. The question is tagged bash not dash. The command env -i sh -c '[ $EUID != 0 ]' fails, but env -i bash -c '(( EUID != 0 ))' works. btw, dash doesn't work for some non-ascii characters on Ubuntu stackoverflow.com/questions/5160125/… It is 2011 and there are people who use other languages. –  J.F. Sebastian Mar 13 '11 at 13:39
    
After looking back on this while being horribly bored, and testing, I am going to use this method as listed here with the $EUID stuff from this point forward. I have changed the accepted answer as a result. –  Thomas W. Aug 3 '11 at 15:15

You can accomplish this by using the whoami command, which returns the current user:

#!/bin/bash

if [ `whoami` != 'root' ]
  then
    echo "You must be root to do this."
    exit
fi

...

Running the above will print You must be root to do this. if the current user is not root.


Note: an alternative in some cases is to simply check the $USER variable:

if [ $USER != 'root' ]
share|improve this answer
    
I'll take a look at the code, see if it doesn’t make the script fail or something. If it works, I'll accept your answer :) –  Thomas W. Mar 13 '11 at 7:40
    
Put this into my scripts, it worked beautifully. Thanks much. :) –  Thomas W. Mar 13 '11 at 7:42
4  
@George Edison: I recommend against using $USER, especially in this way. $USER is set by the login shell, it does not necessary propagate to the program (env -i sh -c 'echo $USER'). In that way, $USER is empty and an syntax error occurs. –  Lekensteyn Mar 13 '11 at 9:26
1  
@Lekensteyn Not only is $USER set by the shell, it is also something that is easy to spoof. Whoami will return the actual user. –  Paul de Vrieze Mar 13 '11 at 11:48
1  
@PauldeVrieze What's the point of fooling that check? –  htorque Mar 13 '11 at 14:01
#!/bin/bash
[[ $(id -u) != 0 ]] 
echo $?
share|improve this answer
#!/bin/bash
uid=`id -u`
if [ "$uid" == "0" ]
then
    echo 1
else
    echo 0
fi
share|improve this answer

This snippet would:

  • Check on different sessions
  • suggest to use sudo !!
  • and return an error
if [ "$(whoami &2>/dev/null)" != "root" ] && [ "$(id -un &2>/dev/null)" != "root" ]
      then
      echo "You must be root to run this script!"
      echo "use 'sudo !!'"
      exit 1
fi
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.