Home  >  Article  >  Backend Development  >  Share the method of changing Linux or Unix password through PHP_PHP tutorial

Share the method of changing Linux or Unix password through PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:21:26672browse

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 this script, and then copy it to your web root directory or another location on the web server (readable by users):

  $ cp chpasswd /var/www/ Or, if you use Lighttpd web server:

$ cp chpasswd /home/lighttpd Step 2: Execute the command as root through sudo

Apache or Lighttpd web server will use non-root permissions immediately after entering the background to run. 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 the 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 URL in the address bar of your web browser and you will see a username and password prompt:

If the password change is successful, you will get the confirmation prompt:

Due to some reasons, if the password change fails, you can refer to the following prompts to get more detailed error information:

Steps 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324889.htmlTechArticleRequired tools and installation: You must install the following tools and software: – Shell script to change password; – Sudo Access rights; – Apache or Lighttpd web server; – PHP server program. Step...
Statement:
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