Check if Python User is Root

Last modified: 
Wednesday, July 1st, 2015
Topics: 
Python

How to check if the user calling a python script is running as root.

import os

if os.geteuid()==0:
  print "Running as root."
else:
  print "User is not root."

The following script will exit with a message to the user if they are not root.

#! /usr/bin/env python
import os, sys

# This script must be run as root!
if not os.geteuid()==0:
    sys.exit('This script must be run as root!')


The operator of this site makes no claims, promises, or guarantees of the accuracy, completeness, originality, uniqueness, or even general adequacy of the contents herein and expressly disclaims liability for errors and omissions in the contents of this website.