Elasticsearch 设置最低安全性

本文档介绍了如何在Elasticsearch中启用安全功能,为内置用户如elastic和kibana_system设置密码,以及如何配置Kibana以使用这些密码连接到Elasticsearch。这适用于开发模式的集群,但在生产环境中需要额外的TLS配置以确保节点间通信的安全。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Elasticsearch 设置最低安全性

您启用 Elasticsearch 安全功能,然后为内置用户创建密码。您可以稍后添加更多用户,但使用内置用户可以简化为集群启用安全性的过程。

最小安全场景对于 生产模式集群是不够的。如果您的集群有多个节点,您必须启用最低安全性,然后 在节点之间配置传输层安全性 (TLS)。

  • 文档使用的 elasticsearch 版本如下:
    Version: 7.15.1, Build: default/tar/83c34f456ae29d60e94d886e455e6a3409bba9ed/2021-10-07T21:56:19.031608185Z, JVM: 17

本文档是测试案例。配置为开发模式,如果一个 Elasticsearch 节点不能通过非环回地址与另一台机器形成集群,我们认为它处于开发模式,如果它可以通过非环回地址加入集群,则认为它处于生产模式。

请注意!!!!!!!
如果 Elasticsearch 处于开发模式,任何失败的引导程序检查都会在 Elasticsearch 日志中显示为警告。如果 Elasticsearch 处于生产模式,任何失败的引导程序检查都会导致 Elasticsearch 拒绝启动。

启用 Elasticsearch 安全功能

当您使用基本许可证时,Elasticsearch 安全功能默认处于禁用状态。启用 Elasticsearch 安全功能会启用基本身份验证,以便您可以使用用户名和密码身份验证运行本地集群。

  • 在集群中的每个节点 上,停止 Kibana 和 Elasticsearch(如果它们正在运行)。
root@ubuntu-x64_02:/opt# systemctl stop kibana

root@ubuntu-x64_02:/opt# systemctl stop elasticsearch9201

root@ubuntu-x64_02:/opt# systemctl stop elasticsearch9202

root@ubuntu-x64_02:/opt# systemctl stop elasticsearch9203
  • 在集群中的每个节点上,将设置 xpack.security.enabled 添加到 $ES_PATH_CONF/elasticsearch.yml 文件并将值设置为true:
# 9201
root@ubuntu-x64_02:/usr/local/elasticsearch9201/config# cp elasticsearch.yml elasticsearch.yml.bak 
root@ubuntu-x64_02:/usr/local/elasticsearch9201/config# sed -i 's/xpack.security.enabled: false/xpack.security.enabled: true/g' elasticsearch.yml


# 9202
root@ubuntu-x64_02:/usr/local/elasticsearch9202/config# cp elasticsearch.yml elasticsearch.yml.bak 
root@ubuntu-x64_02:/usr/local/elasticsearch9202/config# sed -i 's/xpack.security.enabled: false/xpack.security.enabled: true/g' elasticsearch.yml


# 9203
root@ubuntu-x64_02:/usr/local/elasticsearch9203/config# cp elasticsearch.yml elasticsearch.yml.bak 
root@ubuntu-x64_02:/usr/local/elasticsearch9203/config# sed -i 's/xpack.security.enabled: false/xpack.security.enabled: true/g' elasticsearch.yml

  • 如果您的集群只有一个节点,请discovery.type在 $ES_PATH_CONF/elasticsearch.yml文件中添加设置并将值设置为single-node. 此设置可确保您的节点不会无意中连接到可能在您的网络上运行的其他集群。
discovery.type: single-node

为内置用户创建密码编辑

  • 要与集群通信,您必须为内置用户配置用户名。除非您启用匿名访问,否则所有不包含用户名和密码的请求都会被拒绝。

  • 在启用最低或基本安全性时,您只需要为elastic和kibana_system用户设置密码。

  • 在集群中的每个节点上,启动 Elasticsearch。

  • 请注意,你要使用开发模式的集群,如果你使用生产环境模式,又不启用TLS,你的集群将不会启动,报错:
    bootstrap check failure [1] of [1]: Transport SSL must be enabled if security is enabled on a [basic] license. Please set [xpack.security.transport.ssl.enabled] to [true] or disable security by setting [xpack.security.enabled] to [false]

  • 如果你使用开发模式,则上面的报错会显示为“警告”


# 启动-->  9201
root@ubuntu-x64_02:/opt# systemctl start elasticsearch9201
root@ubuntu-x64_02:/opt# ps -ef | grep elasticsearch9201
elk       3513     1 99 14:11 ?        00:00:22 /usr/local/elasticsearch9201/jdk/bin/java -Xshare:auto -Des.networkaddress.cache.ttl=60 -Des.networkaddress.cache.negative.ttl=10 -XX:+AlwaysPreTouch -Xss1m -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djna.nosys=true -XX:-OmitStackTraceInFastThrow -XX:+ShowCodeDetailsInExceptionMessages -Dio.netty.noUnsafe=true -Dio.netty.noKeySetOptimization=true -Dio.netty.recycler.maxCapacityPerThread=0 -Dio.netty.allocator.numDirectArenas=0 -Dlog4j.shutdownHookEnabled=false -Dlog4j2.disable.jmx=true -Djava.locale.providers=SPI,COMPAT --add-opens=java.base/java.io=ALL-UNNAMED -Xms1g -Xmx1g -XX:+UseG1GC -Djava.io.tmpdir=/tmp/elasticsearch-4767306356958695874 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=data -XX:ErrorFile=logs/hs_err_pid%p.log -Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m -XX:MaxDirectMemorySize=536870912 -XX:G1HeapRegionSize=4m -XX:InitiatingHeapOccupancyPercent=30 -XX:G1ReservePercent=15 -Des.path.home=/usr/local/elasticsearch9201 -Des.path.conf=/usr/local/elasticsearch9201/config -Des.distribution.flavor=default -Des.distribution.type=tar -Des.bundled_jdk=true -cp /usr/local/elasticsearch9201/lib/* org.elasticsearch.bootstrap.Elasticsearch -d
elk       3534  3513  0 14:11 ?        00:00:00 /usr/local/elasticsearch9201/modules/x-pack-ml/platform/linux-x86_64/bin/controller


# 启动-->  9202
root@ubuntu-x64_02:/opt# systemctl start elasticsearch9202
root@ubuntu-x64_02:/opt# ps -ef | grep elasticsearch9202
elk       3743     1 99 14:13 ?        00:00:04 /usr/local/elasticsearch9202/jdk/bin/java -Xshare:auto -Des.networkaddress.cache.ttl=60 -Des.networkaddress.cache.negative.ttl=10 -XX:+AlwaysPreTouch -Xss1m -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djna.nosys=true -XX:-OmitStackTraceInFastThrow -XX:+ShowCodeDetailsInExceptionMessages -Dio.netty.noUnsafe=true -Dio.netty.noKeySetOptimization=true -Dio.netty.recycler.maxCapacityPerThread=0 -Dio.netty.allocator.numDirectArenas=0 -Dlog4j.shutdownHookEnabled=false -Dlog4j2.disable.jmx=true -Djava.locale.providers=SPI,COMPAT --add-opens=java.base/java.io=ALL-UNNAMED -Xms1g -Xmx1g -XX:+UseG1GC -Djava.io.tmpdir=/tmp/elasticsearch-12089215587958578062 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=data -XX:ErrorFile=logs/hs_err_pid%p.log -Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m -XX:MaxDirectMemorySize=536870912 -XX:G1HeapRegionSize=4m -XX:InitiatingHeapOccupancyPercent=30 -XX:G1ReservePercent=15 -Des.path.home=/usr/local/elasticsearch9202 -Des.path.conf=/usr/local/elasticsearch9202/config -Des.distribution.flavor=default -Des.distribution.type=tar -Des.bundled_jdk=true -cp /usr/local/elasticsearch9202/lib/* org.elasticsearch.bootstrap.Elasticsearch -d
elk       3764  3743  0 14:13 ?        00:00:00 /usr/local/elasticsearch9202/modules/x-pack-ml/platform/linux-x86_64/bin/controller


# 启动--> 9203
root@ubuntu-x64_02:/opt# systemctl start elasticsearch9203
root@ubuntu-x64_02:/opt# ps -ef | grep elasticsearch9203
elk       3974     1 99 14:13 ?        00:00:05 /usr/local/elasticsearch9203/jdk/bin/java -Xshare:auto -Des.networkaddress.cache.ttl=60 -Des.networkaddress.cache.negative.ttl=10 -XX:+AlwaysPreTouch -Xss1m -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djna.nosys=true -XX:-OmitStackTraceInFastThrow -XX:+ShowCodeDetailsInExceptionMessages -Dio.netty.noUnsafe=true -Dio.netty.noKeySetOptimization=true -Dio.netty.recycler.maxCapacityPerThread=0 -Dio.netty.allocator.numDirectArenas=0 -Dlog4j.shutdownHookEnabled=false -Dlog4j2.disable.jmx=true -Djava.locale.providers=SPI,COMPAT --add-opens=java.base/java.io=ALL-UNNAMED -Xms1g -Xmx1g -XX:+UseG1GC -Djava.io.tmpdir=/tmp/elasticsearch-1626046538224446804 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=data -XX:ErrorFile=logs/hs_err_pid%p.log -Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m -XX:MaxDirectMemorySize=536870912 -XX:G1HeapRegionSize=4m -XX:InitiatingHeapOccupancyPercent=30 -XX:G1ReservePercent=15 -Des.path.home=/usr/local/elasticsearch9203 -Des.path.conf=/usr/local/elasticsearch9203/config -Des.distribution.flavor=default -Des.distribution.type=tar -Des.bundled_jdk=true -cp /usr/local/elasticsearch9203/lib/* org.elasticsearch.bootstrap.Elasticsearch -d
elk       3995  3974  1 14:13 ?        00:00:00 /usr/local/elasticsearch9203/modules/x-pack-ml/platform/linux-x86_64/bin/controller
  • 检查集群启动状态: REST请求缺少身份验证凭据,接下来我们需要通过运行实用程序为内置用户设置密码
root@ubuntu-x64_02:/opt# curl "http://127.0.0.1:9201/_cat/health/?v"
{"error":{"root_cause":[{"type":"security_exception","reason":"missing authentication credentials for REST request [/_cat/health/?v]","header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}}],"type":"security_exception","reason":"missing authentication credentials for REST request [/_cat/health/?v]","header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}},"status":401}

为内置用户创建密码

在另一个终端窗口中,通过运行实用程序为内置用户设置密码 elasticsearch-setup-passwords, 您可以elasticsearch-setup-passwords针对集群中的任何节点运行该实用程序。但是,您应该只为整个集群运行一次该实用程序。

  • 使用该auto参数将随机生成的密码输出到控制台,您可以在以后根据需要更改这些密码:
  • 实际密码会明文打印,这里使用星号代替
cd /usr/local/elasticsearch9201/
root@ubuntu-x64_02:/usr/local/elasticsearch9201# ./bin/elasticsearch-setup-passwords auto
Initiating the setup of passwords for reserved users elastic,apm_system,kibana,kibana_system,logstash_system,beats_system,remote_monitoring_user.
The passwords will be randomly generated and printed to the console.
Please confirm that you would like to continue [y/N]y


Changed password for user apm_system
PASSWORD apm_system = G5e******qb

Changed password for user kibana_system
PASSWORD kibana_system = Z4i******Wy7

Changed password for user kibana
PASSWORD kibana = Z4i******y7

Changed password for user logstash_system
PASSWORD logstash_system = sdR******BY

Changed password for user beats_system
PASSWORD beats_system = rIF******BA

Changed password for user remote_monitoring_user
PASSWORD remote_monitoring_user = dt5******gD

Changed password for user elastic
PASSWORD elastic = G9e******nG
  • interactive如果要使用自己的密码,请使用参数而不是参数运行命令 auto。使用此模式可引导您完成所有内置用户的密码配置。
cd /usr/local/elasticsearch9201/
root@ubuntu-x64_02:/usr/local/elasticsearch9201# ./bin/elasticsearch-setup-passwords interactive
  • 保存生成的密码。您将需要它们将内置用户添加到 Kibana。
  • 为用户设置密码后,您将无法再次运行该命令。elasticsearch-setup-passwords

配置 Kibana 以使用密码连接到 Elasticsearch

  • 启用 Elasticsearch 安全功能后,用户必须使用有效的用户名和密码登录 Kibana。

  • 您将配置 Kibana 以使用您之前创建的内置 kibana_system 用户和密码。Kibana 执行一些需要 kibana_system 用户使用的后台任务。

  • 此帐户不适用于个人用户,无权从浏览器登录 Kibana。相反,您将以超级用户身份登录 Kibana elastic。

  • 将设置添加elasticsearch.username到KIB_PATH_CONF/kibana.yml 文件并将值设置为kibana_system用户:

root@ubuntu-x64_02:/usr/local/kibana/config# echo 'elasticsearch.username: "kibana_system"' >> kibana.yml
root@ubuntu-x64_02:/usr/local/kibana/config# egrep "^[a-Z]" kibana.yml 
server.port: 5601
server.host: 192.168.88.12
server.publicBaseUrl: "http://192.168.88.12:5601"
elasticsearch.hosts: ["http://127.0.0.1:9201" ,"http://127.0.0.1:9202" ,"http://127.0.0.1:9203"] 
kibana.index: ".kibana"
kibana.defaultAppId: "home"
elasticsearch.pingTimeout: 1500
elasticsearch.requestTimeout: 30000
elasticsearch.shardTimeout: 30000
elasticsearch.username: "kibana_system"

创建 Kibana 密钥库:

root@ubuntu-x64_02:/usr/local/kibana# ./bin/kibana-keystore create
Created Kibana keystore in /usr/local/kibana/config/kibana.keystore

将用户 kibana_system 的密码添加到 Kibana 密钥库:
出现提示时,输入用户kibana_system的密码

root@ubuntu-x64_02:/usr/local/kibana# ./bin/kibana-keystore add elasticsearch.password
Setting elasticsearch.password already exists. Overwrite? [y/N] y
Enter value for elasticsearch.password: ********************

重启 kibana 服务

root@ubuntu-x64_02:/usr/local/kibana# systemctl restart kibana.service 

root@ubuntu-x64_02:/usr/local/kibana# ps -ef | grep kibana
elk       2370     1 11 19:22 ?        00:00:00 /usr/local/kibana/bin/../node/bin/node /usr/local/kibana/bin/../src/cli/dist
elk       2385  2370 99 19:22 ?        00:00:06 /usr/local/kibana/node/bin/node --preserve-symlinks-main --preserve-symlinks /usr/local/kibana/src/cli/dist

以用户 elastic 身份登录 Kibana 。使用此超级用户帐户来管理空间、创建新用户和分配角色。

如果您在本地运行 Kibana,请转至http://localhost:5601 查看登录页面。

登录后,可以看到 Management 下面security可以对用户和角色权设置限管理~
在这里插入图片描述

小结

以上是最低安全性配置, 如果您想在笔记本电脑上设置 Elasticsearch 并开始开发,这个场景适合您。此配置通过为内置用户设置密码来防止未经授权访问您的本地集群。您还可以为 Kibana 配置密码身份验证。

最小安全场景对于 生产模式集群是不够的。如果您的集群有多个节点,您必须启用最低安全性,然后 在节点之间配置传输层安全性 (TLS)。

请参考 “Elasticsearch 设置基本安全性”,以保护集群中节点之间的所有内部通信

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ethanchen's notes

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值