LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-02-2007, 11:00 PM   #1
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518

Rep: Reputation: 39
how to get my username from command shell


Hello,
what i would like to know and if it is possible or not is:

Once in BASH for instanced i've su'ed to be the root user "su root", i want to know if its still possible to run a command or something after the su to be root to work out the username and the homearea for the user that is su into root.

For instance if user x su'ed to be root of course if you try the who or check the home area it would be root, so is it possible after becoming root in bash to still get the user x username and the directory path to the user x home area.

Thanks and regards
 
Old 06-02-2007, 11:02 PM   #2
vxc69
Member
 
Registered: Jul 2004
Distribution: Ubuntu
Posts: 387

Rep: Reputation: 33
Just type whoami.

EDIT: Oh wait, hang on. You want to know the user who su'd into root. As in who the user was before you su'd into root? Oh, sorry no clue. It must be possible in some way though.

Last edited by vxc69; 06-02-2007 at 11:05 PM.
 
Old 06-02-2007, 11:26 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
With a ps -ef you should be able to see the shell and
the parent process ID of the user who su'd to root ...

Code:
ps -ef |awk -v var=`ps -ef| grep 'su -'|grep -v grep|awk '{print $3}'` '$2 == var {print $1}'

Cheers,
Tink
 
Old 06-03-2007, 07:38 AM   #4
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518

Original Poster
Rep: Reputation: 39
thanks for the reply peoples, and Tink unfortunately that didn't work it returned no output. Also you have grep 'su -' and in a ps -ef on this it will only show su as a process so i guess that might be one problem, and for that su process anyway it has the root as the user. it does however show /bin/bash as a process and that has the right username, but i'm thinking that would be no good as anyone could be running a bash shell at the time and then it could pick up any or one of the users which co9uld be the wrong one.

Thanks for your help
 
Old 06-03-2007, 08:14 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
i don't know how your system is configured, but one way may be to check your su log (assuming you have turned su logging on) for the latest entry. An example on my machine, it is captured in /var/log/messages, so I can grep for the latest entry in that log. sample:
Code:
Jun  2 20:11:59 linux su: (to root) root on /dev/pts/1
once i know the format of this log, i can decide what i want to do.
 
Old 06-03-2007, 08:38 AM   #6
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518

Original Poster
Rep: Reputation: 39
an interesting idea, how do i turn on su logging?
 
Old 06-03-2007, 09:18 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
please check your distribution's help references.
 
Old 06-03-2007, 09:38 AM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint
Posts: 9,669

Rep: Reputation: 398Reputation: 398Reputation: 398Reputation: 398
There is actually no need to for a more or less complex script or turning on some logging.

vxc69 was close, the "who am i" (or who -m) command will tell you under what account you were originally logged in before running su.
 
Old 06-03-2007, 01:12 PM   #9
dawkcid
Member
 
Registered: May 2007
Location: UK
Distribution: LFS,Slackware,Slamd64,NetBSD
Posts: 102

Rep: Reputation: 15
For some reason, who -m gives me no output. Instead you could try:

Code:
ls -l $(tty) | awk '{print $3}'
that will (should) give you the username, and the following will give you the uid (may only work on Linux though):

Code:
awk '/Uid/{print $2}' /proc/$PPID/status
 
Old 06-04-2007, 06:49 AM   #10
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518

Original Poster
Rep: Reputation: 39
Thanks very very much thats exactly what i was looking for jlliagre, works perfect. Brilliant

dawkcid i wonder if you didn't get an output because you forgot to su to root before running the `who -m` or `who am i` command, it works perfect for me!

Thanks again perfect, I'm really pleased

dawkcid, i will also try you commands and let you know how it went, they could also be quite useful

Thanks to all

Last edited by helptonewbie; 06-04-2007 at 08:21 AM.
 
Old 06-04-2007, 06:55 PM   #11
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518

Original Poster
Rep: Reputation: 39
thanks those commands did work and dawkcid, i tried yours to and it worked fine as well. I have run into an un-expected problem however, the commands are run from within a script and the script has been run through a kde-su which is how it has got to be, therefore it changes the output of retreiving the username, it is now root again instead of the required output. So any other ideas, or anyone know how to retrieve the username of a person before a kde-su was performed, sorry but i thought a su was more or less the same as a kde-su, i guess not.

Any help appreciated
 
Old 06-04-2007, 08:17 PM   #12
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by helptonewbie
thanks for the reply peoples, and Tink unfortunately that didn't work it returned no output. Also you have grep 'su -' and in a ps -ef on this it will only show su as a process so i guess that might be one problem, and for that su process anyway it has the root as the user. it does however show /bin/bash as a process and that has the right username, but i'm thinking that would be no good as anyone could be running a bash shell at the time and then it could pick up any or one of the users which co9uld be the wrong one.

Thanks for your help
Did you copy & paste it, or type it in?

Basically what it does is the following:
look through ps -ef to find the PPID of the shell that
became root. Grab that PPID and look for the PID that
matches, then print the user who owns that PID. Works
here.


Code:
root: ~ # whoami
root
root: ~ # ps -ef |awk -v var=`ps -ef| grep 'su -'|grep -v grep|awk '{print $3}'` '$2 == var {print $1}'
tink
Try running the chunk in the back-ticks individually to see what it does.
Code:
ps -ef| grep 'su -'|grep -v grep|awk '{print $3}'
It should return the PPID of a process.


Cheers,
Tink
 
Old 06-04-2007, 10:29 PM   #13
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,877

Rep: Reputation: 350Reputation: 350Reputation: 350Reputation: 350
ghostdog74's suggestion works for me: tail /var/log/messages | grep 'root'.

That's if I've su'd to root. Generally, I have sudo set up for the commands I'm most likely to need root authority.
 
Old 06-05-2007, 06:40 AM   #14
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518

Original Poster
Rep: Reputation: 39
tink, no it didn't return the value i noticed the grep -v 'grep' and thought shouldn't it be grep -e 'grep' and now it returns the PPID. But the thing as a whole still doesn't work, and i receive no error messages. Another strange thing is that if i do this:

Code:
ps -ef| grep 'su -'|grep -e grep|awk '{print $3}'
i for instance get the PPID of 5000 if i do this:
Code:
NUMBER=`ps -ef| grep 'su -'|grep -e grep|awk '{print $3}'`
echo $NUMBER
i get a completely different PPID


I dont think this will work anyway for what i need as i'm now looking to try and get the return of a username for the user that ran a kdesu, or more specifically double clicked on a "Desktop Config File" which runs a command to run a script as the root user. a if your idea will still work for that as well then i'm very interested, or if you have another idea then i'm really willing to listen to all, i'm having a lot of trouble trying to find solution, working on it for a few days now
 
Old 06-05-2007, 06:54 AM   #15
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518

Original Poster
Rep: Reputation: 39
YEY, i fixed it, just found the variable $USER is something automatically set, echo $USER regardless of how i have su'd/kdesu'd or whatever still returns the original user name.

Now the only question left is is the $USER variable going to be the same on most / all different Linux Distro's, as i might be running this on more that one distro so it doesn't want to be distro specific.

Thanks to everyone for their help
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
return the username in a command matthewhardwick Linux - General 6 10-12-2007 07:30 AM
GCC inserts my username in command line rickthemick Solaris / OpenSolaris 4 03-19-2007 11:54 AM
Shell: command not found / Runs fine with "Run command" badbunny Linux - Newbie 1 01-22-2007 02:21 AM
useradd: invalid username username$ engyeow Fedora 5 12-05-2004 05:35 AM
command prompt - username@mac address? ryedunn Linux - Newbie 5 03-16-2004 08:55 AM


All times are GMT -5. The time now is 06:22 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration