温馨提示×

怎么重置ubuntu apache2密码

小樊
37
2026-09-17 09:33:43
栏目: 智能运维

在 Ubuntu 上,Apache2 本身没有“用户密码”这个概念,你大概率是遇到了下面几种情况之一。我按常见场景给你说明怎么“重置”。


一、你是忘了 Linux 系统用户密码(如 root / 普通用户)

Apache 是跑在系统用户下的,如果你是说进不了服务器 / sudo 要用密码

重置 Ubuntu 用户密码

  1. 重启服务器
  2. 进入 Recovery Mode(恢复模式)
  3. 选择 root shell
  4. 执行:
    mount -o remount,rw /
    passwd 用户名
    
  5. 输入新密码
  6. 重启:
    reboot
    

二、你是忘了 Apache 基本认证(.htpasswd)密码

常见于:

  • 访问 http://ip/admin 弹出账号密码
  • 用了 AuthType Basic

重置 .htpasswd 密码

找到 .htpasswd 文件(通常在 /etc/apache2/ 下):

sudo htpasswd -c /etc/apache2/.htpasswd 用户名
  • -c 是新建(会覆盖)
  • 不加 -c 是修改已有用户

示例:

sudo htpasswd /etc/apache2/.htpasswd admin

然后重启 Apache:

sudo systemctl restart apache2

三、你是忘了 MySQL / MariaDB 密码(常和 Apache 一起用)

如果其实是数据库密码忘了:

sudo systemctl stop mysql
sudo mysqld_safe --skip-grant-tables &
mysql
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';

然后重启 MySQL。


四、你是说 phpMyAdmin 登录密码

那是 MySQL 用户密码,不是 Apache 的。


五、你是说 Apache 没密码但想加

sudo apt install apache2-utils
sudo htpasswd -c /etc/apache2/.htpasswd user1

Apache 配置里加:

<Directory /var/www/html/admin>
    AuthType Basic
    AuthName "Restricted"
    AuthUserFile /etc/apache2/.htpasswd
    Require valid-user
</Directory>

如果你能告诉我:

  • 登录系统 还是 网页弹窗
  • 报错截图或提示文字

我可以直接给你精确命令。

0