Home > Article > Backend Development > Use PHP script to change Linux or Unix system password_PHP tutorial
Required tools and installation:
You must install the following tools and software:
– Shell script to change password;
– Sudo access;
– Apache or Lighttpd web server;
– PHP server program.
Step 1: Install a shell script that can modify user passwords
This script can actually be used to modify the password of Linux users (tested on Linux and FreeBSD).
Example: shell script code
#!/bin/sh # exec expect -f “$0″ ${1+”$@”} set password [lindex $argv 1 ] spawn passwd [lindex $argv 0] sleep 1 expect “assword:” send “$passwordr” expect “assword:” send “$passwordr” expect eof
Run shell script (download link):
$ chpasswd username password
Download the script, and then copy it to your web root directory or other location of the web server (readable by users):
$ cp chpasswd /var/www/
Or, if you use the Lighttpd web server:
$ cp chpasswd /home/lighttpd
Step 2: Execute the command as root through sudo
The Apache or Lighttpd web server will use non-root permissions immediately after running in the background. This can prevent password changes very well, just like the passwd command requires root privileges to change the passwords of other user accounts.
Usually, Apache 2 uses the www-data user, and Lighttpd uses the lighttpd user (both are ordinary users, non-root users). Log in as the root user, and then execute the following command:
# visudo Now your web server allows the execution of the password modification script (chpasswd). If you use the Apache web server, execute the following command:
www-data ALL=NOPASSWD: /var/www/chpasswd
Or, if you use Lighttpd web Server, execute the following command:
httpd ALL=NOPASSWD: /home/lighttpd/chpasswd
Save and exit the file.
Step 3. Create a PHP-based interface
Now you need to write a php script. Here is an example php script. You can modify it according to your needs. At the very least you need to set up the shell script location correctly. Open the php script and find the shellscript line:
$shellscript = “sudo /home/lighttpd/chpasswd”;
Modify the shellscript to point to the correct location. Download the PHP source code from here:
Step 4: Run the script
Enter the address - https://mydomain.com/changepassword.php - in the address bar of your web browser. You will see a username and password prompt:
If the password change is successful, you will get a confirmation prompt:
Due to some reasons, if the password change fails, you can refer to the following tips for more information More detailed error message:
Step 5: Security
◆Never run the above script directly via http protocol. Instead, use https protocol.
◆Put the script into a password-protected directory.
◆Never trust user input. The above php script is just an example. In a real production environment, you need to consider implementing stronger user input validation. Discussing security in PHP programming is beyond the scope of this article. You can refer to a good PHP book or search for relevant websites using your favorite search engine.