Hemanth's Scribes

cli

Check for Root User

Author Photo

Hemanth HM

Thumbnail

The superuser UID is always 0 (from Linux source).

Check by UID

Bash: isRoot=$(($UID == 0))

Node.js: var isRoot = process.getuid && process.getuid() === 0;

Ruby: isRoot = Process.uid.zero?

Python: isRoot = os.geteuid() == 0

Haskell: let isRoot = fmap (== 0) getRealUserID

Check by Write Access

Bash: [[ -w / ]] && isRoot=true || isRoot=false

Ruby: isRoot = File.writable_real?("/")

Python: isRoot = os.access('/', os.W_OK)

#linux#cli
Author Photo

About Hemanth HM

Hemanth HM is a Sr. Machine Learning Manager at PayPal, Google Developer Expert, TC39 delegate, FOSS advocate, and community leader with a passion for programming, AI, and open-source contributions.