Home php教程 php手册 使用PHP脚本修改Linux或Unix系统口令

使用PHP脚本修改Linux或Unix系统口令

Jun 13, 2016 am 11:39 AM
linux php unix use Revise Password and install Install tool system Script software

需要的工具和安装:

你必须安装下面的工具和软件:



– 修改口令的Shell脚本;

– Sudo 访问权;

– Apache or Lighttpd web 服务器;

– PHP服务端程序。



步骤1: 安装可以修改用户口令的shell脚本

该脚本可以实际用于修改Linux用户的口令(已在Linux和FreeBSD测试)。

例子: shell脚本代码


#!/bin/sh # \ exec expect -f “$0″ ${1+”$@”} set password [lindex $argv 1] spawn passwd [lindex $argv 0] sleep 1 expect “assword:” send “$password\r” expect “assword:” send “$password\r” expect eof


运行shell脚本(下载链接):


$ chpasswd username password


下载该脚本, 然后拷贝到你的web根目录或者web服务器的其它位置(用户可读):


$ cp chpasswd /var/www/



或者, 如果你使用Lighttpd web服务器:


$ cp chpasswd /home/lighttpd


步骤2: 通过sudo以root身份执行命令

Apache或Lighttpd web服务器进入后台运行后会马上使用非root权限。这样可以很好的防止口令修改, 就像passwd命令需要root权限才能修改其它用户帐号的口令。

通常, Apache 2使用www-data用户, Lighttpd使用lighttpd用户(皆为普通用户, 非root用户)。使用root用户登陆, 然后执行下面的命令:

# visudo现在你的web服务器允许执行口令修改脚本(chpasswd)。如果你使用Apache web服务器, 执行下面的命令:


www-data ALL=NOPASSWD: /var/www/chpasswd


或者, 如果你使用Lighttpd web服务器, 执行下面的命令:


httpd ALL=NOPASSWD: /home/lighttpd/chpasswd


保存和退出文件。

步骤3. 创建一个基于PHP的接口

现在你需要写一个php脚本。这里有一个php脚本实例。你可以根据你的需要来修改。至少你需要正确设置好的shell脚本位置。打开php脚本和找到shellscript一行:


$shellscript = “sudo /home/lighttpd/chpasswd”;


修改shellscript指向到正确的位置。PHP的源代码从这里下载:

步骤4: 运行脚本

在你的web浏览器地址栏输入网地址 - https://mydomain.com/changepassword.php。你将会看到用户名和口令提示:

如果口令修改成功, 你会得到的确认提示:

由于一些原因, 如果口令修改失败, 你可以参考下面提示获得更多详细的错误信息:

步骤5: 安全

◆永远不要通过http协议直接运行上面的脚本. 而是使用https协议。

◆把脚本放入到受口令保护的目录。

◆永远不要信任用户的输入。上面的php脚本只是一个例子。在现实的生产环境中, 你需要考虑采用更强大的用户输入确认。讨论PHP编程的安全超出了本文的范围。你可以参考一本好的PHP书籍或者使用你喜欢的搜索引擎搜索相关的网站。

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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
How reliable is Binance Plaza? How reliable is Binance Plaza? May 07, 2025 pm 07:18 PM

Binance Square is a social media platform provided by Binance Exchange, aiming to provide users with a space to communicate and share information related to cryptocurrencies. This article will explore the functions, reliability and user experience of Binance Plaza in detail to help you better understand this platform.

phpMyAdmin's Function: Interacting with MySQL (SQL) phpMyAdmin's Function: Interacting with MySQL (SQL) May 07, 2025 am 12:16 AM

phpMyAdmin simplifies MySQL database management through the web interface. 1) Create databases and tables: Use graphical interface to operate easily. 2) Execute complex queries: such as JOIN query, implemented through SQL editor. 3) Optimization and best practices: including SQL query optimization, index management and data backup.

The 5 Essential Elements of Linux: Explained The 5 Essential Elements of Linux: Explained May 07, 2025 am 12:14 AM

The five core elements of Linux are: 1. Kernel, 2. Command line interface, 3. File system, 4. Package management, 5. Community and open source. Together, these elements define the nature and functionality of Linux.

Composer: Installing, Updating, and Managing Dependencies Composer: Installing, Updating, and Managing Dependencies May 07, 2025 am 12:07 AM

Composer is a dependency management tool for PHP, and manages project dependencies through composer.json and composer.lock files. 1. Install Composer: Run a specific command and move composer.phar to the system path. 2. Update Composer: Use composelself-update command. 3. Dependency management: add dependencies through the composerrequire command, automatically update relevant files and download packages.

2025 Binance Binance Exchange Latest Login Portal 2025 Binance Binance Exchange Latest Login Portal May 07, 2025 pm 07:03 PM

As the world's leading cryptocurrency exchange, Binance is always committed to providing users with a safe and convenient trading experience. Over time, Binance has continuously optimized its platform features and user interface to meet the changing needs of users. In 2025, Binance launched a new login portal aimed at further improving the user experience.

Best PHP Performance Optimization Techniques Best PHP Performance Optimization Techniques May 07, 2025 pm 03:05 PM

PHP performance optimization can be achieved through the following steps: 1) use require_once or include_once on the top of the script to reduce the number of file loads; 2) use preprocessing statements and batch processing to reduce the number of database queries; 3) configure OPcache for opcode cache; 4) enable and configure PHP-FPM optimization process management; 5) use CDN to distribute static resources; 6) use Xdebug or Blackfire for code performance analysis; 7) select efficient data structures such as arrays; 8) write modular code for optimization execution.

The latest download tutorial for Ouyi OKX6.118.0 version The latest download tutorial for Ouyi OKX6.118.0 version May 07, 2025 pm 06:51 PM

The latest download tutorial for Ouyi OKX6.118.0 version: 1. Click on the quick link in the article; 2. Click on the download (if you are a web user, please register the information first). The latest Android version v6.118.0 optimizes some functions and experiences to make trading easier. Update the app now to experience a more extreme trading experience.

2025 Binance latest address 2025 Binance latest address May 07, 2025 pm 06:57 PM

As the world's leading cryptocurrency exchange, Binance is always committed to providing users with a safe and convenient trading experience. Over time, Binance has continuously optimized its platform features and user interface to meet the changing needs of users. In 2025, Binance launched a new login portal aimed at further improving the user experience.

See all articles