Elasticsearch 官方博客文档:https://blog.csdn.net/UbuntuTouch/article/details/118937942

找回集群密码, 在 Elasticsearch 的安装目录下,有一个 utility 叫做 elasticsearch-users

$ pwd
/Users/liuxg/elastic3/elasticsearch-7.13.0
$ ls bin/elasticsearch-users
bin/elasticsearch-users

 我们可以使用这个工具找回我们的用户密码。我们首先来查看一下这个工具的一些用法:

$ ./bin/elasticsearch-users --help
Manages elasticsearch file users
 
Commands
--------
useradd - Adds a file user
userdel - Deletes a file based user
passwd - Changes the password of an existing file based user
roles - Edit roles of an existing user
list - List existing file based users and their corresponding roles
 
Non-option arguments:
command              
 
Option             Description        
------             -----------        
-E <KeyValuePair>  Configure a setting
-h, --help         Show help          
-s, --silent       Show minimal output
-v, --verbose      Show verbose output

从上面,我们可以看出来,它可以创建和删除一些用户。这个为我们重设密码提供了便利。

假如我们忘记了超级用户 elastic 的密码,那么我们该怎么办呢?

我们使用如下的步骤:

1)首先,我们来创建另外一个超级用户 newadmin:

bin/elasticsearch-users useradd newadmin -p password -r superuser

2)我们接下来使用这个刚刚创建的超级用户来针对原有的 elastic 用户进行重置密码:

curl -s --user newadmin:password -XPUT "http://localhost:9200/_xpack/security/user/elastic/_password?pretty" -H 'Content-Type: application/json' -d '{"password":"password1"}'

在上面,我们重置用户 elastic 的密码为 password1。我们可以通过如下的命令来检查我们是否可以通过这个重置的密码来对集群进行访问:

curl --user elastic:password1 -X GET "http://localhost:9200?pretty"
$ curl --user elastic:password1 -X GET "http://localhost:9200?pretty"
{
  "name" : "liuxg",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "-vtUaRdHSnGJb5XdGC32bA",
  "version" : {
    "number" : "7.13.0",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "5ca8591c6fcdb1260ce95b08a8e023559635c6f3",
    "build_date" : "2021-05-19T22:22:26.081971330Z",
    "build_snapshot" : false,
    "lucene_version" : "8.8.2",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

修改 elastic 原密码

# 开启安全校验配置
# 修改 config/elasticsearch.yml
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true

# password 后为新密码, 控制台提示输入老密码
curl -XPOST -u elastic "http://localhost:9200/_security/user/elastic/_password" -H 'Content-Type: application/json' -d '{"password":"newpassword"}'

# 不提示直接改
curl -XPOST --u elastic:现密码 "http://localhost:9200/_security/user/elastic/_password" -H 'Content-Type: application/json' -d '{"password":"newpassword"}'

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐