?
Solved

Getting User ID in Bash

Posted on 2005-03-04
12
Medium Priority
?
784 Views
Last Modified: 2010-04-20
I want to just check to see the user id of the user running the bash script. The script needs to be run as root, so I want to make sure root is executing the script.
0
Comment
Question by:dignified
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +3
12 Comments
 
LVL 23

Expert Comment

by:Mysidia
ID: 13462545
User identification is provided by one of the standard bash variables...

http://www.gnu.org/software/bash/manual/bashref.html#SEC62
0
 
LVL 14

Expert Comment

by:trigger-happy
ID: 13462953
the env variable is $USER
you can use that for finding out who the user is.

--trigger-happy
0
 
LVL 40

Expert Comment

by:jlevie
ID: 13463139
You can use the id command for this, which works for any shell. For example:

wilowisp> id
uid=1716(jlevie) gid=11(noc) groups=11(noc)
0
Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

 
LVL 23

Expert Comment

by:Mysidia
ID: 13463158
USER is not a shell built in, it may be set, but it is not built in...
(no guarantee it would be set)

besides which it can easily be set by the person trying to run the script via
export USER=root

More importantly... it won't be set to root necessarily when a user su's to
root via "su"..  USER typically reflects the user at login time..  in this
sense "USER" is similar to the "LOGNAME" variable

What would be more appropriate to use would be a variable
like EUID or UID
0
 
LVL 14

Expert Comment

by:trigger-happy
ID: 13463199
oh ok, thnx for correcting me

--trigger-happy
0
 
LVL 38

Expert Comment

by:wesly_chen
ID: 13463902
Hi,

#!/bin/sh

for USERS in `ps -o "%U : %a" | grep -v grep | grep bash | awk -F: '{print $1}'`
do
  id $USERS
done

For more information about "id", do
man id

Wesly
0
 
LVL 23

Expert Comment

by:Mysidia
ID: 13463984
Who cares what other users are running bash?

If you want to use ps, then do

ps -o %U $$ |tail -1 |xargs id
0
 
LVL 23

Expert Comment

by:Mysidia
ID: 13464004
Err, oops.. that's just weird, don't use programs like PS to get the current ID,
A more practical approach was mentioned earlier
0
 
LVL 38

Expert Comment

by:wesly_chen
ID: 13464084
Thanks Mysdia for pointing out "ps -o %U" issue.

#!/bin/sh

for USERS in `ps -elf | grep -v grep | grep bash | awk  '{print $3}'| sort -u`
do
  id $USERS
done

Wesly
0
 
LVL 3

Expert Comment

by:mike_mian
ID: 13468807
`you could  just set the permission fo the script so that no-none but root can run it.
ie
chmod 700 <scriptname>

This sets teh permission on hte script to readwrite and execute  root only.

I am assuming that the script is owned by root and you have access to the root if not so can change the ownership (eg chown root <scriptname>)
0
 
LVL 3

Assisted Solution

by:bryanlloydharris
bryanlloydharris earned 80 total points
ID: 16048793
Using the info from the first post:

$ echo $EUID
500

Bryan
0
 
LVL 23

Accepted Solution

by:
Mysidia earned 120 total points
ID: 16050735
Right, specifically:

if [ $EUID -ne 0 ] ; then
  echo "Sorry, this script must be run as root"
  exit 0
fi
0

Featured Post

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses
Course of the Month10 days, 16 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question