LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 10-18-2008, 05:29 PM   #1
almatic
Member
 
Registered: Mar 2007
Distribution: Debian
Posts: 547

Rep: Reputation: 67
bash - how to test, if a script is exececuted with sudo ?


Hi,

as title says, how can I test, if a script is exececuted with sudo ?
Testing for root is easy, but what about those nasty ubuntu users ?

if {what here ??};then
echo "I was executed with sudo"
else
echo "I was not executed with sudo"
fi
 
Old 10-18-2008, 05:44 PM   #2
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,617

Rep: Reputation: 136Reputation: 136
Does it work on your system ?
Code:
if [ ! -z "$SUDO_USER" ]; then
    echo "I was executed with sudo"
fi
 
Old 10-18-2008, 06:30 PM   #3
almatic
Member
 
Registered: Mar 2007
Distribution: Debian
Posts: 547

Original Poster
Rep: Reputation: 67
no, unfortunately not. I have no variable SUDO_USER set. Where is it set normally ? Is it set on a default ubuntu system ?
 
Old 10-18-2008, 07:07 PM   #4
jcookeman
Member
 
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417

Rep: Reputation: 33
Indeed

Code:
$ cat /etc/issue
Ubuntu 8.04.1 \n \l
$ cat sudo.sh
#!/usr/bin/env sh
echo $SUDO_USER
$ sudo ./sudo.sh
jcook
 
Old 10-18-2008, 08:36 PM   #5
lwasserm
Member
 
Registered: Mar 2008
Location: Baltimore Md
Distribution: ubuntu
Posts: 184

Rep: Reputation: 41
Quote:
Originally Posted by almatic View Post
Hi,

as title says, how can I test, if a script is exececuted with sudo ?
Testing for root is easy, but what about those nasty ubuntu users ?

if {what here ??};then
echo "I was executed with sudo"
else
echo "I was not executed with sudo"
fi
Are you meaning to distingusish between a script run with sudo vs. a script run by a "real" root or superuser? The man page for sudo says:
Quote:
... sudo allows a permitted user to execute a command as the superuser or another user, as specified in the sudoers file. The real and effective uid and gid are set to match those of the target user as specified in the passwd file...
so I'm not sure you can do a simple test. On a system using sudo there often is no way for a real superuser to even log on.

It's usually recommended to avoid using environment variables like $UID for security purposes because they are easily changed by the user. Use something like the id command instead.
 
Old 10-19-2008, 12:06 PM   #6
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,523

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
I was not aware that running as 'sudo' in Ubuntu was any different than running as 'sudo' in Fedora (or any other distro).

If you want to verify if a user has superuser privileges, then try:
Code:
if [ `id -u` -eq 0 ]
then
        echo "Ah, welcome O' Mighty One."
else
        echo "No grunt users allowed beyond this point!"
        exit 1
fi
 
Old 10-19-2008, 04:36 PM   #7
almatic
Member
 
Registered: Mar 2007
Distribution: Debian
Posts: 547

Original Poster
Rep: Reputation: 67
yes, you're right, there is no need to test for sudo. I thought it wouldn't change the uid on ubuntu, but it does. I should have read the manpage better ...
 
  


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
Bash script directory test rwills Linux - General 10 11-21-2009 07:59 AM
bash shell script "sudo - lunlun Linux - Newbie 2 05-09-2008 10:42 PM
help with Bash script (test) Benanzo Linux - Software 1 04-09-2007 04:18 AM
Is there a way to test for a keypress during a bash script? davecs Linux - Software 10 02-27-2007 12:50 PM
Test output from bash script estratos Programming 6 11-16-2006 10:01 PM


All times are GMT -5. The time now is 09:30 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration