Loading
Home ›
Check to see if a script was run as root
Apr 16, 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.
Trending Topics
| You Need A Budget | Feb 10, 2012 |
| The Linux powered LAN Gaming House | Feb 08, 2012 |
| Creating a vDSO: the Colonel's Other Chicken | Feb 06, 2012 |
| Your CMS Is Not Your Web Site | Feb 01, 2012 |
| Casper, the Friendly (and Persistent) Ghost | Jan 31, 2012 |
| Razor-qt 0.4 - Qt based Desktop Environment | Jan 30, 2012 |
- Fun with ethtool
- Linux-Based X Terminals with XDMCP
- 100% disappointed with the decision to go all digital.
- Readers' Choice Awards 2011
- Parallel Programming with NVIDIA CUDA
- You Need A Budget
- Validate an E-Mail Address with PHP, the Right Way
- The Linux powered LAN Gaming House
- The Linux RAID-1, 4, 5 Code
- Python for Android
- Gnome3 is such a POS. No one
5 hours 5 min ago - Gnome 3 is the biggest POS
5 hours 15 min ago - I didn't knew this thing by
11 hours 20 min ago - Author's reply
14 hours 44 min ago - Link to modlys
15 hours 51 min ago - I use YNAB because of the
16 hours 2 min ago - Search
21 hours 5 min ago - Question
21 hours 29 min ago - for the record
21 hours 31 min ago - That's disappointing. Thanks
23 hours 54 min ago





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.