Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6950147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:03:26+00:00 2026-05-27T14:03:26+00:00

I have a CentOS 5.7 linux server and use php5.3.x. On a pfSense system,

  • 0

I have a CentOS 5.7 linux server and use php5.3.x.

On a pfSense system, you can restart services-that required root permissions using a php web page.

I’m trying to do something similar, I have written some php code to execute shell commands. For example, to restart the sshd service:

<?php
exec('/sbin/service sshd restart');
?>

and I tried to execute that command via exec function, but it needs root permission, but we have a apache user authority.

I have come across a few solutions:

  1. “run apache with root user”
    really unsafe. I do not want to do that.
  2. “apache ALL=NOPASSWD:/sbin/service to /etc/sudoers”
    I tried but and still have a problem.

Any other solutions?
Thanks for answers.


now.. it’s interesting. i tried @refp post and it worked my local ubuntu server. But when i tried same at my cenOS vps server. It’s not working.and that is apache’s error log “rm: cannot remove `/var/lock/subsys/vsftpd’: Permission denied”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:03:26+00:00Added an answer on May 27, 2026 at 2:03 pm

    Read this whole post before trying it out, there are choices to be made.


    Solution using a binary wrapper (with suid bit)

    1) Create a script (preferrably .sh) that contains what you want to be ran as root.

    # cat > php_shell.sh <<CONTENT
      #!/bin/sh
      /sbin/service sshd restart
    CONTENT
    

    2) This file should be owned by root, and since it will later run with root permissions make sure that only root has permission to write to the file.

    # chown root php_shell.sh
    # chmod u=rwx,go=xr php_shell.sh
    

    3) To run the script as root no matter what user that executes it, we will need a binary wrapper. Create one that will execute our php_shell.sh.

    # cat > wrapper.c <<CONTENT
      #include <stdlib.h>
      #include <sys/types.h>
      #include <unistd.h>
    
      int
      main (int argc, char *argv[])
      {
         setuid (0);
    
         /* WARNING: Only use an absolute path to the script to execute,
          *          a malicious user might fool the binary and execute
          *          arbitary commands if not.
          * */
    
         system ("/bin/sh /path/to/php_shell.sh");
    
         return 0;
       }
    CONTENT
    

    4) Compile and set proper permissions, including the suid bit (saying that it should run with root privileges):

    # gcc wrapper.c -o php_root
    # chown root php_root
    # chmod u=rwx,go=xr,+s php_root
    

    php_root will now run with root permissions, and execute the commands specified in php_shell.sh.


    If you don’t need to the option to easily change what commands that will be executed I’d recommend you to write the commands directly in wrapper.c under step 4. Then you don’t need to have a binary executing a external script executing the commands in question.

    In wrapper.c, use system ("your shell command here"); to specify what commands you’d like to execute.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have ImageMagick installed on my CentOS linux server. I am trying to get
I have recently installed Subversion onto a Linux server ( CentOS ) and everything
We have Xserver-less CentOS system for continuous integration. So no UI. I want to
I have a Red Had Enterprise Linux 5 and a CentOS 5 box, both
I have linux centos 5.3. I am in progress to create a website but
On a CentOS Linux machine, developers have user accounts. They need to be able
I have a Spring+hibernate+mysql+jstl architecture, running in a linux CENTOS production enviroment. I added
I have some Perl/CGI programs on a CentOS Linux webserver. I wish to write
I run a DV 3.5 server on MediaTemple with Linux CentOS 5, php and
I need some guidance to fix my server. I have a VPS with CentOS,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.