在 RHEL 中使用 goose 实现远程主机管理

《OpenShift / RHEL / DevSecOps 汇总目录》

文章目录

本文将实现以下场景:本地的 RHEL 用户使用 goose 和 MCP Server 管理维护远程的 RHEL 系统。

在进行本文前,请先完成《 在 RHEL 中使用 goose 实现本地主机运维》。

注意:

  • goose 功能在 RHEL 9.8 或 10.2 以上版本才有。
  • 本文已在 RHEL 10.2 上验证。

远程主机配置

远程主机既是被管理的 RHEL 主机。

  1. 本远程主机上创建一个允许 MCP Server 通过 SSH 远程访问的用户。
bash 复制代码
$ sudo useradd mcp
$ sudo passwd mcp

本地主机配置

本地主机既是运行 goose 的 RHEL 主机。

配置免密访问远程主机

  1. 生成一个用来通过 SSH 访问远程主机的密钥对。
bash 复制代码
$ ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_mcp -C "rhel-mcp-agent" -N ""
  1. 用远程主机地址替换 <remote-rhel-ip>,将公钥复制到远程主机的 mcp 用户下。然后验证可以通过秘钥访问到登录远程主机。
bash 复制代码
$ ssh-copy-id -i ~/.ssh/id_ed25519_mcp.pub mcp@<remote-rhel-ip>
$ ssh -i ~/.ssh/id_ed25519_mcp mcp@192.168.56.136 whoami
mcp
  1. 在本地主机的操作用户下创建配置文件,并在其中添加访问远程主机的配置。其中 rhel-remote-01 是 MCP Server 用来识别远程主机的标示名称,这个名只是一个标识,并非实际的远程主机名。
bash 复制代码
$ cat ~/.ssh/config
Host rhel-remote-01
    HostName <remote-rhel-ip>
    User mcp
    Port 22
    IdentityFile ~/.ssh/id_ed25519_mcp
    StrictHostKeyChecking no
  1. 执行命令验证,确认可以访问到远程主机。
bash 复制代码
$ ssh rhel-remote-01 "hostname"
rhel-01

为 goose 配置 MCP Server

  1. 运行命令,安装 MCP Server。
bash 复制代码
$ sudo dnf install python3 python3-pip
$ pip install --user uv
$ uv tool install linux-mcp-server
  1. 运行 goose-configure 命令,然后根据以下完成 MCP Server 的配置。
bash 复制代码
$ goose configure

This will update your existing config files
  if you prefer, you can edit them directly at /home/dawnsky/.config/goose

┌   goose-configure
│
◇  What would you like to configure?
│  Add Extension
│
◇  What type of extension would you like to add?
│  Command-line Extension
│
◇  What would you like to call this extension?
│  linux-mcp-server
│
◇  What command should be run?
│  /home/dawnsky/.local/bin/linux-mcp-server
│
◇  Please set the timeout for this tool (in secs):
│  300
│
◇  Enter a description for this extension:
│  Linux MCP server
│
◇  Would you like to add environment variables?
│  No
│
└  Added linux-mcp-server extension
└  Configuration saved successfully to /home/dawnsky/.config/goose/config.yaml

用 goose 远程管理主机

  1. 在本地运行 goose。
bash 复制代码
$ goose
  1. 在 goose 提示符下发送指令。
bash 复制代码
> Check disk usage on rhel-remote-01
  1. 确认是对 rhel-remote-01 主机进行操作,并在执行步骤中选择 Allow 以便允许执行相关操作。
bash 复制代码
  ────────────────────────────────────────
  ▸ get_disk_usage linux-mcp-server
    host: rhel-remote-01

◇  Goose would like to call the above tool, do you allow?
│  Allow
│
Here is the disk usage for `rhel-remote-01`:

```
Filesystem                 Size  Used Avail Use% Mounted on
/dev/mapper/rhel_192-root   35.9G  2.2G  33.7G   6% /
devtmpfs                   959.1M     0 959.1M   0% /dev
tmpfs                      980.6M     0 980.6M   0% /dev/shm
tmpfs                      392.2M  5.5M  386.8M   1% /run
tmpfs                        1M     0    1M   0% /run/credentials/systemd-journald.service
/dev/sda2                    1.9G  361.7M   1.6G  18% /boot
tmpfs                        1M     0    1M   0% /run/credentials/getty@tty1.service
tmpfs                      196.1M    4K 196.1M   0% /run/user/1000
tmpfs                      196.1M    4K 196.1M   0% /run/user/1001
```
  ⏱ 18.04s
Always review AI-generated content prior to use.
  ━━╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ 8% 11k/128k
  1. 在 goose 提示符下发送其他指令。
bash 复制代码
> 检查远程机器 rhel-remote-01 上当前处于 LISTEN 状态的端口列表,以及是否有异常外部连接
  1. 在执行过程中确认有以下提示,这可能是模型重复生成括号或参数引起的。
bash 复制代码
-32602: The model produced malformed tool arguments. Try resending your message or breaking the task into smaller steps.
Received 52 characters; cut off at: {"host": "rhel-remote-01"}{"host": "rhel-remote-01"}    -32602: The model produced malformed tool arguments. Try resending your message or breaking the task into smaller steps.
Received 52 characters; cut off at: {"host": "rhel-remote-01"}{"host": "rhel-remote-01"}
  1. 再次发送指令,并在指令中增加 "注意:每次仅调用一个工具,参数必须是严格合法的单层 JSON,切勿重复输出参数块" 的附加说明,以便强调返回结果的要求。
bash 复制代码
> 检查远程机器 rhel-remote-01 上当前处于 LISTEN 状态的端口列表,以及是否有异常外部连接。注意:每次仅调用一个工具,参数必须是严格合法的单层 JSON,切勿重复输出参数块。
  1. 确认是对 rhel-remote-01 主机进行操作,并在执行步骤中选择 Allow 以便允许执行相关操作。
bash 复制代码
  ────────────────────────────────────────
  ▸ get_listening_ports linux-mcp-server
    host: rhel-remote-01

◇  Goose would like to call the above tool, do you allow?
│  Allow
│

  ────────────────────────────────────────
  ▸ get_network_connections linux-mcp-server
    host: rhel-remote-01

◇  Goose would like to call the above tool, do you allow?
│  Allow
│
在远程机器 `rhel-remote-01` 上,当前监听的端口和活跃的网络连接分析如下:

**监听端口 (LISTEN 状态):**

*   **UDP 323 (IPv4 和 IPv6 回环地址)**: 通常用于 `chronyd` (NTP 服务),这是正常现象。
*   **TCP 22 (所有接口)**: SSH 服务,用于远程管理。这是预期的。
*   **TCP 7080 (仅在本地回环地址 127.0.0.1)**: 表明有一个本地服务正在监听此端口,但不对外部网络开放。

**活跃网络连接:**

*   **SSH (TCP 22):**
    *   存在多个从 `192.168.56.1` 连接到本机 `192.168.56.135:22` 的已建立连接 (`ESTAB`)。
    *   还存在本机 `192.168.56.135` 上的一个端口 (`59500`) 到本机 SSH 端口 (`22`) 的连接 (`ESTAB`),这可能是一个本地用户或脚本通过 SSH 连接到本机。
    *   如果 `192.168.56.1` 是您的管理主机或授权访问的客户端,则这些 SSH 连接是正常的。
*   **TCP 7080:**
    *   有多个状态为 `CLOSE-WAIT`, `TIME-WAIT`, `FIN-WAIT-2` 的本地回环连接。这些状态表示之前的连接正在关闭过程中。由于它们都发生在 `127.0.0.1` 之间,所以是系统内部连接,通常不构成外部安全风险。
*   **UDP:**
    *   显示了与 DHCP 服务器 (端口 67) 和 NTP (端口 323) 相关的 `UNCONN` (未连接) 状态的 UDP 流量,这些是系统正常运行所必需的。

**异常外部连接分析:**

目前没有发现明显的异常外部连接。所有的 `ESTAB` 连接都是 SSH 连接,并且主要源自 `192.168.56.1`。 **请务必确认 `192.168.56.1` 是否为授权连接到此 RHEL 机器的 IP 地址。** 如果该 IP 地址来源不明或未经授权,则这些 SSH 连接需要进一步调查。其他连接都是正常的系统流量或本地服务间通信。
  ⏱ 1m 11s
Always review AI-generated content prior to use.
  ━━━╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ 13% 17k/128k
  1. 还可在 goose 的配置文件 ~/.config/goose/config.yaml 中添加以下配置来降低温度(Temperature),因为温度过高极易导致模型重复生成括号或参数。
bash 复制代码
temperature: 0.1
  1. 也可在运行 goose 前设置 OPENAI_TEMPERATURE 环境变量来降低温度(Temperature)。
bash 复制代码
$ export OPENAI_TEMPERATURE=0.1

参考

https://access.redhat.com/articles/7142302

https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/10/html/interacting_with_the_command-line_assistant/using-the-rhel-mcp-server-to-enable-ai-assistants-to-run-discover-and-troubleshoot-complex-issues

相关推荐
飞奔的瓜牛6 小时前
Nginx 生产环境运维实践:常见问题、关注要点与日常维护
运维·nginx
Eyfcom9 小时前
〖物流 SaaS 实战 02〗SaaS 租户的计费与订阅模型:从“一锤子买卖”到“按需订阅”
linux·服务器·数据库·快递系统·物流saas实战
瑞克-桑切斯10 小时前
Kubernetes 1.28 保姆级新手学习教程
运维·学习·docker·云原生·容器·kubernetes
数据库急诊室_阿哲11 小时前
一次割接后的性能复核记录:基线、样本、回滚点
运维·数据库·dba
忆挽篱笙歌11 小时前
systemV进程通信
linux·运维·服务器
飞奔的瓜牛13 小时前
Wireshark 抓包实战:呼叫中心项目中的 SIP、DTMF 与 RTP 分析
运维·wireshark
Decay__14 小时前
SQLServer 监控体系构建 —— sql_exporter + Prometheus + Grafana 全栈实战
运维·数据库·sql·mysql·架构
七夜zippoe14 小时前
流式输出:让 AI 回答像打字机一样实时返回(SSE 全链路实战)
人工智能·spring·ai·流式输出·ssf
遇雪长安14 小时前
wslc 来了:Docker Desktop 还要留着吗?
运维·docker·容器