Home  >  Article  >  Backend Development  >  How to Execute Root Commands in PHP on a Linux System with Apache User Authority?

How to Execute Root Commands in PHP on a Linux System with Apache User Authority?

Linda Hamilton
Linda HamiltonOriginal
2024-11-16 18:38:03405browse

How to Execute Root Commands in PHP on a Linux System with Apache User Authority?

Executing Root Commands via PHP

Problem:

In a Linux system with Apache user authority, how can you execute shell commands that require root privileges using PHP, without resorting to unsafe solutions or sudoers rules?

Solution 1: Binary Wrapper (with suid Bit)

  1. Create a Shell Script:

    • Create a script containing the root commands you want to execute (e.g., php_shell.sh).
  2. Set Script Permissions:

    • Set root as the file owner and grant write permissions only to root.
  3. Create a Binary Wrapper:

    • Create a C program that executes the shell script with suid bit permissions.
  4. Compile and Set Permissions:

    • Compile the wrapper (wrapper.c) and grant it root permissions, including the suid bit.

Alternative:

If you don't need to change the commands frequently, you can write them directly in the wrapper program, eliminating the need for an external script.

The above is the detailed content of How to Execute Root Commands in PHP on a Linux System with Apache User Authority?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn