SUDO shell command and sudoers file
Have you anyone ever faced any problems while executing the SUDO shell command in PHP? I had faced that problem yesterday. I had tried to execute the Asterisk server commands from PHP in Linux environment. Asterisk server was already started in the local machine. I developed a PHP program that help the users to register them self in the Asterisk server to use the SIP communication SIP phones in the on-line world. I needed to develop a logic that, first add the users in a file then which is inside the Asterisk server then needs to run a script that reload that file to the Asterisk server through PHP. To develop this logic I used the shell_exec command in PHP.
When I run the shell commands in the Super user mode - by using SUDO, its send an error message to the Apache log file.
[sudo] no password for nobody
Above error message had been encounted in the Apache log file. After some Googleing I found that who is that 'nobody' in my machine.
I made some modification in my PHP file to display the user who is accessing the SUDO command by writing this lines.
echo shell_exec("whoami");.
Then I found that who is that 'nobody'. Then I add some permission to that nobody in the sudoers file which is inside the /etc/ dir in the Linux environment. To edit that file I had to change the mode to 777. And after successful editing that sudoers file I needed to change the mode again to 440. Then only Apache could work with sudo shell scripts.
I add following line of code to give the ALL kind of permission to that user nobody. There after its work perfectly.. :)
nobody ALL= (ALL) NOPASSWD:ALL
Note : Don't give this kind of ALL permission to any users. It will be harmful to your system. So you can give only some permission to the particular commands that you needs to run in shell scripts. It will safe guade your machine from vulnerability attacks.
nobody localhost=(ALL) NOPASSWD:command_which_i_need_to_run
This is the line of code which is help you to run the particular command in the privileged mode. :)
enjoy.. :)
This Articles was posted on 10:52 AMMonday, May 25, 2009
|
Labels:
Experiences,
Info,
Linux,
Shell Scripts
|
0 comments:
Post a Comment