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 |
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
|
06-02-2007, 10:00 PM
|
#1
|
Member
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518
Rep:
|
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
|
|
|
06-02-2007, 10:02 PM
|
#2
|
Member
Registered: Jul 2004
Distribution: Ubuntu
Posts: 387
Rep:
|
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 10:05 PM.
|
|
|
06-02-2007, 10:26 PM
|
#3
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
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
|
|
|
06-03-2007, 06:38 AM
|
#4
|
Member
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518
Original Poster
Rep:
|
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
|
|
|
06-03-2007, 07:14 AM
|
#5
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
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.
|
|
|
06-03-2007, 07:38 AM
|
#6
|
Member
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518
Original Poster
Rep:
|
an interesting idea, how do i turn on su logging?
|
|
|
06-03-2007, 08:18 AM
|
#7
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
please check your distribution's help references.
|
|
|
06-03-2007, 08:38 AM
|
#8
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
|
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.
|
|
|
06-03-2007, 12:12 PM
|
#9
|
Member
Registered: May 2007
Location: UK
Distribution: LFS,Slackware,Slamd64,NetBSD
Posts: 102
Rep:
|
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
|
|
|
06-04-2007, 05:49 AM
|
#10
|
Member
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518
Original Poster
Rep:
|
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 07:21 AM.
|
|
|
06-04-2007, 05:55 PM
|
#11
|
Member
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518
Original Poster
Rep:
|
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
|
|
|
06-04-2007, 07:17 PM
|
#12
|
Moderator
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
|
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
|
|
|
06-04-2007, 09:29 PM
|
#13
|
LQ Addict
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908
|
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.
|
|
|
06-05-2007, 05:40 AM
|
#14
|
Member
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518
Original Poster
Rep:
|
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
|
|
|
06-05-2007, 05:54 AM
|
#15
|
Member
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518
Original Poster
Rep:
|
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
|
|
|
All times are GMT -5. The time now is 10:23 AM.
|
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
|
|