>>>You will need to have the NOPASSWD option set in sudoers for the php user
How is this done?
Main Topics
Browse All TopicsHello,
I am trying to login as root through sudo in php in the simplist form possible. Please assum the server root password is: 12345 and add the following command to the script:
root > /sbin/service httpd restart
If possible please don't use any outside files to find the password, i am not bothered about security issues.
Thanks so much for your help.
P.S
Other ways of logining in via root in php are a bonus,
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
No not this way
If you are using sudo you need to use the password or the NOPASSWD option as above, the only other way in Unix to get around passwords and put them in a command is to use something like eXpect (http://expect.nist.gov), but this would put your root password in plain text in a cgi script which is a bad idea
If you use sudo, you don't need to "su" again! visudo looks like:
apache ALL=(root) NOPASSWD: /sbin/service httpd restart
PS: you might want to replace apache with user "nobody", I normally run apache as
nobody.
and change your php script to:
exec ('/usr/local/bin/sudo /sbin/service httpd restart') ;
No password required!
Similar aproach as the sudo one, just a little simpler (and you are missing the sudo-logging in this way)
copy /bin/service to the apache/php-users homedirectory.
make the file owned by root (mostprobably it is already) and a group where only the apache/php-user is in.
chang permission on the file to 4550 (r-sr-x---, or in words: setuid and only readable / executable by owner and group)
in php
<?php
exec ('/home/apache/service httpd restart') ;
?>
Late again, I'll continue to disagree, especially in TA about unix and security, the security rule is, that you can login as root from console (physical access established), but nowhere else. Thus, all other answers may be right, just not in this TA. Try the TA for games if that is what you want to do, just playing around.
I disagree.
Business Accounts
Answer for Membership
by: liddlerPosted on 2004-12-14 at 03:40:07ID: 12817929
<?php
exec ('/usr/local/bin/sudo su - root -c /sbin/service httpd restart') ;
?>
You will need to have the NOPASSWD option set in sudoers for the php user