Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm trying to write a bash script (in Ubuntu) that will backup a directory using tar.

How can I do a check in the script so that it can only be run as root (or with sudo)?

For instance, if a user runs the script, it should say that this script must be run with sudo privileges, and then quit. If the script is executed as root, it will continue past the check.

I know there has to be an easy solution, I just haven't been able to find it by googling.

share|improve this question
add comment

4 Answers

up vote 22 down vote accepted

To pull the effective uid use this command:

id -u

If the result is ‘0’ then the script is either running as root, or using sudo. You can run the check by doing something like:

if [[ $(/usr/bin/id -u) -ne 0 ]]; then
    echo "Not running as root"
    exit
fi
share|improve this answer
4  
I'd recommend fully-qualifying the path to id (e.g., /usr/bin/id). Otherwise a devious user could write their own script/binary that always returns 0 and then put it in a location that exists earlier in the executing users' path. –  ktower Jul 8 '09 at 17:57
    
Agreed. Fixing with an edit. –  Scott Pack Jul 8 '09 at 18:05
4  
Anyone 'devious' trying to run the script won't be stopped by you using the full path to id. –  theotherreceive Jul 8 '09 at 18:14
    
This is exactly what I'm looking for. Thanks –  Cory Plastek Jul 8 '09 at 19:23
1  
This doesn't address the "sudo" requirement. –  GregB May 9 '12 at 18:14
show 1 more comment

I assume you know that by changing the ownership to root

chown root:root file

and setting the permissions to 700

chmod 700 file

you will accomplish the same thing - without the suggestion to run as sudo.

But I will post this answer for completeness.

share|improve this answer
    
This is a more appopriate solution than the accepted answer. - My $0.02 –  Chris Jul 22 '09 at 11:38
add comment

What is your objective here, to inform the user that they should run the script as root or as some kind of security precaution?

If you just want to inform the user than any of the uid suggestions are fine, but they're as useful as tyres on a horse as a security precaution - there's nothing to stop a user from copying the script, taking out the if statement, and running it anyway.

If this is a security issue then the script should be set to 700, owned by root:root, so that it is not readable or executable by any other user.

share|improve this answer
    
Or, it could be the script requires access to files or commands only accessible to root in order to carry out its work, as in my case –  chrisbunney Sep 30 '11 at 15:01
add comment

You can use whoami command as well.

if [ ! "`whoami`" = "root" ]
then
    echo "\nPlease run script as root."
    exit 1
fi
share|improve this answer
add comment

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.