部署wazuh实现对多台 Ubuntu 主机进行集中监控

对多台 Ubuntu 主机（例如 Ubuntu 18.04）进行集中监控，同时实现 **主机入侵检测（HIDS）** 和 **行为监控**，以下是一个轻量级、实用的集中监控方案，适用于中小型企业：
### ✅ 方案目标
* 实现主机入侵检测（Host-based IDS）
* 实现进程、登录、系统变更等行为监控
* 支持集中化日志采集与统一展示
* 成本低、依赖少，易于部署和维护
## 🧩 架构组件
| 角色                  | 工具                                                | 说明                           |
| ------------------- | ------------------------------------------------- | ---------------------------- |
| Agent（每台 Ubuntu 主机） | [Wazuh Agent](https://documentation.wazuh.com)    | 实时监控主机安全事件（文件完整性、登录、root提权等） |
| 集中服务器               | Wazuh Manager + Filebeat + Elasticsearch + Kibana | 管理所有 Agent 数据，并提供可视化界面       |
| 日志转发                | Filebeat                                          | 将日志高效传输至 Elasticsearch       |
| 可选轻量级替代             | [OSSEC](https://www.ossec.net/) + Graylog         | 更轻的方案，但功能相对简单                |
---
## 🛠️ 部署简要步骤
### 1. 安装 Wazuh Server（集中监控端）
部署在一台专用 Ubuntu 主机（建议 ≥4C/8G）
cp /etc/apt/sources.list /etc/apt/sources.list.bak
cat > /etc/apt/sources.list << EOF
deb https://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
EOF
apt update && apt upgrade -y
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&\
apt-get install ntpdate -y &&\
if ! crontab -l | grep "ntpdate" &>/dev/null; then
    (echo "*/5 * * * * /usr/sbin/ntpdate ntp.aliyun.com >/dev/null 2>&1"; crontab -l) | crontab
fi
linux系统通过代理上网
vi ~/.bashrc
在文件末尾添加以下内容
cat >> ~/.bashrc << EOF
export http_proxy="http://192.168.8.5:1080"
export https_proxy="http://192.168.8.5:1080"
export ftp_proxy="http://192.168.8.5:1080"
export no_proxy="localhost,127.0.0.1,::1"
EOF
保存关闭
source ~/.bashrc
sudo vi /etc/apt/apt.conf.d/95proxies
添加以下内容：
Acquire::http::Proxy "http://192.168.8.5:1080";
Acquire::https::Proxy "http://192.168.8.5:1080";
Acquire::ftp::Proxy "http://192.168.8.5:1080";
在系统级别配置代理，以便所有应用程序都使用代理服务器
sudo vi /etc/environment
http_proxy="http://192.168.8.5:1080"
https_proxy="http://192.168.8.5:1080"
ftp_proxy="http://192.168.8.5:1080"
no_proxy="localhost,127.0.0.1,::1"
保存关闭
sudo reboot
http_proxy="http://10.2.2.24:1080"
https_proxy="http://10.2.2.24:1080"
ftp_proxy="http://10.2.2.24:1080"
no_proxy="localhost,127.0.0.1,::1"
如果你还需要Docker通过代理上网，可以通过以下步骤配置Docker代理。
创建Docker的systemd目录（如果不存在）：
sudo mkdir -p /etc/systemd/system/docker.service.d
sudo vi /etc/systemd/system/docker.service.d/http-proxy.conf
添加以下内容：
[Service]
Environment="HTTP_PROXY=http://192.168.8.5:1080"
Environment="HTTPS_PROXY=http://192.168.8.5:1080"
Environment="NO_PROXY=localhost,127.0.0.1,::1"
保存关闭
sudo systemctl daemon-reload
sudo systemctl restart docker
curl -sO https://packages.wazuh.com/4.12/wazuh-install.sh && sudo bash ./wazuh-install.sh -a
在 Wazuh 4.x（包括你安装的 4.12.0）中，`admin` 是一个系统保留账户，不允许通过 Web 界面或 API 直接重设密码。
## ✅ 正确的解决方法：
### 🛠 方法一：在命令行重置 `admin` 密码
你可以在 **Wazuh Dashboard 所在的主机上**运行以下命令来重置密码：
```bash
/usr/share/wazuh-indexer/plugins/opensearch-security/tools/wazuh-passwords-tool.s reset-admin
```
这将：
* 生成一个新密码
* 输出密码（请记得保存）
### 🛠 方法二：手动设置密码（推荐）
```bash
/usr/share/wazuh-indexer/plugins/opensearch-security/tools/wazuh-passwords-tool.sh --user admin --password 新密码
```
例如：
/usr/share/wazuh-indexer/plugins/opensearch-security/tools/wazuh-passwords-tool.sh --user admin --password Heli@147369.
执行后会看到提示密码修改成功。
## ✅ 修改后重启 Wazuh Dashboard（可选）
如果你登录仍有问题，可重启 Dashboard 服务：
systemctl restart wazuh-dashboard
安装agent
官方方法：在ubuntu20.04 18.04 14.04上测试通过
wget https://packages.wazuh.com/4.x/apt/pool/main/w/wazuh-agent/wazuh-agent_4.12.0-1_amd64.deb && sudo WAZUH_MANAGER='10.2.2.16' WAZUH_AGENT_NAME='ubuntu20.04-2.18' dpkg -i ./wazuh-agent_4.12.0-1_amd64.deb
10.2.2.16 替换为 wazuh server端ip地址
ubuntu20.04-2.18 替换为该agent的名称
sudo systemctl daemon-reload &&\
sudo systemctl enable wazuh-agent &&\
sudo systemctl start wazuh-agent
其他方法，在ubuntu14.04上测试通过
curl -s https://packages.wazuh.com/key/GPG-KEY-WAZUH | gpg --no-default-keyring --keyring gnupg-ring:/usr/share/keyrings/wazuh.gpg --import && chmod 644 /usr/share/keyrings/wazuh.gpg
echo "deb [signed-by=/usr/share/keyrings/wazuh.gpg] https://packages.wazuh.com/4.x/apt/ stable main" | tee -a /etc/apt/sources.list.d/wazuh.list
apt-get update
apt-get install gnupg apt-transport-https
curl -s https://packages.wazuh.com/key/GPG-KEY-WAZUH | apt-key add -
echo "deb https://packages.wazuh.com/4.x/apt/ stable main" | tee -a /etc/apt/sources.list.d/wazuh.list
WAZUH_MANAGER="192.168.30.49" apt-get install wazuh-agent
ubuntu安装客户端
service wazuh-agent start
service wazuh-agent status
wazuh-modulesd is running...
wazuh-logcollector is running...
wazuh-syscheckd is running...
wazuh-agentd is running...
wazuh-execd is running...
卸载方法：
1. 停止 agent 服务（可选但推荐）：
sudo systemctl stop wazuh-agent
2. 卸载 wazuh-agent：
sudo apt-get remove --purge wazuh-agent -y
3. 删除相关配置文件（可选，如果你想彻底清除）：
sudo rm -rf /var/ossec
sudo rm -rf /etc/ossec-init.conf
sudo rm -rf /etc/systemd/system/wazuh-agent.service
4. 更新 systemd（可选）：
sudo systemctl daemon-reload
## ✅ 修改 agent 名称的正确方式
Wazuh agent 的名称保存在配置文件中，你可以按以下步骤修改：
### 1. 停止 agent 服务
```bash
sudo systemctl stop wazuh-agent
```
### 2. 修改配置文件 `/var/ossec/etc/ossec.conf`
找到如下配置段落（大约在前几十行）：
```xml
<client>
  <name>ubuntu14.04-2.14</name>
  <server-ip>10.2.2.16</server-ip>
</client>
```
将 `<name>` 修改为你想要的新名称，例如：
```xml
<client>
  <name>ubuntu14.04-new</name>
  <server-ip>10.2.2.16</server-ip>
</client>
```
保存退出。
---
### 3. 删除旧的 `client.keys` 条目（可选，若你在 manager 上要重配）
登录 Wazuh Manager，然后运行：
```bash
/var/ossec/bin/manage_agents
```
选择：
* `R` 删除旧名称对应的 agent
* `A` 添加新名称
* `E` 保存并分发 key，得到以下这串字符
MDA0IHVidW50dTE0LjA0LWNjaW90LTIuMTQgMTAuMi4yLjE0IDEwMjJiY2U2N2ViNGY2MmYzMTAzZGZjNTFlZWMzYzBhNjE1ZjI1ZGNmNzU4ZGRlNjRiMmZjMmY3MWMxMGI1ZWM=
到 agent 端运行：
/var/ossec/bin/manage_agents
选 I ，粘贴key  上面那串字符
### 4. 重启 agent 服务
```bash
sudo systemctl start wazuh-agent
```
---
### ✅ 验证新名称是否生效
查看 agent 日志：
```bash
sudo tail -f /var/ossec/logs/ossec.log
```
你应该能看到类似如下内容：
```
ossec-agent: INFO: Using agent name as configured: 'ubuntu14.04-new'
```
在 Wazuh Manager UI 中刷新 agent 列表，也应能看到新名称。