在 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

相关推荐
在世修行1 小时前
智学文枢:变式题生成引擎
python·ai·pyside·题目生成
Industio_触觉智能1 小时前
把NPU检测直接嵌进采集终端|RK3576 AI采集终端全链路拆解教程-下
嵌入式硬件·ai·硬件架构·npu·ai视觉·rk3576·采集终端
Linux运维技术栈3 小时前
CVE-2026-91403 高危漏洞修复:Rocky Linux 9 多版本 JDK 环境清理实操
java·linux·运维
云上先途13 小时前
企业 AI 服务部署周期多久,会不会影响原有业务运转?
大数据·运维·人工智能
执伞人ONLY4 小时前
AI知识库自动化知识加工没做对,人工整理永远追不上知识产生
运维·人工智能·自动化
slongzhang_4 小时前
linux 递归删除源码文件|递归删除目录下 *.js,但排除 *.min.js
linux·开发语言·javascript
叼烟扛炮4 小时前
Linux 学习笔记(三十三):线程互斥——互斥锁(Mutex)与死锁
linux·运维·服务器·笔记·学习·1024程序员节
汤姆宝库4 小时前
安全深拆:越狱物理、注入攻击与纵深防御
人工智能·安全·机器学习·ai