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.

Possible Duplicate:
How can a script check if it’s being run as root?

This is something I've been curious about. I make a lot of small bash scripts (.sh files) to do tasks that I routinely do. Some of those tasks require everything to be ran as superuser. I've been curious: Is it possible to, within the BASH script prior to everything being run, check if the script is being run as superuser, and if not, print a message saying You must be superuser to use this script, then subsequently terminate the script itself. The other side of that is I'd like to have the script run when the user is superuser, and not generate the error.

Any ideas on coding (if statements, etc.) on how to execute the aforementioned?

share|improve this question

marked as duplicate by Nathan Osman, Tom Brossman, jokerdino, John S Gruber, maythux Oct 13 '12 at 4:47

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers 3

up vote 23 down vote accepted

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
    
@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
    
Wow, you're responding to every question I ask with information that is extremely useful :P I'll take a look at the code, see if it doesnt make the script fail or something. If it works, I'll accept your answer :) –  Thomas W. Mar 13 '11 at 7:40
    
@Evil: I'm starting a trend :) –  Nathan Osman Mar 13 '11 at 7:41
1  
@Evil: The thing is... if you ask a question here, then the next guy that runs into the problem will be able to get a quick answer. –  Nathan Osman Mar 13 '11 at 7:44
2  
@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

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

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