Home Backend Development PHP Tutorial Use PHP script to change Linux or Unix system password_PHP tutorial

Use PHP script to change Linux or Unix system password_PHP tutorial

Jul 21, 2016 pm 02:57 PM
linux php unix use Revise Password and install Install tool system Script software

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364029.htmlTechArticleRequired 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: Installation can...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Apr 18, 2025 am 09:24 AM

When developing websites using CraftCMS, you often encounter resource file caching problems, especially when you frequently update CSS and JavaScript files, old versions of files may still be cached by the browser, causing users to not see the latest changes in time. This problem not only affects the user experience, but also increases the difficulty of development and debugging. Recently, I encountered similar troubles in my project, and after some exploration, I found the plugin wiejeben/craft-laravel-mix, which perfectly solved my caching problem.

Solve the PHP timeout problem: application of phpunit/php-invoker library Solve the PHP timeout problem: application of phpunit/php-invoker library Apr 17, 2025 pm 11:45 PM

When developing PHP projects, you often encounter the problem that some functions or methods have been executed for too long, causing program timeout. I've tried multiple solutions, but the results are not satisfactory until I discovered the phpunit/php-invoker library. This library completely solved my problem by setting the timeout time to call the executable function.

How to use Composer to manage PHP project version number How to use Composer to manage PHP project version number Apr 18, 2025 am 06:24 AM

Version control is a key link when managing PHP projects. Recently I was working on a Git-based PHP project and I encountered a problem: how to automatically generate and manage version numbers during development. This problem seems simple, but manual maintenance of the version number is not only cumbersome, but also prone to errors. After some exploration, I found a very useful tool - the sebastian/version library, which was easily integrated into the project through Composer, completely solving my troubles.

See all articles