Check to see if a script was run as root
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.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- New Products
- Linux Systems Administrator
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- Designing Electronics with Linux
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- Reply to comment | Linux Journal
4 hours 48 min ago - Nice article, thanks for the
15 hours 29 min ago - I once had a better way I
21 hours 15 min ago - Not only you I too assumed
21 hours 32 min ago - another very interesting
23 hours 25 min ago - Reply to comment | Linux Journal
1 day 1 hour ago - Reply to comment | Linux Journal
1 day 8 hours ago - Reply to comment | Linux Journal
1 day 8 hours ago - Favorite (and easily brute-forced) pw's
1 day 10 hours ago - Have you tried Boxen? It's a
1 day 16 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?



Comments
I don't belive this is the
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
Bash won't let you do "export UID=0", UID is a read-only variable.
Mitch Frazier is an Associate Editor for Linux Journal.
Effective User ID
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
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.
bash != sh
$ 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?
A little safer/more reliable to do:
uid=$(/usr/bin/id -u) && [ "$uid" = "0" ] ||
{ echo "must be root"; exit 1; }
Use #!/bin/bash
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.