How to check what user php is running as?
Asked Answered
A

16

150

I need to detect if php is running as nobody. How do I do this?

Are there any other names for "nobody"? "apache"? Any others?

Afore answered 14/10, 2011 at 17:56 Comment(6)
What do you mean exactly with the "are there any other names" question? Sysadmins can create users with whatever name they want.Bausch
Any other default names for anything that might be considered the server; apache, nobody, www-data, etc..Afore
If I recall correctly, system users normally have a small UID (below 1024?). That might be a better clue for whatever you are trying to accomplish.Bausch
php.net/manual/en/function.get-current-user.phpGeotropism
get_current_user() returns the owner of current script, not the user php is currently running.Aestivate
Related: How do I find out, which user is running the current php script?Weatherman
M
107

If available you can probe the current user account with posix_geteuid and then get the user name with posix_getpwuid.

$username = posix_getpwuid(posix_geteuid())['name'];

If you are running in safe mode however (which is often the case when exec is disabled), then it's unlikely that your PHP process is running under anything but the default www-data or apache account.

Multifid answered 14/10, 2011 at 18:3 Comment(1)
There is a comment on get_current_user() manpage that shows this approach. As of PHP 5.4, it can be shortened to this: print posix_getpwuid(posix_geteuid())['name'];Weatherman
N
249

<?php echo exec('whoami'); ?>

Nobility answered 14/10, 2011 at 17:58 Comment(6)
Can't really use exec or system or any such passthru functions.Afore
Was just what I needed. get_current_user() was not what I needed definitely.Downstream
Might be useful to some users: if you run this from the command line interface you will get the user that runs the command, and not the PHP user.Bulganin
@BramVanroy then where should you run it ? =|Mycetozoan
@BramVanroy - I want to understand your comment, but I don't. if you run that from the cli, isn't you who owns the current php process? Who would the PHP user be in this case other than you?Clinkerbuilt
in C-Panel that option is disabled by default, at least on the two websites that I checked.Ibnsina
M
107

If available you can probe the current user account with posix_geteuid and then get the user name with posix_getpwuid.

$username = posix_getpwuid(posix_geteuid())['name'];

If you are running in safe mode however (which is often the case when exec is disabled), then it's unlikely that your PHP process is running under anything but the default www-data or apache account.

Multifid answered 14/10, 2011 at 18:3 Comment(1)
There is a comment on get_current_user() manpage that shows this approach. As of PHP 5.4, it can be shortened to this: print posix_getpwuid(posix_geteuid())['name'];Weatherman
D
87

Kind of backward way, but without exec/system:

file_put_contents("testFile", "test");
$user = fileowner("testFile");
unlink("testFile");

If you create a file, the owner will be the PHP user.

This could also likely be run with any of the temporary file functions such as tempnam(), which creates a random file in the temporary directory and returns the name of that file. If there are issues due to something like the permissions, open_basedir or safe mode that prevent writing a file, typically, the temp directory will still be allowed.

Dellora answered 14/10, 2011 at 18:4 Comment(9)
Very clever cross platform solution. Boh!Takishatakken
Nice: the only non-privileged solution here to also find the group that PHP is running as.Sphere
a worthy hack, deserves a higher placeHenrique
I can't write to the web directory, which is what brought me here, but rather than getting trapped in a circle of fail I read Jonathan Kuhn's comment, and tempnam worked a treat. I still can't write to the directory, but at least I've confirmed who I am.Brey
Depends on the write permissions php user is having or not. And for giving write permissions you are required to know the username, Practically only feasible for those programmers who forgot the php user after assigning it the write permissions.Pester
@RavinderPayal Unless you use the tempnam function like I suggested. It is almost guaranteed that the user can write to the temporary directory regardless of who they are. Without being able to write to the temporary directory, you could temporarily give a folder 777 permissions and if enabled, disable selinux for that path.Dellora
Got it brother. Sorry for not reading last two - three lines of answer.Pester
@RavinderPayal there are additional functions to return the temp path. So $temp_file = tempnam(sys_get_temp_dir(), 'TMP'); would create a temporary file in the temp directory on most any system. You could then use that file to get the user/group. I left some of that up to the user to figure out.Dellora
Thanks brother for adding more value to answerPester
C
20

More details would be useful, but assuming it's a linux system, and assuming php is running under apache, it will run as what ever user apache runs as.

An easy way to check ( again, assuming some unix like environment ) is to create a php file with:

<?php
    print shell_exec( 'whoami' );
?>

which will give you the user.

For my AWS instance, I am getting apache as output when I run this script.

Cretonne answered 14/10, 2011 at 18:6 Comment(0)
B
18

Straight from the shell you can run:

php -r "echo exec('whoami');"
Backcross answered 9/2, 2019 at 3:35 Comment(0)
L
16

You can try using backticks like this:

echo `whoami`;
Lobby answered 14/10, 2011 at 18:2 Comment(2)
It's just duplicate of questions above.Chimere
Might be useful to some users: if you run this from the command line interface you will get the user that runs the command, and not the PHP user.Bulganin
A
15

I would use:

  • lsof -i
  • lsof -i | less
  • lsof -i | grep :http

You can type any of these in your ssh command line and you will see which user is listening to each service.

You can also check this file:

more /etc/apache2/envvars

and look for these lines:

export APACHE_RUN_USER=user-name
export APACHE_RUN_GROUP=group-name

To filter out envvars file data, you can use grep:

more /etc/apache2/envvars | grep APACHE_RUN_
Adal answered 8/2, 2014 at 20:52 Comment(1)
grep "APACHE_RUN_" /etc/apache2/envvars worked well, thanks.Bushnell
G
14

exec('whoami') will do this

<?php
echo exec('whoami');
?>
Geter answered 14/10, 2011 at 17:58 Comment(3)
Can't really use exec or system or any such passthru functions.Afore
@Wesley: so that's not possible.Geter
Might be useful to some users: if you run this from the command line interface you will get the user that runs the command, and not the PHP user.Bulganin
C
1

add the file info.php to the following directory - your default http/apache directory - normally /var/www/html

with the following contents

<?php                                                                           
phpinfo();                                                                    
?>  

Then httpd/apache restart the go to your default html directory http://enter.server.here/info.php

would deliver the whole php pedigree!

Copra answered 10/6, 2017 at 14:6 Comment(3)
and which variable / value in that output are we looking for ?Mycetozoan
I am sure you have already found this but search for "user" on that page?Copra
It's USERNAME in the Environment section.Topping
B
0

In my setup I want to check if the current process has permission to create folders, subfolders and files before I begin a process and suggest a solution if it looks like I can't. I wanted to run stat(<file>) on various things to ensure the permissions match those of the running process (I'm using php-fpm so it varies depending on the pool).
The posix based solution Mario gave above, seems perfect, however it seems the posix extension is --disabled so I couldn't do the above and as I want to compare the results with the response from running stat() running whoami in a separate shell isn't helpful either (I need the uid and gid not the username).

However I found a useful hint, I could stat(/proc/self) and stat(/proc/self/attr) and see the uid and gid of the file.

Hope that helps someone else

Babel answered 22/4, 2014 at 17:4 Comment(0)
W
0

Proposal

A tad late, but even though the following is a work-around, it solves the requirement as this works just fine:

<?
    function get_sys_usr()
    {
        $unique_name = uniqid();  // not-so-unique id
        $native_path = "./temp/$unique_name.php";
        $public_path = "http://example.com/temp/$unique_name.php";
        $php_content = "<? echo get_current_user(); ?>";
        $process_usr = "apache";  // fall-back

        if (is_readable("./temp") && is_writable("./temp"))
        {
            file_put_contents($native_path,$php_content);
            $process_usr = trim(file_get_contents($public_path));
            unlink($native_path);
        }

        return $process_usr;
    }


    echo get_sys_usr();  // www-data
?>


Description

The code-highlighting above is not accurate, please copy & paste in your favorite editor and view as PHP code, or save and test it yourself.

As you probably know, get_current_user() returns the owner of the "current running script" - so if you did not "chown" a script on the server to the web-server-user it will most probably be "nobody", or if the developer-user exists on the same OS, it will rather display that username.

To work around this, we create a file with the current running process. If you just require() this into the current running script, it will return the same as the parent-script as mentioned; so, we need to run it as a separate request to take effect.

Process-flow

In order to make this effective, consider running a design pattern that incorporates "runtime-mode", so when the server is in "development-mode or test-mode" then only it could run this function and save its output somewhere in an include, -or just plain text or database, or whichever.

Of course you can change some particulars of the code above as you wish to make it more dynamic, but the logic is as follows:

  • define a unique reference to limit interference with other users
  • define a local file-path for writing a temporary file
  • define a public url/path to run this file in its own process
  • write the temporary php file that outputs the script owner name
  • get the output of this script by making a request to it
  • delete the file as it is no longer needed - or leave it if you want
  • return the output of the request as return-value of the function
Weariful answered 26/5, 2017 at 10:37 Comment(0)
G
0

You can use these commands :

<? system('whoami');?>

or

<? passthru('whoami');?>

or

<? print exec('whoami');?>

or

<? print shell_exec('whoami');?>

Be aware, the get_current_user() returns the name of the owner of the current PHP script !

Gentlefolk answered 23/5, 2019 at 0:38 Comment(2)
get_current_user() does not return the current user. It returns the owner of the script the function is called in.Entomb
why the vote-downs? it's a good answer.Precipitant
C
-1

I usually use

<?php echo get_current_user(); ?>

I will be glad if it helped you

Coupe answered 3/6, 2016 at 11:53 Comment(2)
This has already been mentioned in the comments under the question and it does have only very little to do with the user the script is running under.Weatherman
get_current_user() - Gets the name of the owner of the current PHP script - not the user running the PHP process.Vassaux
S
-1
$_SERVER["USER"]

$_SERVER["USERNAME"] 
Sashenka answered 11/10, 2016 at 14:3 Comment(1)
echo $_SERVER["USER"]; works but gives the current logged in user name in SSH.Bushnell
H
-2
<?php phpinfo(); ?>

save as info.php and

open info.php in your browser

ctrl+f then type any of these:

APACHE_RUN_USER
APACHE_RUN_GROUP
user/group

you can see the user and the group apache is running as.

Humanly answered 13/11, 2018 at 0:22 Comment(1)
There are none of these lines in phpinfoReign
A
-3
$user = $_SERVER['REMOTE_USER'];

http://php.net/manual/en/reserved.variables.server.php

Authenticated user

Apostatize answered 4/10, 2018 at 0:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.