基于 SSH 的 MCP (Model Context Protocol) 服务器,允许通过 MCP 协议远程执行 SSH 命令。
📝 项目介绍
ssh-mcp-server 是一个桥接工具,可以让 AI 助手等支持 MCP 协议的应用通过标准化接口执行远程 SSH 命令。这使得 AI 助手能够安全地操作远程服务器,执行命令并获取结果,而无需直接暴露 SSH 凭据给 AI 模型。
✨ 功能亮点
- 🔒 安全连接:支持多种安全的 SSH 连接方式,包括密码认证和私钥认证(支持带密码的私钥)
- 🛡️ 命令安全控制:通过灵活的黑白名单机制,精确控制允许执行的命令范围,防止危险操作
- 🔄 标准化接口:符合 MCP 协议规范,与支持该协议的 AI 助手无缝集成
- 📂 文件传输:支持双向文件传输功能,可上传本地文件到服务器或从服务器下载文件
- 🔑 凭据隔离:SSH 凭据完全在本地管理,不会暴露给 AI 模型,增强安全性
- 🚀 即用即走:使用 NPX 可直接运行,无需全局安装,方便快捷
📦 开源仓库
GitHub:https://github.com/classfang/ssh-mcp-server
NPM: https://www.npmjs.com/package/@fangjunjie/ssh-mcp-server
🛠️ 工具列表
| 工具 | 名称 | 描述 |
|---|---|---|
| execute-command | 命令执行工具 | 在远程服务器上执行 SSH 命令并获取执行结果 |
| upload | 文件上传工具 | 将本地文件上传到远程服务器指定位置 |
| download | 文件下载工具 | 从远程服务器下载文件到本地指定位置 |
| list-servers | 服务器列表工具 | 列出所有可用SSH服务器配置 |
📚 使用方法
🔧 MCP 配置示例
⚠️ 重要提示 : 在 MCP 配置文件中,每个命令行参数和其值必须是
args数组中的独立元素。不要用空格将它们连接在一起。例如,使用"--host", "192.168.1.1"而不是"--host 192.168.1.1"。
⚙️ 命令行选项
选项:
--config-file JSON 配置文件路径(推荐用于多服务器配置)
--ssh-config-file SSH 配置文件路径(默认: ~/.ssh/config)
--ssh SSH 连接配置(可以是 JSON 字符串或旧格式)
-h, --host SSH 服务器主机地址或 SSH 配置中的别名
-p, --port SSH 服务器端口
-u, --username SSH 用户名
-w, --password SSH 密码
-k, --privateKey SSH 私钥文件路径
-P, --passphrase 私钥密码(如果有的话)
-W, --whitelist 命令白名单,以逗号分隔的正则表达式
-B, --blacklist 命令黑名单,以逗号分隔的正则表达式
-s, --socksProxy SOCKS 代理地址 (e.g., socks://user:password@host:port)
--pty 为命令执行分配伪终端 (默认: true)
--pre-connect 启动时预连接所有配置的 SSH 服务器
🔑 使用密码
bash
{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456"
]
}
}
}
🔐 使用私钥
bash
{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--privateKey", "~/.ssh/id_rsa"
]
}
}
}
🔏 使用带密码私钥
bash
{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--privateKey", "~/.ssh/id_rsa",
"--passphrase", "pwd123456"
]
}
}
}
📋 使用 ~/.ssh/config
你可以使用 ~/.ssh/config 文件中定义的主机别名。服务器会自动从 SSH 配置中读取连接参数:
bash
{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "myserver"
]
}
}
}
假设你的 ~/.ssh/config 包含:
Host myserver
HostName 192.168.1.1
Port 22
User root
IdentityFile ~/.ssh/id_rsa
你也可以指定自定义的 SSH 配置文件路径:
bash
{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "myserver",
"--ssh-config-file", "/path/to/custom/ssh_config"
]
}
}
}
注意 :命令行参数优先级高于 SSH 配置值。例如,如果你指定了 --port 2222,它会覆盖 SSH 配置中的端口。
🌐 使用 SOCKS 代理
bash
{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456",
"--socksProxy", "socks://username:password@proxy-host:proxy-port"
]
}
}
}
📝 使用命令白名单和黑名单
使用 --whitelist 和 --blacklist 参数可以限制可执行的命令范围,多个模式之间用逗号分隔。每个模式都是一个正则表达式,用于匹配命令。
示例:使用命令白名单
bash
{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456",
"--whitelist", "^ls( .*)?,^cat .*,^df.*"
]
}
}
}
示例:使用命令黑名单
bash
{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.1.1",
"--port", "22",
"--username", "root",
"--password", "pwd123456",
"--blacklist", "^rm .*,^shutdown.*,^reboot.*"
]
}
}
}
注意:如果同时指定了白名单和黑名单,系统会先检查命令是否在白名单中,然后再检查是否在黑名单中。命令必须同时通过两项检查才能被执行。
🧩 多SSH连接用法示例
有三种方式可以配置多个 SSH 连接:
📄 方式一:使用配置文件(推荐)
创建 JSON 配置文件(例如 ssh-config.json):
数组格式:
bash
[
{
"name": "dev",
"host": "1.2.3.4",
"port": 22,
"username": "alice",
"password": "{abc=P100s0}",
"socksProxy": "socks://127.0.0.1:10808"
},
{
"name": "prod",
"host": "5.6.7.8",
"port": 22,
"username": "bob",
"password": "yyy",
"socksProxy": "socks://127.0.0.1:10808"
}
]
对象格式:
bash
{
"dev": {
"host": "1.2.3.4",
"port": 22,
"username": "alice",
"password": "{abc=P100s0}",
"socksProxy": "socks://127.0.0.1:10808"
},
"prod": {
"host": "5.6.7.8",
"port": 22,
"username": "bob",
"password": "yyy",
"socksProxy": "socks://127.0.0.1:10808"
}
}
然后使用 --config-file 参数:
bash
{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--config-file", "ssh-config.json"
]
}
}
}
🔧 方式二:使用 JSON 格式的 --ssh 参数
可以直接传递 JSON 格式的配置字符串:
bash
{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--ssh", "{\"name\":\"dev\",\"host\":\"1.2.3.4\",\"port\":22,\"username\":\"alice\",\"password\":\"{abc=P100s0}\",\"socksProxy\":\"socks://127.0.0.1:10808\"}",
"--ssh", "{\"name\":\"prod\",\"host\":\"5.6.7.8\",\"port\":22,\"username\":\"bob\",\"password\":\"yyy\",\"socksProxy\":\"socks://127.0.0.1:10808\"}"
]
}
}
}
📝 方式三:旧格式逗号分隔(向后兼容)
对于密码中不包含特殊字符的简单情况,仍可使用旧格式:
npx @fangjunjie/ssh-mcp-server \
--ssh "name=dev,host=1.2.3.4,port=22,user=alice,password=xxx" \
--ssh "name=prod,host=5.6.7.8,port=22,user=bob,password=yyy"
⚠️ 注意 :旧格式在处理包含特殊字符(如
=、,、{、})的密码时可能会有问题。如果密码包含特殊字符,请使用方式一或方式二。
在MCP工具调用时,通过 connectionName 参数指定目标连接名称,未指定时使用默认连接。
示例(在prod连接上执行命令):
{
"tool": "execute-command",
"params": {
"cmdString": "ls -al",
"connectionName": "prod"
}
}
示例(带超时选项的命令执行):
{
"tool": "execute-command",
"params": {
"cmdString": "ping -c 10 127.0.0.1",
"connectionName": "prod",
"timeout": 5000
}
}
⏱️ 命令执行超时
execute-command 工具支持超时选项,防止命令无限期挂起:
- timeout: 命令执行超时时间(毫秒,可选,默认为30000ms)
这对于像 ping、tail -f 或其他可能阻塞执行的长时间运行进程特别有用。
🗂️ 列出所有SSH服务器
可以通过MCP工具 list-servers 获取所有可用的SSH服务器配置:
调用示例:
{
"tool": "list-servers",
"params": {}
}
返回示例:
[
{ "name": "dev", "host": "1.2.3.4", "port": 22, "username": "alice" },
{ "name": "prod", "host": "5.6.7.8", "port": 22, "username": "bob" }
]
🛡️ 安全注意事项
该服务器提供了在远程服务器上执行命令和传输文件的强大功能。为确保安全使用,请注意以下几点:
- 命令白名单 :强烈建议 使用
--whitelist选项来限制可执行的命令集合。如果没有白名单,任何命令都可以在远程服务器上执行,这可能带来重大的安全风险。 - 私钥安全 :服务器会将 SSH 私钥读入内存。请确保运行
ssh-mcp-server的机器是安全的。不要将服务器暴露给不受信任的网络。 - 拒绝服务攻击 (DoS):服务器没有内置的速率限制。攻击者可能通过向服务器发送大量连接请求或大文件传输来发起 DoS 攻击。建议在具有速率限制功能的防火墙或反向代理后面运行服务器。
- 路径遍历 :服务器内置了对本地文件系统路径遍历攻击的保护。但是,仍然需要注意在
upload和download命令中使用的路径。
🎮 演示
🖥️ Cursor 接入


🖥️workbuddy接入ssh-mcp-server


填入如下内容,密码根据实际情况修改
bash
{
"mcpServers": {
"ssh-mcp-server": {
"command": "npx",
"args": [
"-y",
"@fangjunjie/ssh-mcp-server",
"--host", "192.168.44.129",
"--port", "22",
"--username", "root",
"--password", "pwd123456",
"--blacklist", "^rm .*,^shutdown.*,^reboot.*"
]
}
}
}
告诉AI,你要干什么
例如:登录192.168.44.129,查看磁盘使用率和负载情况,输出为md格式

查看生成的报告:
服务器状态报告
主机地址: 192.168.44.129
采集时间: 2026-03-22 22:32:40
系统运行时长: 已运行 1 天 12 小时 24 分钟
登录用户数: 4 人
一、系统负载
| 指标 | 值 |
|---|---|
| 1 分钟平均负载 | 0.27 |
| 5 分钟平均负载 | 0.13 |
| 15 分钟平均负载 | 0.12 |
| CPU 核心数 | 2 |
| 总进程数 | 254 |
| 运行中进程 | 1 |
| 睡眠进程 | 253 |
| 僵尸进程 | 0 |
💡 评估: 当前 1 分钟负载为 0.27,CPU 核心数为 2,负载比值约 0.14 ,整体负载较低,系统运行平稳。
二、CPU 使用情况
| 类型 | 占比 |
|---|---|
| 用户态 (us) | 47.1% |
| 内核态 (sy) | 38.1% |
| 空闲 (id) | 0.0% |
| 硬件中断 (hi) | 11.6% |
| 软件中断 (si) | 3.2% |
| I/O 等待 (wa) | 0.0% |
⚠️ 注意: CPU 空闲率为 0%,用户态 + 内核态占用超过 85%,CPU 使用率偏高,建议关注具体进程。
三、内存使用情况
| 类型 | 总量 | 已用 | 空闲 | 缓存/缓冲 | 可用 |
|---|---|---|---|---|---|
| 物理内存 | 7.7 GiB | 1.1 GiB | 3.8 GiB | 3.2 GiB | 6.6 GiB |
| Swap | 4.0 GiB | 0 B | 4.0 GiB | --- | --- |
✅ 评估: 内存使用约 14.3%,Swap 未使用,内存资源充裕。
四、磁盘使用情况
| 文件系统 | 总大小 | 已用 | 可用 | 使用率 | 挂载点 |
|---|---|---|---|---|---|
| /dev/mapper/rlm_rocky-root | 35 G | 4.7 G | 31 G | 14% | / |
| /dev/nvme0n1p1 | 960 M | 217 M | 744 M | 23% | /boot |
| devtmpfs | 4.0 M | 0 | 4.0 M | 0% | /dev |
| tmpfs | 3.9 G | 0 | 3.9 G | 0% | /dev/shm |
| tmpfs | 1.6 G | 9.1 M | 1.6 G | 1% | /run |
| tmpfs | 791 M | 0 | 791 M | 0% | /run/user/0 |
✅ 评估: 根分区使用率仅 14% ,/boot 分区使用率 23%,磁盘空间充足,无告警风险。
五、整体健康摘要
| 项目 | 状态 | 说明 |
|---|---|---|
| 系统负载 | ✅ 正常 | 负载比值 0.14,远低于警戒线(1.0) |
| CPU 使用率 | ⚠️ 偏高 | 空闲率 0%,建议排查高占用进程 |
| 内存使用 | ✅ 正常 | 使用率约 14.3%,Swap 未启用 |
| 磁盘空间 | ✅ 正常 | 根分区剩余 31G,使用率 14% |
报告由 WorkBuddy 自动生成
执行黑名单的命令

使用飞书机器人操作linux服务器

