Ubuntu 24.04 上安装 Elasticsearch 9.2.1

Ubuntu 24.04 上安装 Elasticsearch 9.2.1

核心步骤:

  1. 更新系统包列表
  2. 安装 Java (JDK)
  3. 下载并安装 Elasticsearch
    • 使用 APT 包管理器(推荐)
    • 或者 手动下载 .deb 包安装
  4. 配置 Elasticsearch
  5. 启动并启用 Elasticsearch 服务
  6. 验证安装
  7. 开启GPU加速

1. 更新系统包列表

打开终端并运行以下命令以确保你拥有最新的软件包信息:

sudo apt update && apt -y dist-upgrade

2. 安装 Java (JDK)

2.1 首先查看系统中已安装的 OpenJDK 相关包,避免误删或漏删:
dpkg --list | grep openjdk

输出示例(openjdk-17-jdk)

openjdk-17-jdk:amd64					17.0.17+10-1~24.04	amd64	OpenJDK Development Kit (JDK)
openjdk-17-jdk-headless:amd64	17.0.17+10-1~24.04	amd64	OpenJDK Development Kit (JDK) (headless)
openjdk-17-jre:amd64					17.0.17+10-1~24.04	amd64	OpenJDK Java runtime, using Hotspot JIT
openjdk-17-jre-headless:amd64	17.0.17+10-1~24.04	amd64	OpenJDK Java runtime, using Hotspot JIT (headless)
2.2 彻底卸载 openjdk
sudo apt purge openjdk-17-* -y
2.3 清理残留依赖(可选但推荐)
sudo apt autoremove -y && sudo apt autoclean
2.4 验证是否卸载成功
java -version
javac -version
2.5 安装 OpenJDK 25
sudo apt install openjdk-25-jdk
2.6 验证安装:
java -version

你应该看到类似如下的输出,确认是 OpenJDK 25 或更高版本:

openjdk version "25.0.1" 2025-10-21
OpenJDK Runtime Environment (build 25.0.1+8-Ubuntu-124.04)
OpenJDK 64-Bit Server VM (build 25.0.1+8-Ubuntu-124.04, mixed mode, sharing)

3. 下载并安装 Elasticsearch

方法一:使用 APT 包管理器 (推荐)

这是最简单且易于维护的方法。

3.1 安装依赖工具(若未安装)
sudo apt install apt-transport-https ca-certificates curl -y
3.2 导入 Elasticsearch GPG 密钥:

首先,添加 Elastic 公司的官方 GPG 密钥用于验证下载的包:

wget -O - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elastic-archive-keyring.gpg
3.3 添加 Elasticsearch APT 仓库:

创建一个新的 APT 源列表文件:

echo "deb [signed-by=/usr/share/keyrings/elastic-archive-keyring.gpg] https://artifacts.elastic.co/packages/9.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-9.x.list
3.4 再次更新包列表:

现在包含了 Elasticsearch 仓库,需要再次更新:

sudo apt update
3.5 安装指定版本 Elasticsearch 9.2.1:

使用 apt-cache policy 查看可用版本:

apt-cache policy elasticsearch

然后安装 9.2.1 版本:

sudo apt install elasticsearch=9.2.1

如果你想安装该主版本下的最新小版本,可以只运行 sudo apt install elasticsearch,但这可能不是精确的 9.2.1。

方法二:手动下载 .deb 包安装

如果你更喜欢直接下载 .deb 文件进行安装:

a. 下载 .deb 包:

访问 Elasticsearch 官方下载页面或直接使用 wget 下载 9.2.1 的 .deb 包:

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-9.2.1-amd64.deb

(请检查官网确认文件名和架构是否正确)

b. 安装 .deb 包:

使用 dpkg 命令安装:

sudo dpkg -i elasticsearch-9.2.1-amd64.deb

如果遇到依赖问题,可以随后运行 sudo apt-get install -f 来修复。

4. 配置 Elasticsearch

Elasticsearch 9.x 默认启用安全功能(HTTPS、密码认证),需修改配置文件以支持外部访问和基础设置。

4.1 编辑主配置文件 elasticsearch.yml
sudo vim /etc/elasticsearch/elasticsearch.yml

修改以下关键配置(按实际需求调整):

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
#       Before you set out to tweak and tune the configuration, make sure you
#       understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: node-1
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
path.data: /var/lib/elasticsearch
#
# Path to log files:
#
path.logs: /var/log/elasticsearch
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# By default Elasticsearch is only accessible on localhost. Set a different
# address here to expose this node on the network:
#
#network.host: 192.168.0.1
network.host: 0.0.0.0
#
# By default Elasticsearch listens for HTTP traffic on the first free port it
# finds starting at 9200. Set a specific HTTP port here:
#
http.port: 9200
http.cors.enabled: true
http.cors.allow-origin: "*"
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.seed_hosts: ["host1", "host2"]
discovery.seed_hosts: []
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
#cluster.initial_master_nodes: ["node-1", "node-2"]
#
# For more information, consult the discovery and cluster formation module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Allow wildcard deletion of indices:
#
#action.destructive_requires_name: false
#----------------------- BEGIN SECURITY AUTO CONFIGURATION -----------------------
#
# The following settings, TLS certificates, and keys have been automatically      
# generated to configure Elasticsearch security features on 27-11-2025 12:43:47
#
# --------------------------------------------------------------------------------

# Enable security features
xpack.security.enabled: true

xpack.security.enrollment.enabled: true

# Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents
xpack.security.http.ssl:
  enabled: true
  keystore.path: certs/http.p12

# Enable encryption and mutual authentication between cluster nodes
xpack.security.transport.ssl:
  enabled: true
  verification_mode: certificate
  keystore.path: certs/transport.p12
  truststore.path: certs/transport.p12
# Create a new cluster with the current node only
# Additional nodes can still join the cluster later
# cluster.initial_master_nodes: ["lhz-mechrevo"]
cluster.initial_master_nodes: ["node-1"]

# Allow HTTP API connections from anywhere
# Connections are encrypted and require user authentication
http.host: 0.0.0.0

# Allow other nodes to join the cluster from anywhere
# Connections are encrypted and mutually authenticated
#transport.host: 0.0.0.0

#----------------------- END SECURITY AUTO CONFIGURATION -------------------------

保存并退出。

以上文件关键修改处:

# 1. 集群名称(自定义,例如 "my-application")
cluster.name: my-application

# 2. 节点名称(自定义,例如 "node-1")
node.name: node-1

# 3. 网络访问(默认只监听 localhost,改为 0.0.0.0 允许外部访问)
network.host: 0.0.0.0

# 4. 内存锁定: 为了防止交换(swapping),建议启用内存锁定,同时,你需要配置 systemd 服务来允许内存锁定。
bootstrap.memory_lock: true

# 5. 端口配置(默认 9200 为 HTTP 端口,9300 为集群通信端口)
http.port: 9200

# 6. 跨域配置(可选,若需前端/第三方工具访问)
http.cors.enabled: true
http.cors.allow-origin: "*"

# 7. 集群初始化(单节点部署,避免集群发现报错)
discovery.seed_hosts: []

# 8. 集群主节点 注意:此次的名字 应用节点名称 node.name 的值一致
cluster.initial_master_nodes: ["node-1"]
4.2 调整 JVM 内存(可选,根据服务器配置)

Elasticsearch 默认 JVM 堆内存为 1GB,若服务器内存 ≥4GB,可修改为 2GB(不建议超过物理内存的 50%):

sudo vim /etc/elasticsearch/jvm.options.d/jvm.options

找到 -Xms-Xmx 行,将其值改小。例如,如果你的服务器只有 2GB 内存,可以尝试设置为 512MB 或 1GB:

-Xms512m
-Xmx512m
-Xms1g
-Xmx1g

注意:同时,你需要配置 systemd 服务来设置内存上限。

4.3 修改系统内存锁定限制(limits.conf

编辑 limits.conf 文件

sudo vim /etc/security/limits.conf

在文件末尾添加以下内容(允许 elasticsearch 用户锁定任意内存)

# Elasticsearch 内存锁定配置
elasticsearch soft memlock unlimited
elasticsearch hard memlock unlimited
  • soft memlock:软限制(系统警告阈值)
  • hard memlock:硬限制(系统强制限制,必须设为 unlimited
4.4 修改 Elasticsearch systemd 服务配置(关键补充)

编辑 Elasticsearch 服务文件

sudo cat /lib/systemd/system/elasticsearch.service

找到 [Service] 开头的部分,新增一行 LimitMEMLOCK=infinity配合配置文件中bootstrap.memory_lock: true

使用 MemoryLimit 参数设置进程总内存上限(建议为物理内存的 75%,需大于 JVM 堆内存):

LimitMEMLOCK=infinity 
MemoryLimit=1G

完整示例如下:

[Unit]
Description=Elasticsearch
Documentation=https://www.elastic.co
Wants=network-online.target
After=network-online.target

[Service]
Type=notify
# the elasticsearch process currently sends the notifications back to systemd
# and for some reason exec does not work (even though it is a child). We should change
# this notify access back to main (the default), see https://github.com/elastic/elasticsearch/issues/86475
NotifyAccess=all
RuntimeDirectory=elasticsearch
PrivateTmp=true
Environment=ES_HOME=/usr/share/elasticsearch
Environment=ES_PATH_CONF=/etc/elasticsearch
Environment=PID_DIR=/var/run/elasticsearch
Environment=ES_SD_NOTIFY=true
EnvironmentFile=-/etc/default/elasticsearch

WorkingDirectory=/usr/share/elasticsearch

User=elasticsearch
Group=elasticsearch

ExecStart=/usr/share/elasticsearch/bin/systemd-entrypoint -p ${PID_DIR}/elasticsearch.pid --quiet
LimitMEMLOCK=infinity 
MemoryLimit=1G

# StandardOutput is configured to redirect to journalctl since
# some error messages may be logged in standard output before
# elasticsearch logging system is initialized. Elasticsearch
# stores its logs in /var/log/elasticsearch and does not use
# journalctl by default. If you also want to enable journalctl
# logging, you can simply remove the "quiet" option from ExecStart.
StandardOutput=journal
StandardError=inherit

# Specifies the maximum file descriptor number that can be opened by this process
LimitNOFILE=65535

# Specifies the maximum number of processes
LimitNPROC=4096

# Specifies the maximum size of virtual memory
LimitAS=infinity

# Specifies the maximum file size
LimitFSIZE=infinity

# Disable timeout logic and wait until process is stopped
TimeoutStopSec=0

# SIGTERM signal is used to stop the Java process
KillSignal=SIGTERM

# Send the signal only to the JVM rather than its control group
KillMode=process

# Java process is never killed
SendSIGKILL=no

# When a JVM receives a SIGTERM signal it exits with code 143
SuccessExitStatus=143

# Allow a slow startup before the systemd notifier module kicks in to extend the timeout
TimeoutStartSec=900

[Install]
WantedBy=multi-user.target

# Built for packages-9.2.1 (packages)

5. 启动并启用 Elasticsearch 服务

安装完成后,启动 Elasticsearch 服务并设置为开机自启:

sudo systemctl daemon-reload  # 如果之前修改了 systemd 配置
sudo systemctl start elasticsearch
sudo systemctl enable elasticsearch

检查服务状态:

sudo systemctl status elasticsearch

服务应该显示为 active (running)。如果启动失败,请检查日志文件 /var/log/elasticsearch/*.log 获取错误信息。

注意:在第一次启动服务的时候,elasticsearch 会初始化一个密码,日志示例如下:

The generated password for the elastic built-in superuser is : ZC7ZqD1EfUZeX=AIVd8m

其中:ZC7ZqD1EfUZeX=AIVd8m 就是生成的密码一定要记录保存好

6. 验证安装

等待几秒钟让 Elasticsearch 完全启动。然后,你可以通过发送 HTTP 请求来测试它是否正常工作:

curl -X GET "localhost:9200"

或者访问健康检查端点:

curl -X GET "localhost:9200/_cluster/health?pretty"

你应该会收到一个包含 Elasticsearch 版本信息和集群状态的 JSON 响应。

如果启用了安全功能,你需要提供用户名和密码:

curl -u elastic -X GET "localhost:9200" # 系统会提示输入密码
# 或者将密码放在命令中 (不推荐,因为会留在 shell 历史记录中)
# curl -u elastic:<your_password> -X GET "localhost:9200"

7、安全配置(Elasticsearch 9.x 必做)

7.1 初始化密码(自动生成或手动设置)

执行 Elastic 内置工具初始化密码:

sudo /usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic -i
  • -u elastic:指定初始化 elastic 超级用户(默认管理员)
  • -i:交互式输入密码(建议设置强密码或者不使用该参数让系统自动生成强密码)

按提示输入密码并确认(密码不会显示,输入后回车即可)。

7.2 测试访问 Elasticsearch

Elasticsearch 9.x 默认使用 HTTPS(自签名证书),访问时需指定协议和认证:

# 本地测试(使用 curl 访问,忽略自签名证书警告)
curl -k -u elastic:你的密码 https://localhost:9200

成功输出示例(包含版本、节点信息):

{
  "name" : "node-1",
  "cluster_name" : "my-application",
  "cluster_uuid" : "6UKMjniySweZrdUKQrmYtA",
  "version" : {
    "number" : "9.2.1",
    "build_flavor" : "default",
    "build_type" : "deb",
    "build_hash" : "4ad0ef0e98a2e72fafbd79a19fa5cae2f026117d",
    "build_date" : "2025-11-06T22:07:39.673130621Z",
    "build_snapshot" : false,
    "lucene_version" : "10.3.1",
    "minimum_wire_compatibility_version" : "8.19.0",
    "minimum_index_compatibility_version" : "8.0.0"
  },
  "tagline" : "You Know, for Search"
}
3. 外部访问(可选)

若需从其他机器访问,需开放 Ubuntu 防火墙端口:

# 开放 9200 端口(Elasticsearch HTTP 端口)
sudo ufw allow 9200/tcp

# 验证防火墙规则
sudo ufw status

之后可通过 https://服务器IP:9200 访问(需携带 elastic 用户密码)。

8、常见问题排查

8.1 服务启动失败(日志显示内存不足)
  • 原因:JVM 堆内存设置超过物理内存,或服务器内存 ≤1GB。
  • 解决:修改 JVM 内存为 512MB(适合 2GB 内存服务器):
    sudo nano /etc/elasticsearch/jvm.options.d/jvm.options
    
    改为:-Xms512m-Xmx512m,重启服务。
8.2 外部访问超时
  • 检查防火墙是否开放 9200 端口(sudo ufw status)。
  • 确认 network.host: 0.0.0.0 已配置(未配置则仅本地可访问)。
  • 检查云服务器安全组(若为云主机,需放行 9200 端口)。
8.3 密码忘记

重新初始化 elastic 用户密码:

sudo /usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic -i

完成以上步骤后,你就成功在 Ubuntu 24.04 上安装了 Elasticsearch 9.2.1。记得后续关注安全配置和性能调优。

### 如何在 Ubuntu 24.04安装 FinalShell #### 准备工作 确保宿主机已成功安装 VMware 并完成 Ubuntu 24.04部署。如果尚未安装,请参考官方文档或相关教程进行操作。 #### 步骤说明 ##### 1. 下载 FinalShell 访问 FinalShell 的官方网站 `www.hostbuf.com`,并下载适用于 Linux 的版本。选择适合系统的架构(通常为 x64),并将 `.deb` 文件保存至本地目录[^1]。 ```bash wget https://www.hostbuf.com/downloads/finalshell_linux_x64.deb -O ~/Downloads/finalshell_linux_x64.deb ``` ##### 2. 安装依赖项 为了顺利安装 `.deb` 包,需先安装 `gdebi` 工具,用于解决可能存在的依赖问题。 ```bash sudo apt update sudo apt install gdebi-core ``` ##### 3. 使用 GDebi 安装 FinalShell 将下载的 `.deb` 文件移动到目标文件夹(如 `/home/user/tool/ubuntu/`)。随后使用以下命令执行安装: ```bash cd /path/to/deb/file/ sudo gdebi finalshell_linux_x64.deb ``` 此过程会自动解析并安装所需的依赖库[^3]。 ##### 4. 验证安装 安装完成后,可通过以下命令验证 FinalShell 是否正常启动: ```bash /usr/lib/finalshell/bin/FinalShell ``` 或者将其添加到桌面快捷方式以便快速调用[^2]。 --- ### 注意事项 - 如果遇到权限不足的情况,请确认当前用户具有管理员权限。 - 若网络环境受限,可尝试手动调整代理设置后再重新下载所需资源。 --- ### 配置 SSH 远程连接 为了让 FinalShell 成功连接到远程服务器,还需确保 Ubuntu 系统上的 SSH 服务已启用。具体方法如下: 1. **安装 OpenSSH Server** ```bash sudo apt install openssh-server ``` 2. **编辑 SSH 配置文件** 修改 `/etc/ssh/sshd_config` 中的相关参数以满足需求[^4]。 ```bash sudo nano /etc/ssh/sshd_config ``` 主要关注以下几个选项: - `PermitRootLogin yes/no` - `PasswordAuthentication yes/no` 3. **重启 SSH 服务** 应用更改后记得重启服务使新配置生效: ```bash sudo systemctl restart sshd ``` ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

李昊哲小课

桃李不言下自成蹊

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

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

打赏作者

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

抵扣说明:

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

余额充值