Determining the username under which PHP is running

I wanted to determine the user name under which PHP was running for a WordPress blog on a hosting site. To do so, I placed a PHP script, whoami_here.php, in the home folder for the WordPress installation and then accessed the webpage for that script via a browser, e.g.

There are a variety of methods you can use to check on which account PHP is running under, though some methods may not work on some systems.

  1. One way is to use <?php passthru("whoami"); ?>. The hosting account user id in this case was johnadoe, so that is what I saw displayed by the PHP function, confirming that PHP was running under the account that was in use for the hosting account.
  2. You can also use <?php shell_exec("whoami"); ?>
  3. Another alternative is <?php exec('whoami'); ?>.
  4. Or you can use <?php echo `whoami`; ?>.
  5. You can also use the Posix posix_geteuid function which returns an integer value corresponding to the userid you would find in /etc/password for the account.

      print posix_geteuid();

    You might see something like the following displayed:


    You could then check for the username for the account to which that user ID corresponds in /etc/passwd.

    $ grep 2159567 /etc/passwd

    Note: I've found this approach did not work on the CentOS 7 systems on which I tested it. And not only didn't it work, the presence of the code in a web page terminated the further display of any HTML code for that page at the point where the code was placed. David Newcomb states in Posix PHP commands not working under CentOS 7 that none of the Posix PHP functions are available under CentOS 7 noting:

    I did a bit of hunting around and I found that RHEL and CentOS distributions of Linux compiled their version of PHP without this module, sighting that it was a security hole to allowed these commands to be run.

    He provdies instructions on how to install that functionality on CentOS systems in his article, if you need those Posix functions to work. In this case I didn't, but, until I found his article, I couldn't figure out why not only wasn't I seeing any results when trying to display information from those functions, but I wasn't seeing anything else output after the point where I inserted them, not even the ending body and html tags, even though the functions seemed to exist. When I encountered the problem, I inserted test conditions in the code to verify the fucntions existed:

    if (function_exists('posix_geteuid')); {
      print "Function posix_geteuid exists<br>"; }
    if (function_exists('posix_getpwuid')); {
      print "Function posix_getpwuid exists<br>"; }

    I saw the following displayed on the web page:

    Function posix_geteuid exists
    Function posix_getpwuid exists

    Yet I could not get any output when I tried to display a result for posix_geteuid() or posix_getpwuid(48) when I tried to see what would happen if I specified the apache user ID in the posix_getpwuid function.

  6. Or, if posix_geteuid works, you can use the posix_getpwuid function to convert the user ID to a user name as explained by Justin Samuel at get_current_user in a comment regarding cases where you wish to get the username of the process owner rather than the username of the file owner.

      $processUser = posix_getpwuid(posix_geteuid());
      print $processUser['name'];

    If the code above is used, you would see johnadoe displayed on the web page.

  7. Yet another method is to create a file on the system with PHP and test the ownership of that file, since it will be created with the owner being the account under which PHP is running. The file can be created with file_put_contents.

    file_put_contents("aTestFile", "testing");
    print fileowner("aTestFile");

    The above method will only work if the account under which PHP is being run can create a file in the directory where the script is placed. If the script can create a file there it will create one named aTestFile with the word "testing" in it. The file will be removed after the fileowner is displayed. The fileowner will be the userid, e.g., 2159567. You can search for that user ID in /etc/passwd as in the prior example.

  8. Since the account may not have write access to that directory, you can also use a variant of the approach immediately above. You can place the file in the /tmp directory, instead. The tempnam function can be used for that purpose. The function "Creates a file with a unique filename, with access permission set to 0600, in the specified directory. If the directory does not exist or is not writable, tempnam() may generate a file in the system's temporary directory, and return the full path to that file, including its name." E.g.:

    $tmpfname = tempnam("/tmp", "aTestFile");
    $handle = fopen($tmpfname, "w");
    fwrite($handle, "writing to tempfile");
    print fileowner($tmpfname);

In addition to the POSIX functions not working on the CentOS 7 systems, I found that the shell_exec method did not work on the CentOS 7 systems. PHP was running as apache on those systems. On those CentOS 7 Linux systems, the apache account was configured to not grant a login shell for the account. I.e.:

$ grep apache /etc/passwd

The passthru("whoami") function, echo exec('whoami') , echo `whoami`, and tempnam methods worked on all of the systems I tested, however. See whoami_here, a text file with the code for all of the tests listed above, if you wish to try them on a system.


  1. How to check what user php is running as?
    Posted: October 14, 2011
    Stack Overflow
  2. Posix PHP commands not working under CentOS 7
    By: David Newcomb
    Date: December 8, 2014
    BigSoft - We'll do I.T. for you


TechRabbit ad 300x250

Justdeals Daily Electronics Deals1x1 px