Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the users real uid if the program is run with sudo?

Tags:

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!

like image 332
chacham15 Avatar asked Apr 22 '12 22:04

chacham15


People also ask

How do I know if sudo is used?

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.

What user does sudo run as?

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.

How do I use sudo as a root user?

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


1 Answers

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.

like image 60
sarnold Avatar answered Oct 05 '22 00:10

sarnold