Finding the username of a user after they've used sudo



Published: 2020-09-28 08:52:01 +0000
Categories: BASH,

Language

BASH

Description

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...

Snippet

logname

Usage Example

# 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

License

BSD-3-Clause

Keywords

sudo, logname, username, user, identify,

Latest Posts


Copyright © 2020 Ben Tasker | Sitemap | Privacy Policy
Available at snippets.bentasker.co.uk and http://phecoopwm6x7azx26ctuqcp6673bbqkrqfeoiz2wwk36sady5tqbdpqd.onion