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


Mitch Frazier is an Associate Editor for Linux Journal.


Comment viewing options

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

I don't belive this is the

Anonymous's picture

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

hope this help

Not a problem, at least with bash

Mitch Frazier's picture

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

lowkey's picture

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

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

Anonymous's picture

$ sudo sh -c 'set' | grep UID
$ sudo bash -c 'set' | grep UID

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?

charliebrady's picture

A little safer/more reliable to do:

uid=$(/usr/bin/id -u) && [ "$uid" = "0" ] ||
{ echo "must be root"; exit 1; }

Use #!/bin/bash

Mitch Frazier's picture

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.

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState