Check to see if a script was run as root
April 16th, 2008 by Mitch Frazier in
If you need to make sure a script is run as root, add the following to the start of the script:
if [[ $UID -ne 0 ]]; then
echo "$0 must be run as root"
exit 1
fi
__________________________
Mitch Frazier is an Associate Editor for Linux Journal and the Web Editor for linuxjournal.com.
Special Magazine Offer -- Free Gift with Subscription
Receive a free digital copy of Linux Journal's System Administration Special Edition as well as instant online access to current and past issues. CLICK HERE for offer
Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.
Subscribe now!
The Latest
Newsletter
Tech Tip Videos
- Jul-01-09
- Jun-29-09
Recently Popular
From the Magazine
July 2009, #183
News Flash: Linux Kernel 3.0 to include an on-the-go Expresso machine interface! Ok, maybe not, but Linux is definitely going mobile, from phones to e-readers. Find out more inside about Android, the Kindle 2, the Western Digital MyBook II, The Bug, and Indamixx (a portable recording studio). And if you've gone mobile and you been wanting more Emacs in your life then check out Conkeror.
To compliment the mobile we've got the stationary: parsing command line options with getopt, checking your Ruby code with metric_fu, and building a secure Squid proxy. How is this stationary you ask? What can we say? It's not. We just wanted to see if anybody actually read this part of the page :) .
All this and more, and all you have to do is get your hot sweaty hands on the latest copy of Linux Journal.
Delicious
Digg
StumbleUpon
Reddit
Facebook








I don't belive this is the
On April 17th, 2008 Anonymous (not verified) says:
I don't belive this is the right way of checking the user. This is as easy as your solution but is harder to bypass.
to bypass your code:
$ sh
$ export UID=0
run command
better way:
LUID=$(id -u)
if [[ $LUID -ne 0 ]]; then
echo "$0 must be run as root"
exit 1
fi
hope this help
bye
Not a problem, at least with bash
On April 22nd, 2008 Mitch Frazier says:
Bash won't let you do "export UID=0", UID is a read-only variable.
__________________________Mitch Frazier is an Associate Editor for Linux Journal and the Web Editor for linuxjournal.com.
Effective User ID
On April 16th, 2008 lowkey (not verified) says:
You may find it more useful to check for the Effective User ID (EUID) rather than the plain User ID (UID). Then your script will work with SUID cases.
Just replace $UID with $EUID in the above snippet.
Maybe
On April 22nd, 2008 Mitch Frazier says:
As I recall the set-uid bit doesn't work on shell scripts, so unless your script is going to be run by a compiled program that has the set-uid bit set this wouldn't make any difference. Or am I missing something here?
__________________________Mitch Frazier is an Associate Editor for Linux Journal and the Web Editor for linuxjournal.com.
bash != sh
On April 16th, 2008 Anonymous (not verified) says:
$ sudo sh -c 'set' | grep UID
SUDO_UID='1000'
$ sudo bash -c 'set' | grep UID
EUID=0
SUDO_UID=1000
UID=0
bash provides the environment variable 'UID', but other shells do not. Additionally '[[', while not bash specific is not present in all shells. A better solution (albeit, one dependent on 'id'):
uid=`id -u` && [ "$uid" = "0" ] ||
{ echo "must be root"; exit 1; }
Whose id command?
On June 5th, 2008 charliebrady says:
A little safer/more reliable to do:
uid=$(/usr/bin/id -u) && [ "$uid" = "0" ] ||
{ echo "must be root"; exit 1; }
Use #!/bin/bash
On April 22nd, 2008 Mitch Frazier says:
I always put "#!/bin/bash" at the top of my scripts, but beyond that I don't worry about non-bash environments. However, there certainly are some scripts that may run or need to run in an environment where bash does not exist.
__________________________Mitch Frazier is an Associate Editor for Linux Journal and the Web Editor for linuxjournal.com.
Post new comment