Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Getting User ID in Bash

Posted on 2005-03-04
12
Medium Priority
?
800 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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

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…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.:
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

578 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