0

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?

3 Answers 3

3

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'

2
  • perfect! I was suspecting that I would have no solution
    – SeanDowney
    Sep 15, 2011 at 17:16
  • ps af seems to do the same, but not showing as much output
    – SeanDowney
    Sep 15, 2011 at 17:19
3

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)
1
  • 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, 2011 at 17:14
0

last command can display last logged in user.

1
  • 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, 2011 at 16:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .