《OpenShift / RHEL / DevSecOps 汇总目录》
文章目录
- 远程主机配置
- 本地主机配置
-
- 配置免密访问远程主机
- [为 goose 配置 MCP Server](#为 goose 配置 MCP Server)
- [用 goose 远程管理主机](#用 goose 远程管理主机)
- 参考
本文将实现以下场景:本地的 RHEL 用户使用 goose 和 MCP Server 管理维护远程的 RHEL 系统。

在进行本文前,请先完成《 在 RHEL 中使用 goose 实现本地主机运维》。
注意:
- goose 功能在 RHEL 9.8 或 10.2 以上版本才有。
- 本文已在 RHEL 10.2 上验证。
远程主机配置
远程主机既是被管理的 RHEL 主机。
- 本远程主机上创建一个允许 MCP Server 通过 SSH 远程访问的用户。
bash
$ sudo useradd mcp
$ sudo passwd mcp
本地主机配置
本地主机既是运行 goose 的 RHEL 主机。
配置免密访问远程主机
- 生成一个用来通过 SSH 访问远程主机的密钥对。
bash
$ ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_mcp -C "rhel-mcp-agent" -N ""
- 用远程主机地址替换
<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
- 在本地主机的操作用户下创建配置文件,并在其中添加访问远程主机的配置。其中
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
- 执行命令验证,确认可以访问到远程主机。
bash
$ ssh rhel-remote-01 "hostname"
rhel-01
为 goose 配置 MCP Server
- 运行命令,安装 MCP Server。
bash
$ sudo dnf install python3 python3-pip
$ pip install --user uv
$ uv tool install linux-mcp-server
- 运行
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 远程管理主机
- 在本地运行 goose。
bash
$ goose
- 在 goose 提示符下发送指令。
bash
> Check disk usage on rhel-remote-01
- 确认是对 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
- 在 goose 提示符下发送其他指令。
bash
> 检查远程机器 rhel-remote-01 上当前处于 LISTEN 状态的端口列表,以及是否有异常外部连接
- 在执行过程中确认有以下提示,这可能是模型重复生成括号或参数引起的。
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"}
- 再次发送指令,并在指令中增加 "
注意:每次仅调用一个工具,参数必须是严格合法的单层 JSON,切勿重复输出参数块" 的附加说明,以便强调返回结果的要求。
bash
> 检查远程机器 rhel-remote-01 上当前处于 LISTEN 状态的端口列表,以及是否有异常外部连接。注意:每次仅调用一个工具,参数必须是严格合法的单层 JSON,切勿重复输出参数块。
- 确认是对 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
- 还可在 goose 的配置文件
~/.config/goose/config.yaml中添加以下配置来降低温度(Temperature),因为温度过高极易导致模型重复生成括号或参数。
bash
temperature: 0.1
- 也可在运行 goose 前设置
OPENAI_TEMPERATURE环境变量来降低温度(Temperature)。
bash
$ export OPENAI_TEMPERATURE=0.1