Claude Code
Claude Code 是 Anthropic 推出的官方命令行编程工具,基于 Claude 大语言模型,可以直接在终端中辅助你编写代码、分析项目、执行系统管理任务。它能够理解整个代码库的上下文,直接读写文件、运行命令,是服务器端开发和运维的强大助手。
- CentOS Stream 9 & 10 / AlmaLinux 9 & 10 / Rocky Linux 9 & 10
- Node.js 22 或更高版本,推荐当前 LTS Node.js 24(Node.js 20 已于 2026-04 EOL)
- Anthropic API 密钥(从 console.anthropic.com 获取)
- 网络能访问 Anthropic API
安装 Node.js
Section titled “安装 Node.js”EL 默认仓库中的 Node.js 版本可能较旧(EL 9.8 / EL 10 官方仓库当前分别为 Node.js 24 / 22),推荐通过 NodeSource 仓库安装最新 LTS 版本。
方式一:使用 NodeSource 仓库(推荐)
Section titled “方式一:使用 NodeSource 仓库(推荐)”# 安装 Node.js 24.x LTScurl -fsSL https://rpm.nodesource.com/setup_24.x | sudo bash -sudo dnf install -y nodejs方式二:使用 dnf module(仅 EL 9)
Section titled “方式二:使用 dnf module(仅 EL 9)”# 查看可用的 Node.js 版本sudo dnf module list nodejs
# 启用并安装 Node.js 24sudo dnf module enable nodejs:24 -ysudo dnf install -y nodejs验证安装:
node --versionnpm --version安装 Claude Code
Section titled “安装 Claude Code”使用 npm 全局安装:
npm install -g @anthropic-ai/claude-code验证安装:
claude --version更新 Claude Code
Section titled “更新 Claude Code”npm update -g @anthropic-ai/claude-code设置 API 密钥
Section titled “设置 API 密钥”首次运行 claude 时会引导你完成认证。你也可以提前通过环境变量设置:
# 临时设置export ANTHROPIC_API_KEY="sk-ant-xxxxx"
# 永久设置(添加到 shell 配置文件)echo 'export ANTHROPIC_API_KEY="sk-ant-xxxxx"' >> ~/.bashrcsource ~/.bashrc使用第三方 API 代理
Section titled “使用第三方 API 代理”如果你通过 API 代理服务访问,可以自定义 API 地址:
export ANTHROPIC_BASE_URL="https://your-proxy.example.com"export ANTHROPIC_API_KEY="your-key"Claude Code 的配置文件位于 ~/.claude/ 目录下。你可以通过交互命令 /config 调整设置,或直接编辑配置文件:
ls -la ~/.claude/启动交互式会话
Section titled “启动交互式会话”在项目目录中直接运行:
cd /path/to/your/projectclaude进入交互式界面后,你可以直接用自然语言描述需求。
单次命令模式
Section titled “单次命令模式”使用 -p 参数进行单次提问,适合脚本调用:
# 单次提问claude -p "解释这个项目的目录结构"
# 分析特定文件claude -p "分析 /etc/nginx/nginx.conf 的配置,有什么安全隐患?"通过管道传入内容
Section titled “通过管道传入内容”# 分析日志journalctl -u nginx --since "1 hour ago" | claude -p "分析这些日志,有什么异常?"
# 审查代码cat app.py | claude -p "审查这段代码的安全性"
# 分析系统状态top -bn1 | claude -p "分析系统负载情况"常用交互命令
Section titled “常用交互命令”在交互模式中,以 / 开头的是内置命令:
| 命令 | 说明 |
|---|---|
/help | 显示帮助信息 |
/config | 修改配置 |
/clear | 清除对话上下文 |
/compact | 压缩对话历史以节省 token |
/cost | 查看当前会话的 API 用量 |
实用场景示例
Section titled “实用场景示例”你:检查当前系统的磁盘使用情况,找出占用空间最大的目录你:帮我写一个 bash 脚本,定时清理 /var/log 下超过 30 天的日志文件你:创建一个 Python Flask API,提供 /health 和 /status 端点你:阅读当前项目的代码,写一份部署到 Rocky Linux 上的步骤配置文件管理
Section titled “配置文件管理”你:帮我生成一个 Nginx 反向代理配置,将 /api 请求转发到后端 8080 端口,启用 SSL你:检查 /etc/ssh/sshd_config 的安全配置,给出加固建议MCP 服务器
Section titled “MCP 服务器”Claude Code 支持 MCP(Model Context Protocol)服务器,可以扩展其能力。MCP 服务器可以为 Claude Code 提供额外的工具和数据源。
配置 MCP 服务器
Section titled “配置 MCP 服务器”在项目根目录创建 .mcp.json 文件:
{ "mcpServers": { "filesystem": { "command": "npx", "args": [ "-y", "@modelcontextprotocol/server-filesystem", "/opt/data" ] }, "postgres": { "command": "npx", "args": [ "-y", "@modelcontextprotocol/server-postgres", "postgresql://user:pass@localhost:5432/mydb" ] } }}也可以在 ~/.claude/ 目录下创建全局 MCP 配置。
常用 MCP 服务器
Section titled “常用 MCP 服务器”通过 Claude Code 交互命令添加:
# 添加文件系统 MCPclaude mcp add filesystem npx -y @modelcontextprotocol/server-filesystem /opt/data
# 添加 PostgreSQL MCPclaude mcp add postgres npx -y @modelcontextprotocol/server-postgres "postgresql://user:pass@localhost/db"
# 查看已配置的 MCP 服务器claude mcp list在 EL 服务器上的使用技巧
Section titled “在 EL 服务器上的使用技巧”搭配 tmux 使用
Section titled “搭配 tmux 使用”在服务器上推荐通过 tmux 运行 Claude Code,防止 SSH 断连导致会话丢失:
# 创建新的 tmux 会话tmux new -s claude
# 在 tmux 中运行 claudeclaude
# 断开时按 Ctrl+B 然后按 D# 重新连接tmux attach -t claude设置 SELinux 上下文
Section titled “设置 SELinux 上下文”如果 Claude Code 需要操作受 SELinux 保护的文件:
# 查看文件的 SELinux 上下文ls -Z /var/www/html/
# 如果 Claude Code 创建的文件上下文不对,可以修复sudo restorecon -Rv /var/www/html/在生产服务器上使用时,建议用普通用户运行 Claude Code,避免使用 root:
# 创建专用用户sudo useradd -m claude-usersudo su - claude-user
# 安装和运行npm install -g @anthropic-ai/claude-codeexport ANTHROPIC_API_KEY="sk-ant-xxxxx"claude离线或受限网络环境
Section titled “离线或受限网络环境”如果服务器通过代理访问外网:
# 设置 HTTPS 代理export HTTPS_PROXY="http://proxy.example.com:7890"export ANTHROPIC_API_KEY="sk-ant-xxxxx"claudenpm 全局安装权限问题
Section titled “npm 全局安装权限问题”如果遇到权限错误:
# 方式一:修改 npm 全局目录mkdir -p ~/.npm-globalnpm config set prefix '~/.npm-global'echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrcsource ~/.bashrcnpm install -g @anthropic-ai/claude-code
# 方式二:使用 sudo(不推荐)sudo npm install -g @anthropic-ai/claude-codeNode.js 版本过低
Section titled “Node.js 版本过低”# 检查版本node --version
# 如果低于 22,按照上述方式升级 Node.js检查网络连通性和代理设置:
# 测试 API 连通性(model 换成当前可用的模型 ID,见 Anthropic 官方文档)curl -v https://api.anthropic.com/v1/messages \ -H "x-api-key: $ANTHROPIC_API_KEY" \ -H "content-type: application/json" \ -H "anthropic-version: 2023-06-01" \ -d '{"model":"claude-sonnet-4-5","max_tokens":10,"messages":[{"role":"user","content":"hi"}]}'