PHP Analysis on Shell Script Permission Issues

小云云
Release: 2023-03-20 15:28:02
Original
1590 people have browsed it

1. Introduction to sudo

sudo allows root to assign some rights that ordinary users do not have, so that ordinary users can also have relevant permissions of the root user (for example, sudo can be assigned to a certain user certain command execution permission).
One of the important files for sudo is /etc/sudoers. Which users have what kind of commands can be executed depends on this file. Mainly, only users assigned in it can use sudo.

sudo -l 列出该用户能执行的命令  
sudo -u user 以指定用户来执行命令  
sudo -k 清楚入场券上的时间,下次使用sudo还要输入密码  
sudo -b 在后台执行命令
Copy after login
Copy after login

Enter vim in the terminal to open the /etc/sudoers file.
Find the place as shown in the picture:
PHP Analysis on Shell Script Permission Issues

The content of the red box in the picture was added by myself later, where www is the user running php, and the default is in different environments The name will be different. My name under centos is www.

Users who can view the running of php can obtain it through the following php web page.

<?php    
    echo shell_exec("id -a");  
?>
Copy after login
Copy after login

Look at the picture. There are three ALLs in each line. The first ALL represents the host in the network. The ALL in the second bracket represents the target user, indicating who is executing the command. The last ALL Represents the executable command name (you can specify the command you want the user to be able to execute).

NOPASSWD means no password is required. (For convenience, otherwise, if I access the program through a browser, there will be an extra step to fill in the password and wait for feedback. It is really troublesome, so it will be much easier)

If it is Ubuntu, there is no need to modify it. If it is Fedora If so, you still need to make some modifications.

Defaults Just open env_reset (that is, remove the # in front);

  1. Check whether it is opened in the php.ini configuration file Safe mode

safe_mode =  off 这样就可以了
Copy after login
Copy after login

There are multiple functions for php to execute Linux scripts, such as shell_exec(), system(), etc. I won’t introduce them here. Just use them. I use system ()function.
route.php

<?php   $ip=(string)$_POST["ip"];  
system("su ./route.sh $ip",$status);  
if($status == &#39;true&#39;)  
{  
    echo "设置成功";  
}  
else  {  
    echo "设置失败";  
}  
?>
Copy after login
Copy after login

After the previous settings are completed, after execution, the browser will display the setting as successful, indicating that the script was successfully executed.

1.php execution shell is actually executed by the user of Apache or Nginx, and php itself does not create a user.
2.Reprinted

Since I used to log in and operate as the root user in ECS every time, sudo was basically not used, but recently the project needs to do this: pass When the browser accesses the php web page, a script under Linux will be called and executed in the php web page. Originally, this matter was very simple, but when I actually operated it, I found that every time I accessed it through the browser, the script was not executed, and there was no problem when I executed the php web page through the command line in the terminal. I thought it was very different. Then I thought about it carefully and found that accessing through a browser is different from executing the initiator directly in the terminal. I thought it might be a problem with user permissions, so I checked online and found sudo. thing.
1. Introduction to sudo

sudo allows root to assign some rights that ordinary users do not have, so that ordinary users can also have relevant permissions of the root user (for example, sudo can be assigned to a certain user to execute certain commands. permissions).
One of the important files for sudo is /etc/sudoers. Which users have what kind of commands can be executed depends on this file. Mainly, only users assigned in it can use sudo.

sudo -l 列出该用户能执行的命令  
sudo -u user 以指定用户来执行命令  
sudo -k 清楚入场券上的时间,下次使用sudo还要输入密码  
sudo -b 在后台执行命令
Copy after login
Copy after login

Enter vim in the terminal to open the /etc/sudoers file.
Find the place as shown in the picture:
PHP Analysis on Shell Script Permission Issues

The content of the red box in the picture was added by myself later, where www is the user running php, and the default is in different environments The name will be different. My name under centos is www.

Users who can view the running of php can obtain it through the following php web page.

<?php    
    echo shell_exec("id -a");  
?>
Copy after login
Copy after login

Look at the picture. There are three ALLs in each line. The first ALL represents the host in the network. The ALL in the second bracket represents the target user, indicating who is executing the command. The last ALL Represents the executable command name (you can specify the command you want the user to be able to execute).

NOPASSWD means no password is required. (For convenience, otherwise, if I access the program through a browser, there will be an extra step to fill in the password and wait for feedback. It is really troublesome, so it will be much easier)

If it is Ubuntu, there is no need to modify it. If it is Fedora If so, you still need to make some modifications.

Defaults Just open env_reset (that is, remove the # in front);

  1. Check whether it is opened in the php.ini configuration file Safe mode

safe_mode =  off 这样就可以了
Copy after login
Copy after login

There are multiple functions for php to execute Linux scripts, such as shell_exec(), system(), etc. I won’t introduce them here. Just use them. I use system ()function.
route.php

<?php   $ip=(string)$_POST["ip"];  
system("su ./route.sh $ip",$status);  
if($status == &#39;true&#39;)  
{  
    echo "设置成功";  
}  
else  {  
    echo "设置失败";  
}  
?>
Copy after login
Copy after login

After the previous settings are completed, after execution, the browser will display the setting as successful, indicating that the script was successfully executed.

  1. The php execution shell is actually executed by the user of Apache or Nginx, and php itself does not create a user.

related suggestion:

How does PHP solve the problem of not generating core files when executing shell scripts

Example of one-click installation of php7 by shell script

shell script Command example

The above is the detailed content of PHP Analysis on Shell Script Permission Issues. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!