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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
10-18-2008, 04:29 PM
|
#1
|
|
Member
Registered: Mar 2007
Distribution: Debian
Posts: 547
Rep:
|
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
|
|
|
|
10-18-2008, 04:44 PM
|
#2
|
|
Senior Member
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282
Rep:
|
Does it work on your system ?
Code:
if [ ! -z "$SUDO_USER" ]; then
echo "I was executed with sudo"
fi
|
|
|
|
10-18-2008, 05:30 PM
|
#3
|
|
Member
Registered: Mar 2007
Distribution: Debian
Posts: 547
Original Poster
Rep:
|
no, unfortunately not. I have no variable SUDO_USER set. Where is it set normally ? Is it set on a default ubuntu system ?
|
|
|
|
10-18-2008, 06:07 PM
|
#4
|
|
Member
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417
Rep:
|
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
|
|
|
|
10-18-2008, 07:36 PM
|
#5
|
|
Member
Registered: Mar 2008
Location: Baltimore Md
Distribution: ubuntu
Posts: 184
Rep:
|
Quote:
Originally Posted by almatic
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.
|
|
|
|
10-19-2008, 11:06 AM
|
#6
|
|
Senior Member
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,388
|
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
|
|
|
|
10-19-2008, 03:36 PM
|
#7
|
|
Member
Registered: Mar 2007
Distribution: Debian
Posts: 547
Original Poster
Rep:
|
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 ...
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:32 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|