The program I am running needs root privledges and therefore is run with sudo
, but it also needs to know what user is running it. getuid
and geteuid
both return root
. How do I get the actual users username (or uid)?
Thanks!
Run sudo -v . It is usually used to extend your sudo password timeout, but can be used for determining whether you have any sudo privileges.
The sudo - Super User Do Like su , if no username is specified, it assumes that you were trying to run commands as the superuser. This why sudo is referred to as superuser do. It is commonly used to install, start and stop applications that require root priviledges.
To use sudo when using the command line, simply type "sudo" before the command you wish to run. Sudo will then prompt you for your password. Sudo will remember your password for a set amount of time (15 minutes by default).
sudo
provides some environment variables to help you with exactly this case:
SUDO_UID Set to the user ID of the user who invoked sudo SUDO_USER Set to the login of the user who invoked sudo
steveayre has pointed out in the comments that the user can set these environment variables in some cases; the sudo(8)
manpage includes in part:
The sudoers policy subjects variables passed on the command line to the same restrictions as normal environment variables with one important exception. If the setenv option is set in sudoers, the command to be run has the SETENV tag set or the command matched is ALL, the user may set variables that would otherwise be forbidden. See sudoers(5) for more information.
So be sure that you don't grant ALL
commands to users when you need to rely upon this feature.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With