Monday, 4 July 2016

Run sudo command with php or Apache in linux

Ubuntu run apache as the www-data user and sudo command ask for password everytime you run sudo command. If you want to use sudo without a password from a web page, you need to allow www-data user to run sudo command with no password.

Steps:

1. Run  sudo visudo  command.

2. add below lines at the end of the file.

www-data ALL=NOPASSWD: ALL

3. or you can specify only for specific program.

www-data ALL=NOPASSWD: /sbin/iptables


Below PHP code to ban ip using ufw firewall in Linux ubuntu.


 <form action="" method="post">  
 Enter IP : <input type="text" name="ip">  
 <input type="submit" value="Ban IP">  
 </form>  
   
 <?php  
   
 if(isset($_POST["ip"]))  
 {  
 $ip= $_POST["ip"];  
 echo "$ip";  
 $ban = shell_exec("sudo ufw deny from $ip");  
 echo "Banned".$ban;  
 }  
   
 ?>

4. Done 

No comments:

Post a Comment