Home > Article > Backend Development > unixtimestamp Share how to modify Linux or Unix password through PHP
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 runs the shell script (download link):
$ chpasswd username password Download the script, and then copy it to your web root directory or other locations on the web server Location (readable by user):
$ cp chpasswd /var/www/ Or, if you use Lighttpd web server:
$ cp chpasswd /home/lighttpd Step 2: Execute the command as root via sudo
Enter Apache or Lighttpd web server Non-root permissions will be used 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.
Normally, 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 the Lighttpd web server, execute the following command:
httpd ALL=NOPASSWD: /home/lighttpd /chpasswd saves and exits files.
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 web address in the address bar of your web browser, you will see the username and password prompt:
If the password is changed successfully, you will get the confirmation prompt:
For some reasons, if the password modification fails, you can refer to the following tips to get more detailed error information:
Step 5: Security
◆Never run the above script directly through the http protocol. Instead, use the 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 using 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.
The above has introduced how to use unixtimestamp to modify Linux or Unix passwords through PHP, including the content of unixtimestamp. I hope it will be helpful to friends who are interested in PHP tutorials.