This is one of those things I know you can do, and is incredibly easy to do, but can never actually remember how
If a user uses sudo
then whoami
will return root (or, whatever user they sudo
'd too), but what if you want to push some kind of log to attribute it to the actual user, i.e. to get their original username while they're sudoed?
It's pretty simple...
logname
# Example snippet taken from a git post-commit hook
COMMITREF=`git log -1 HEAD | grep commit | cut -d\ -f2 | head -n1`
CHANGEDBY=`logname`
echo "#Last User authorisation update was $COMMITREF (changed by $CHANGEDBY)" | write_log