Search code examples
shellscriptingsudo

How to find out who run sudo command?


I want to know which user has run the command

sudo -udelpoy sh abc.sh

When I echo whoami inside abc.sh I receive deploy as the user but I want to know which user has run the sudo command on first place.


Solution

  • You can use the -m option of who to get this info:

    julien> whoami
    julien
    julien> who -m
    julien   pts/4        2014-06-25 13:42 (:0)
    julien> sudo su
    root  > whoami
    root
    root  > who -m
    julien   pts/4        2014-06-25 13:42 (:0)
    

    If you only want the username, use awk:

    who -m | awk '{print $1}'
    

    From the manpage of who:

    -m     only hostname and user associated with stdin