“ 停ES服务,通过本地临时创建es超级管理员来修改。”
有时候我们会遇到密码忘记的情况,比如Elasticsearch默认的管理员elastic用户,这个就很头疼。如何重置忘记的Elasticsearch管理员密码呢?

步骤 1:停止 Elasticsearch 集群,每个节点执行,最终确认es服务关闭。
sudo systemctl stop elasticsearch
步骤 2:启用本地文件认证(File Realm)编辑 Elasticsearch 配置文件:
sudo vim etc/elasticsearch/elasticsearch.yml
添加或确认以下配置:
xpack.security.enabled: truexpack.security.authc.realms.file.file1.order: 0
Elasticsearch 8.x 默认启用安全功能,无需显式添加 xpack.security.enabled。
步骤 3:创建临时管理员用户,切换至 Elasticsearch 安装目录:
cd /usr/share/elasticsearch
创建临时用户(需在服务停止状态):
sudo ./bin/elasticsearch-users useradd temp_admin -p YourTemporaryPassword123! -r superuser
-r superuser:赋予用户超级管理员权限。
若提示 command not found,检查路径是否为 usr/share/elasticsearch/bin/elasticsearch-users。
步骤 4:启动 Elasticsearch 服务
sudo systemctl start elasticsearch# 等待服务完全启动(检查状态)sudo systemctl status elasticsearch
步骤 5:使用临时用户重置 elastic 密码
调用密码重置 API:
curl -u temp_admin:YourTemporaryPassword123! -XPOST "http://localhost:9200/_security/user/elastic/_password" -H "Content-Type: application/json" -d'{"password": "YourNewSecurePassword456!"}'
成功响应:{"_doc":{"_index":".security","_id":"elastic","_version":2,"result":"updated"}}
验证新密码:
curl -u elastic:YourNewSecurePassword456! http://localhost:9200/_cluster/health
预期输出显示集群健康状态(如 "status":"green")。
步骤 6:清理临时用户
curl -u elastic:YourNewSecurePassword456! -XDELETE "http://localhost:9200/_security/user/temp_admin"
常见问题排查
1. 文件认证配置不生效,启动后无法用临时用户登录。
检查 elasticsearch.yml 语法(缩进必须为空格,非 Tab)。
确认集群所有节点配置一致。
查看日志 var/log/elasticsearch/elasticsearch.log 定位错误。
2. API 调用返回 403 权限错误,临时用户权限不足或密码错误。
# 检查临时用户权限curl -u temp_admin:YourTemporaryPassword123! http://localhost:9200/_security/_authenticate
3. 集群节点间配置不一致,部分节点无法加入集群。
在所有节点重复步骤 2-5。
重启后检查集群状态:
curl -u elastic:YourNewSecurePassword456! http://localhost:9200/_cat/nodes?v




