[opensuse] Quick Bash Q - How to test for root when under 'su'
Listmates, In a script, I need to test that the user is either root or has su'ed to root before continuing. The USER in the environment remains the original user and there is nothing set related to 'su'. Any thoughts? -- David C. Rankin, J.D., P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Tuesday 30 October 2007 14:14:54 David C. Rankin J.D. P.E. wrote:
Listmates,
In a script, I need to test that the user is either root or has su'ed to root before continuing. The USER in the environment remains the original user and there is nothing set related to 'su'. Any thoughts?
[ $UID -eq 0 ] && echo I am root Anders -- Madness takes its toll -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Here is the piece of code I have used:
ROOT_UID=0 # Only users with $UID 0 have root privileges.
E_NOTROOT=67 # Non-root exit error.
if [ "$UID" -ne "$ROOT_UID" ]; then
echo "Must be root to run this script."
exit $E_NOTROOT
fi
There is a forum about bash scripting: http://bashscripts.org/
Bogdan
On 10/30/07, David C. Rankin J.D. P.E.
Listmates,
In a script, I need to test that the user is either root or has su'ed to root before continuing. The USER in the environment remains the original user and there is nothing set related to 'su'. Any thoughts?
-- David C. Rankin, J.D., P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
-- Bogdan Cristea -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Cristea Bogdan wrote:
Here is the piece of code I have used:
ROOT_UID=0 # Only users with $UID 0 have root privileges. E_NOTROOT=67 # Non-root exit error. if [ "$UID" -ne "$ROOT_UID" ]; then echo "Must be root to run this script." exit $E_NOTROOT fi
There is a forum about bash scripting: http://bashscripts.org/ Thanks Bogdan!
-- David C. Rankin, J.D., P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 David C. Rankin J.D. P.E. wrote:
Listmates,
In a script, I need to test that the user is either root or has su'ed to root before continuing. The USER in the environment remains the original user and there is nothing set related to 'su'. Any thoughts?
UID should contain the UID of the current user. It is a read only variable - -- ============================================================================== I have always wished that my computer would be as easy to use as my telephone. My wish has come true. I no longer know how to use my telephone. Bjarne Stroustrup ============================================================================== -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (GNU/Linux) Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org iD8DBQFHJzLAasN0sSnLmgIRAn9mAKDfQVu7JLko0NJTPQ/eR27DjlQU4QCgi7E5 oV1WqMVOKTanDVLRsRGOI7U= =xoIq -----END PGP SIGNATURE----- -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Tuesday 30 October 2007 14:14, David C. Rankin J.D. P.E. wrote:
Listmates,
In a script, I need to test that the user is either root or has su'ed to root before continuing. The USER in the environment remains the original user and there is nothing set related to 'su'. Any thoughts?
A bit more obvious (but starting an external program) than checking
$UID for 0:
whoami
This uses the effective (!) user ID, i.e. it also returns "root" for "su"
or "sudo".
CU
--
Stefan Hundhammer
On Oct 30 2007 17:13, Stefan Hundhammer wrote:
On Tuesday 30 October 2007 14:14, David C. Rankin J.D. P.E. wrote:
Listmates,
In a script, I need to test that the user is either root or has su'ed to root before continuing. The USER in the environment remains the original user and there is nothing set related to 'su'. Any thoughts?
A bit more obvious (but starting an external program) than checking $UID for 0:
whoami
This uses the effective (!) user ID, i.e. it also returns "root" for "su" or "sudo".
There is no guarantee that "root" or UID 0 actually have the privilege to do this or that. Think Linux Security Modules (LSM).. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Tuesday 30 October 2007 17:15, Jan Engelhardt wrote:
There is no guarantee that "root" or UID 0 actually have the privilege to do this or that. Think Linux Security Modules (LSM)..
No, but that also wasn't the question here.
For the ultimate check, there is little you can do other than try to do
whatever privileged operation you are up to and check for failure. But doing
this "root" check first sounds very sensible to me to catch the most common
cause for problems and (equally important) give the user some useful error
message.
CU
--
Stefan Hundhammer
participants (7)
-
Anders Johansson
-
Cristea Bogdan
-
David C. Rankin
-
David C. Rankin J.D. P.E.
-
G T Smith
-
Jan Engelhardt
-
Stefan Hundhammer