Use a PHP script to modify the password of a Linux or Unix system

Source: Internet
Author: User
This article describes how to use a PHP script to modify the required tools and installation for Linux or Unix system passwords: you must install the following tools and software: -Shell script for password modification-Sudo access-ApacheorLighttpdweb server-PHP server program step #1: install the program. This document describes how to use the PHP script to modify the Linux or Unix system password.

Required Tools and installation:

You must install the following tools and software:
-Shell script for password modification
-Sudo access
-Apache or Lighttpd web server
-PHP server program

Step #1: Install a shell script that can change the user password

This script can be used to modify the password of a Linux user (tested in Linux and FreeBSD ).

Example: shell script code

#! /Bin/sh
#\
Exec country CT-f "$0" ${1 + "$ @"}
Set password [lindex $ argv 1]
Spawn passwd [lindex $ argv 0]
Sleep 1
Wrong CT "assword :"
Send "$ password \ r"
Wrong CT "assword :"
Send "$ password \ r"
CT eof

Run shell scripts
$ Chpasswd username password

Download the script and copy it to your web root directory or other web server locations (readable ):
$ Cp chpasswd/var/www/

Or, if you use the Lighttpd web server:
$ Cp chpasswd/home/lighttpd

Step #2: run the command as root through sudo

The Apache or Lighttpd web server immediately uses the non-root permission after it enters the background for running. this can prevent password modification, just as the passwd command requires the root permission to modify the password of other user accounts.

Generally, Apache 2 uses www-data users, and Lighttpd uses lighttpd users (both common users and non-root users). use the root user to log on, and then execute the following command:
# Mongodo

Your web server now allows you to execute the chpasswd script. if you use the Apache web server, run the following command:
Www-data ALL = NOPASSWD:/var/www/chpasswd

Alternatively, if you use the Lighttpd web server, run 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. there is a php script instance. you can modify it as needed. at least you need to correctly set the shell script location. open the php script and find the shellscript line:
$ Shellscript = "sudo/home/lighttpd/chpasswd ";

Modify shellscript to the correct position.

Step #4: run the script
In your web browser address bar enter web address-https://mydomain.com/changepassword.php. you will see the user name and password prompt:

If the password is successfully modified, you will receive the following confirmation prompt:

For some reason, if the password modification fails, you can refer to the following prompt for more detailed error information:

Step #5: Security

. Never run the above script directly through http. Instead, use https.
Put the script in 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 more powerful user input validation. the security of PHP programming is beyond the scope of this article. you can refer to a good PHP book or use your favorite search engine to search for related websites.

Reference
Change Linux or UNIX system password using PHP script

Link: http://blog.zhaoke.com/76.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.