LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-27-2012, 07:34 AM   #1
mogleyzw
LQ Newbie
 
Registered: May 2012
Posts: 8

Rep: Reputation: Disabled
Code to ensure script runner is root user


Hi,

I just need a code fragment with checks if the user is the root user.

Basically I need to ensure it was run as "sudo myscript" rather then just "myscript"

I found

Code:
if [ "$USER" != "root" ]; then 
    echo "You are not root user, please log in as root user"
    exit
fi
But that didn't work. I thought maybe it was because I had not defined the variable $USER and I wasn't sure how to.

Any help will be appreciated.
 
Old 05-27-2012, 07:43 AM   #2
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,252

Rep: Reputation: 313Reputation: 313Reputation: 313Reputation: 313
You can use "id" program, like that:
Code:
if [ $(id -ur) != 0 ]; then 
    echo "You are not root user, please log in as root user"
    exit
fi
 
Old 05-27-2012, 10:21 AM   #3
sysfce2
Member
 
Registered: Jul 2009
Distribution: Slackware
Posts: 96

Rep: Reputation: 39
Why not just set the execute permissions so that only root can run the script?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Pass root permissions to user from a root script Ahaaa Programming 18 04-21-2012 11:53 PM
Ensure strength of passwords set by root satya123 Linux - Newbie 1 03-16-2011 08:04 AM
Ensure changes to system-auth do not lockout root mccartjd Linux - Security 1 01-11-2010 06:24 PM
script to ensure lines are in sequence docaia Linux - General 2 05-30-2008 06:10 AM
How can I have a script owned as root and run as root by a user: setuid? stickey bit? abefroman Linux - Newbie 9 04-19-2008 06:15 PM


All times are GMT -5. The time now is 10:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration