配置与设置
约 466 字大约 2 分钟
2026-03-20
配置文件
Claude Code 的配置通过 settings.json 管理,支持多级作用域:
| 文件 | 位置 | 作用域 | 提交 Git |
|---|---|---|---|
~/.claude/settings.json | 用户主目录 | 全局 | N/A |
.claude/settings.json | 项目根目录 | 项目级 | 是 |
.claude/settings.local.json | 项目根目录 | 项目级(本地) | 否 |
优先级:本地 > 项目 > 全局。
常用配置项
权限控制
配置允许自动执行的工具:
{
"permissions": {
"allow": [
"Read",
"Glob",
"Grep",
"Edit",
"Write",
"Bash(npm run *)",
"Bash(pnpm *)",
"Bash(git *)"
],
"deny": [
"Bash(rm -rf *)"
]
}
}allow 中支持 glob 模式匹配 Bash 命令。
模型设置
{
"model": "claude-sonnet-4-6",
"smallFastModel": "claude-haiku-4-5-20251001"
}环境变量
{
"env": {
"NODE_ENV": "development",
"DEBUG": "true"
}
}MCP Server
详见 MCP Server 章节。
Hooks
详见 Hooks 钩子 章节。
完整配置示例
{
"permissions": {
"allow": [
"Read",
"Glob",
"Grep",
"Edit",
"Write",
"Bash(npm run *)",
"Bash(git status)",
"Bash(git diff *)",
"Bash(git log *)"
]
},
"model": "claude-sonnet-4-6",
"env": {
"NODE_ENV": "development"
},
"mcpServers": {
"github": {
"command": "npx",
"args": ["-y", "@anthropic-ai/mcp-server-github"],
"env": {
"GITHUB_TOKEN": "ghp_xxx"
}
}
},
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"hooks": [
{
"type": "command",
"command": "npx prettier --write $CLAUDE_FILE_PATH 2>/dev/null || true"
}
]
}
]
}
}CLI 参数
除了配置文件,也可以通过 CLI 参数覆盖设置:
| 参数 | 说明 |
|---|---|
--model <model> | 指定模型 |
--allowedTools <tools> | 允许自动执行的工具列表 |
--dangerously-skip-permissions | 跳过所有权限确认 |
--max-turns <n> | 限制最大交互轮数 |
--verbose | 详细日志输出 |
--no-telemetry | 禁用遥测 |
Memory 记忆系统
Claude Code 有一个基于文件的记忆系统,存储在 ~/.claude/projects/ 下:
~/.claude/
├── CLAUDE.md # 全局指令
├── settings.json # 全局设置
└── projects/
└── {project-path}/
└── memory/
├── MEMORY.md # 记忆索引
├── user_role.md # 用户信息
├── feedback_xxx.md # 反馈记忆
└── project_xxx.md # 项目记忆记忆类型:
| 类型 | 说明 |
|---|---|
user | 用户角色、偏好、知识背景 |
feedback | 用户对 Claude 行为的纠正和确认 |
project | 项目上下文、进行中的工作 |
reference | 外部资源的链接和说明 |
Claude 会在后续对话中自动加载相关记忆,确保跨会话的连续性。
