Skip to content

设置参考

Claude Code 提供丰富的配置项,可以通过 settings.json 文件或 /config 命令调整。本文是所有配置选项的完整参考。

配置作用域

Claude Code 使用分层配置系统,不同位置的配置影响不同范围:

作用域位置影响范围是否共享
托管MDM/注册表/managed-settings.json机器上所有用户是(IT 部署)
用户~/.claude/settings.json你的所有项目
项目.claude/settings.json仓库所有协作者是(提交到 git)
本地.claude/settings.local.json仅你在此仓库否(gitignore)

使用场景

  • 托管:全组织强制执行的安全策略、合规要求
  • 用户:个人偏好(主题、编辑器)、跨项目工具
  • 项目:团队共享的权限、Hooks、MCP 服务器
  • 本地:个人对特定项目的覆盖、测试配置

优先级(从高到低)

  1. 托管设置(不可覆盖)
  2. 命令行参数(临时覆盖)
  3. 本地项目设置(.claude/settings.local.json
  4. 共享项目设置(.claude/settings.json
  5. 用户设置(~/.claude/settings.json

数组合并:数组类型的设置(如 permissions.allowsandbox.filesystem.allowWrite)跨作用域会合并(拼接去重),不是覆盖。低优先级可以追加,高优先级也可以追加。

配置文件示例

json
{
  "$schema": "https://json.schemastore.org/claude-code-settings.json",
  "permissions": {
    "allow": [
      "Bash(npm run lint)",
      "Bash(npm run test *)",
      "Read(~/.zshrc)"
    ],
    "deny": [
      "Bash(curl *)",
      "Read(./.env)",
      "Read(./.env.*)",
      "Read(./secrets/**)"
    ]
  },
  "env": {
    "CLAUDE_CODE_ENABLE_TELEMETRY": "1"
  }
}

加上 $schema 行后,VS Code 和 Cursor 会提供自动补全和验证。

核心设置项

说明示例值
model覆盖默认模型"claude-sonnet-4-6"
effortLevel思考深度:low/medium/high"medium"
alwaysThinkingEnabled默认开启扩展思考true
languageClaude 的响应语言"japanese"
env每次会话都注入的环境变量{"FOO": "bar"}
autoUpdatesChannel更新频道:lateststable"stable"
cleanupPeriodDays会话历史保留天数(0=不保留)30
plansDirectory计划文件存储目录"./plans"
statusLine自定义状态栏显示见下方
outputStyle调整系统提示的输出风格"Explanatory"
agent以指定子代理的身份运行主线程"code-reviewer"
includeGitInstructions是否注入内置 git 工作流指令(默认 truefalse
companyAnnouncements启动时循环显示的公告["Welcome to Acme!"]

权限设置

详细语法见权限配置文档。

说明示例值
permissions.allow允许的工具规则数组["Bash(git diff *)"]
permissions.ask需要确认的工具规则数组["Bash(git push *)"]
permissions.deny拒绝的工具规则数组["WebFetch", "Read(./.env)"]
permissions.additionalDirectories额外可访问的目录["../docs/"]
permissions.defaultMode默认权限模式"acceptEdits"
permissions.disableBypassPermissionsMode禁止 bypassPermissions 模式"disable"

沙箱设置

沙箱隔离 Bash 命令的文件系统和网络访问,与权限系统互补。

json
{
  "sandbox": {
    "enabled": true,
    "autoAllowBashIfSandboxed": true,
    "excludedCommands": ["docker", "git"],
    "filesystem": {
      "allowWrite": ["/tmp/build", "~/.kube"],
      "denyRead": ["~/.aws/credentials"]
    },
    "network": {
      "allowedDomains": ["github.com", "*.npmjs.org"],
      "allowUnixSockets": ["/var/run/docker.sock"],
      "allowLocalBinding": true
    }
  }
}

沙箱选项详解

说明默认值
sandbox.enabled启用沙箱(macOS、Linux、WSL2)false
sandbox.autoAllowBashIfSandboxed沙箱内自动批准 Bash 命令true
sandbox.excludedCommands在沙箱外运行的命令[]
sandbox.allowUnsandboxedCommands允许通过 dangerouslyDisableSandbox 参数逃出沙箱true

文件系统沙箱

说明
sandbox.filesystem.allowWrite沙箱内可写的额外路径(数组跨作用域合并)
sandbox.filesystem.denyWrite沙箱内禁止写入的路径
sandbox.filesystem.denyRead沙箱内禁止读取的路径
sandbox.filesystem.allowReaddenyRead 区域内重新允许读取(优先级更高)

网络沙箱

说明默认值
sandbox.network.allowedDomains允许的出站域名(支持 *.example.com 通配符)
sandbox.network.allowUnixSockets允许访问的 Unix Socket 路径
sandbox.network.allowAllUnixSockets允许所有 Unix Socket 连接false
sandbox.network.allowLocalBinding允许绑定 localhost 端口(仅 macOS)false
sandbox.network.httpProxyPort自定义 HTTP 代理端口
sandbox.network.socksProxyPort自定义 SOCKS5 代理端口

沙箱路径前缀

前缀含义
/从文件系统根的绝对路径
~/相对于家目录
./ 或无前缀相对于项目根(项目设置)或 ~/.claude(用户设置)

模型设置

json
{
  "model": "claude-sonnet-4-6",
  "availableModels": ["sonnet", "haiku"],
  "modelOverrides": {
    "claude-opus-4-6": "arn:aws:bedrock:us-east-1:123456789:inference-profile/..."
  }
}
说明
model覆盖默认模型
availableModels限制用户可以通过 /model 选择的模型列表
modelOverrides将 Anthropic 模型 ID 映射到 Bedrock ARN 等提供商 ID

归因设置

Claude Code 默认在 git commit 和 PR 中添加归因信息:

json
{
  "attribution": {
    "commit": "🤖 Generated with [Claude Code](https://claude.com/claude-code)\n\nCo-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>",
    "pr": ""
  }
}

设为空字符串可隐藏归因。

Worktree 设置

优化大型单仓库(monorepo)中 --worktree 的性能:

json
{
  "worktree": {
    "symlinkDirectories": ["node_modules", ".cache"],
    "sparsePaths": ["packages/my-app", "shared/utils"]
  }
}
说明
worktree.symlinkDirectories从主仓库软链接到 worktree 的目录,避免磁盘重复占用
worktree.sparsePaths使用 git sparse-checkout 只检出指定路径,加速大型仓库

文件建议设置

自定义 @ 文件路径补全命令:

json
{
  "fileSuggestion": {
    "type": "command",
    "command": "~/.claude/file-suggestion.sh"
  }
}

命令通过 stdin 接收 JSON:

json
{"query": "src/comp"}

在 stdout 输出换行分隔的文件路径(最多 15 条)。

状态栏设置

json
{
  "statusLine": {
    "type": "command",
    "command": "~/.claude/statusline.sh"
  }
}

插件设置

json
{
  "enabledPlugins": {
    "formatter@acme-tools": true,
    "deployer@acme-tools": true,
    "analyzer@security-plugins": false
  },
  "extraKnownMarketplaces": {
    "acme-tools": {
      "source": "github",
      "repo": "acme-corp/claude-plugins"
    }
  }
}

/plugin 命令交互式管理插件(浏览、安装、启用/禁用)。

MCP 相关设置

说明
enableAllProjectMcpServers自动批准项目 .mcp.json 中的所有 MCP 服务器
enabledMcpjsonServers只批准列出的 MCP 服务器
disabledMcpjsonServers拒绝列出的 MCP 服务器

全局配置项(~/.claude.json)

这些项存储在 ~/.claude.json,不在 settings.json 中(否则会报 schema 错误):

说明默认值
autoConnectIde从外部终端启动时自动连接 IDEfalse
autoInstallIdeExtension从 VS Code 终端启动时自动安装扩展true
editorMode输入框键绑定模式:normalvim"normal"
showTurnDuration响应后显示用时信息true
terminalProgressBarEnabled显示终端进度条true

企业托管设置(仅 managed-settings.json)

说明
disableBypassPermissionsMode设为 "disable" 全局禁止跳过权限模式
allowManagedPermissionRulesOnly仅允许托管权限规则,用户/项目规则无效
allowManagedHooksOnly仅允许托管 Hooks
allowManagedMcpServersOnly仅允许托管 MCP 服务器白名单
channelsEnabledTeam/Enterprise 用户启用 Channels 功能
allowedMcpServersMCP 服务器白名单
deniedMcpServersMCP 服务器黑名单
strictKnownMarketplaces允许的插件市场
blockedMarketplaces屏蔽的插件市场
pluginTrustMessage自定义插件信任警告附加信息
forceLoginMethod限定登录方式:claudeaiconsole
forceLoginOrgUUID自动选择指定组织(需配合 forceLoginMethod

托管设置文件位置

系统路径
macOS/Library/Application Support/ClaudeCode/managed-settings.json
Linux/WSL/etc/claude-code/managed-settings.json
WindowsC:\Program Files\ClaudeCode\managed-settings.json

验证当前设置

在 Claude Code 中运行 /status 可以查看所有激活的配置来源,包括托管设置来自哪里(远程、plist、注册表还是文件)。如果 settings.json 有语法错误,/status 会报告具体问题。

排除敏感文件

推荐在 .claude/settings.json 中添加:

json
{
  "permissions": {
    "deny": [
      "Read(./.env)",
      "Read(./.env.*)",
      "Read(./secrets/**)",
      "Read(./config/credentials.json)"
    ]
  }
}

相关资源