Check to see if a script was run as root

April 16th, 2008 by Mitch Frazier in

Your rating: None Average: 5 (2 votes)

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.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Anonymous's picture

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

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.

lowkey's picture

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.

Mitch Frazier's picture

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.

Anonymous's picture

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; }

charliebrady's picture

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; }

Mitch Frazier's picture

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

Please note that comments may not appear immediately, so there is no need to repost your comment.
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <i> <b>
  • Lines and paragraphs break automatically.

More information about formatting options

Newsletter

Each week Linux Journal editors will tell you what's hot in the world of Linux. You will receive late breaking news, technical tips and tricks, and links to in-depth stories featured on www.linuxjournal.com.
Sign up for our Email Newsletter

Tech Tip Videos

From the Magazine

December 2009, #188

If last month's Infrastrucuture issue was too "big" for you then try on this month's Embedded issue. Find out how to use Player for programming mobile robots, build a humidity controller for your root cellar, find out how to reduce the boot time of your embedded system, and if you're new to embedded systems find out the basics that go into one. You can also read about the Beagle Board, the Mesh Potato and a spate of other interestingly named items. And along with our regular columns don't miss our new monthly column: Economy Size Geek.


Read this issue