用Rust编写的MCP(模型上下文协议)服务器,提供获取Linux系统信息和通过PolicyKit安全认证在终端执行命令的工具。
🔐 安全性:此服务器使用PolicyKit(pkexec)进行root命令的身份验证——系统会弹出一个原生窗口请求您的密码,该密码永远不会通过MCP传输!
要执行需要管理员权限的命令(如apt update,systemctl restart等),添加use_polkit: true:
{
"name": "execute_command",
"arguments": {
"command": "apt",
"args": ["update"],
"use_polkit": true
}
}
会发生什么:系统会弹出一个原生窗口请求您的密码(就像您安装程序时一样)。您的密码永远不会通过MCP发送——操作系统负责安全的身份验证。
❌ 错误(会导致“权限被拒绝”):
{
"command": "apt",
"args": ["update"]
}
✅ 正确(添加use_polkit: true):
{
"command": "apt",
"args": ["update"],
"use_polkit": true
}
get_system_info:获取详细的系统信息
execute_command:在终端执行命令
use_polkit: true):使用pkexec并显示系统原生图形窗口——推荐用于需要root权限的命令无需执行命令即可访问系统信息:
linux://logs/system - 系统日志的最后100行(journalctl)linux://logs/auth - 最近50条SSH认证日志linux://config/network - 当前网络配置(ip addr show)linux://processes/top - 按内存使用量排名的前10个进程linux://system/status - 系统总体状态(CPU、内存、运行时间)引导式完成常见的管理任务:
system_troubleshooting - 交互式诊断问题(CPU、内存、磁盘、网络、进程)security_audit - 系统安全审计(基本或全面范围)service_management - 管理systemd服务(状态、启动、停止、重启、启用、禁用)log_analysis - 日志分析(系统、认证、内核、应用程序)disk_cleanup - 安全清理磁盘(保守或激进模式)cargo build --release
二进制文件将生成在target/release/linux-mcp
./target/release/linux-mcp
服务器通过标准I/O(stdin/stdout)遵循MCP协议进行通信。
🤖 详细指南:请参阅CLAUDE_DESKTOP_SETUP.md获取完整的逐步说明!
快速概览:
cargo build --release~/.config/Claude/claude_desktop_config.json(Linux){
"mcpServers": {
"linux-mcp": {
"command": "/完整路径到/linux-mcp-wrapper.sh",
"args": [],
"env": {}
}
}
}
配置路径:
~/.config/Claude/claude_desktop_config.json~/Library/Application Support/Claude/claude_desktop_config.json%APPDATA%\Claude\claude_desktop_config.json在项目的根目录创建.cursor/mcp_config.json(Cursor)或.vscode/mcp.json(VS Code)文件:
# 对于Cursor
mkdir -p .cursor
nano .cursor/mcp_config.json
# 对于VS Code
mkdir -p .vscode
nano .vscode/mcp.json
文件内容:
{
"mcpServers": {
"linux-mcp": {
"command": "/home/marcos/Documents/Pessoal/linux-mcp/target/release/linux-mcp",
"args": [],
"env": {}
}
}
}
在~/.config/cursor/mcp_config.json或~/.config/Code/User/mcp.json中进行全局配置
npx @modelcontextprotocol/inspector /完整路径到/linux-mcp
{
"name": "get_system_info",
"arguments": {
"info_type": "all"
}
}
{
"name": "get_system_info",
"arguments": {
"info_type": "cpu"
}
}
{
"name": "execute_command",
"arguments": {
"command": "ls",
"args": ["-la", "/home"]
}
}
{
"name": "execute_command",
"arguments": {
"command": "apt",
"args": ["update"],
"use_polkit": true
}
}
结果:系统原生窗口请求密码 → 安全执行命令 ✅
会发生什么:一个官方系统图形窗口出现,请求您的管理员密码(就像您通过应用中心安装程序时一样)。
资源提供了无需执行命令即可直接访问系统信息的方式:
{
"method": "resources/read",
"params": {
"uri": "linux://logs/system"
}
}
{
"method": "resources/read",
"params": {
"uri": "linux://config/network"
}
}
{
"method": "resources/read",
"params": {
"uri": "linux://processes/top"
}
}
{
"method": "resources/read",
"params": {
"uri": "linux://system/status"
}
}
提示引导您完成常见的管理任务:
{
"method": "prompts/get",
"params": {
"name": "system_troubleshooting",
"arguments": {
"problem_type": "cpu"
}
}
}
{
"method": "prompts/get",
"params": {
"name": "security_audit",
"arguments": {
"scope": "full"
}
}
}
{
"method": "prompts/get",
"params": {
"name": "service_management",
"arguments": {
"service_name": "nginx",
"action": "restart"
}
}
}
{
"method": "prompts/get",
"params": {
"name": "log_analysis",
"arguments": {
"log_type": "auth",
"priority": "warning"
}
}
}
{
"method": "prompts/get",
"params": {
"name": "disk_cleanup",
"arguments": {
"aggressive": "true"
}
}
}
use_polkit: true需要root权限的命令(如apt update,systemctl restart等)必须包括提升方法:
| 无提升方法的命令 ❌ | 正确的命令 ✅ |
|---|---|
"command": "apt", "args": ["update"] | "command": "apt", "args": ["update"], "use_polkit": true |
"command": "systemctl", "args": ["restart", "nginx"] | "command": "systemctl", "args": ["restart", "nginx"], "use_polkit": true |
✅ PolicyKit更安全:显示身份验证的图形对话框,并允许细粒度的权限控制。请参阅完整的PolicyKit指南获取详细说明。
PolicyKit是Linux原生的管理员特权认证系统。
# Ubuntu/Debian
sudo apt install polkitd policykit-1
# Fedora/RHEL
sudo dnf install polkit
# Arch Linux
sudo pacman -S polkit
检查安装:
which pkexec
systemctl status polkit
{
"name": "execute_command",
"arguments": {
"command": "systemctl",
"args": ["restart", "nginx"],
"use_polkit": true
}
}
会发生什么:
pkexec systemctl restart nginx窗口外观:
✅ 您的密码永远不会通过MCP发送——操作系统负责身份验证!
为了允许某些命令无需密码,创建自定义规则:
# 复制示例文件
sudo cp examples/polkit/50-linux-mcp.rules /etc/polkit-1/rules.d/
# 编辑为您的用户
sudo nano /etc/polkit-1/rules.d/50-linux-mcp.rules
# 重启polkit
sudo systemctl restart polkit
📖 完整指南:请参阅examples/polkit/README_POLKIT.md获取详细说明、示例和故障排除。
原因:MCP没有访问图形会话。
解决方案1:在MCP中配置环境变量
{
"mcpServers": {
"linux-mcp": {
"command": "/完整路径到/linux-mcp",
"env": {
"DISPLAY": ":0",
"XAUTHORITY": "/home/您的用户/.Xauthority",
"DBUS_SESSION_BUS_ADDRESS": "unix:path=/run/user/1000/bus"
}
}
}
}
解决方案2:检查polkit代理是否正在运行
# 检查进程
ps aux | grep polkit
# 手动启动(GNOME/Ubuntu)
/usr/libexec/polkit-gnome-authentication-agent-1 &
# 手动启动(KDE)
/usr/lib/polkit-kde-authentication-agent-1 &
解决方案:安装PolicyKit
# Ubuntu/Debian
sudo apt install policykit-1 polkitd
# Fedora/RHEL
sudo dnf install polkit
# Arch Linux
sudo pacman -S polkit
检查安装:
which pkexec
systemctl status polkit
原因:您的用户没有权限或PolicyKit规则阻止了它。
解决方案:配置PolicyKit规则
# 复制规则示例
sudo cp examples/polkit/50-linux-mcp.rules /etc/polkit-1/rules.d/
# 编辑并替换“marcos”为您的用户
sudo nano /etc/polkit-1/rules.d/50-linux-mcp.rules
# 重启polkit
sudo systemctl restart polkit
查看错误日志:
journalctl -u polkit -f
原因:您忘记添加use_polkit: true。
错误示例:
{
"exit_code": 100,
"stderr": "E: 无法打开锁文件 - open (13: 权限被拒绝)",
"elevation_method": "none"
}
解决方案:添加提升方法:
{
"command": "apt",
"args": ["update"],
"use_polkit": true // ← 添加这个!
}
# 实时查看PolicyKit的日志
journalctl -u polkit -f
# 在终端手动测试pkexec
pkexec systemctl status nginx
# 查看所有可用的PolicyKit操作
pkaction
# 检查环境变量
echo $DISPLAY
echo $DBUS_SESSION_BUS_ADDRESS
execute_command工具可以在系统上执行任何命令。
已实施的安全措施:
最佳实践:
use_polkit: true)examples/polkit/)journalctl -u polkit -f本项目是开源的,采用MIT许可证。
欢迎贡献!随时提出issues或pull requests。