LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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, 04: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, 04:44 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Does it work on your system ?
Code:
if [ ! -z "$SUDO_USER" ]; then
    echo "I was executed with sudo"
fi
 
Old 10-18-2008, 05: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, 06: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, 07: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, 11:06 AM   #6
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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, 03: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



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:17 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
Open Source Consulting | Domain Registration