Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I use su when I often need to set proper permissions when copying files etc, but forget if I used su to get there, ex:

root@host> su bob
bob@host> copy folderA folderB

But sometimes I forget if I logged in as bob or if I was root and su'd to become bob, make since? So a full example:

bob@host> su
....
root@host> su bob
bob@host> cp folderA folderB
....
bob@host> ????????

Now at this point I forget if I am directly logged into the computer or if I am su'd in, I can press CTRL-D which would tell me if I was root, but if not I get logged out.

I understand sudo is good and secure and all, but I'm curious if there is a way to do this without avoiding using su.

Anyone have some ideas?

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

You can do this by looking at the running processes in a tree.

ps axjf

Look through the long output till you find something like this:

    1  2426  2426  2426 ?           -1 S<s      0   3:35 /usr/sbin/sshd
 2426  7024  7024  7024 ?           -1 S<s      0   0:00  \_ sshd: user1 [priv]
 7024  7029  7024  7024 ?           -1 S<    1002   0:00      \_ sshd: user1@pts/0
 7029  7030  7030  7030 pts/0    29857 S<s   1002   0:00          \_ -bash
 7030 29831 29831  7030 pts/0    29857 S<       0   0:00              \_ su
29831 29833 29833  7030 pts/0    29857 S<       0   0:00                  \_ bash
29833 29845 29845  7030 pts/0    29857 S<    1017   0:00                      \_ su user2
29845 29846 29846  7030 pts/0    29857 S<    1017   0:00                          \_ bash
29846 29857 29857  7030 pts/0    29857 R<+   1017   0:00                              \_ ps axjf

You can see i logged in as user1 and then su'd to root and then su'd to user2

This command worked fine for me on debian. If you use a differnt version of linux and it doesn't work just check the man page for ps and search for the word 'tree'

share|improve this answer
    
perfect! I was suspecting that I would have no solution –  SeanDowney Sep 15 '11 at 17:16
    
ps af seems to do the same, but not showing as much output –  SeanDowney Sep 15 '11 at 17:19
add comment

You can use whoami to tell you the effective userid and you can use who am i to tell you who you logged in as e.g.

$ who am i
iain     pts/0        2011-09-15 17:33 (192.168.1.104)
$ whoami
iain

$ su -
# whoami
root
# who am i
iain     pts/0        2011-09-15 17:33 (192.168.1.104)

# su - user1
$ whoami
user1
$ who am i
iain     pts/0        2011-09-15 17:33 (192.168.1.104)
share|improve this answer
    
this is good to detect if you logged on as a user, but it's possible to su to Iain and not know if you were logged in that way or su'd –  SeanDowney Sep 15 '11 at 17:14
add comment

last command can display last logged in user.

share|improve this answer
    
yes, but I don't think "su user" is logged and I wouldn't know if I logged in as the user or if I su'd into that user –  SeanDowney Sep 15 '11 at 16:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.