💬 hippo:這是 Claude Code 官方文件精讀系列的其中一篇。


一、這個功能是什麼

Claude Code 的設定系統是一套分層組態機制,控制從介面主題到權限沙盒的所有行為。核心在於「作用域」概念:企業級設定優先順序最高,往下依序為命令列參數、本機設定、專案設定與使用者設定。越具體的作用域永遠會覆寫越寬泛的設定——這代表企業安全原則不會被個人設定繞過,同時團隊與個人仍能依需求彈性客製。

這套系統涉及兩個不同的設定檔:settings.json 負責管理權限、沙盒、Hooks 等執行期行為;~/.claude.json 則是獨立的全域偏好設定檔,用來儲存編輯器模式、IDE 連線等個人選項。釐清這兩個檔案的分工,是掌握 Claude Code 設定的第一步。


二、官方教學精讀

2.1 設定作用域與檔案位置

Claude Code 使用四種作用域來決定設定的套用範圍與共用策略:

作用域 位置 影響範圍 與團隊共用?
Managed 伺服器管理、MDM/登錄檔或系統級 managed-settings.json 機器上的所有使用者 是(由 IT 部署)
User ~/.claude/ 目錄 你,跨所有專案 否
Project 儲存庫中的 .claude/ 此儲存庫的所有協作者 是(提交至 git)
Local .claude/settings.local.json 你,僅限此儲存庫 否(自動 gitignore)

優先順序由高至低: Managed > 命令列參數 > Local > Project > User。Managed 設定無法被任何內容覆寫。

💡 提示:.claude/settings.local.json 建立後會自動加入 .gitignore,適合放置個人的實驗性設定。

每種作用域的典型情境:

  • Managed:企業安全政策、合規要求、IT 統一部署的組態
  • User:個人偏好(主題、編輯器設定)、跨專案通用的工具與外掛
  • Project:團隊共用設定(權限規則、Hooks、MCP 伺服器)、團隊標準化工具
  • Local:個人針對特定專案的覆寫、測試中的組態、與特定機器相關的設定

作用域適用的功能一覽:

功能 User 位置 Project 位置 Local 位置
Settings ~/.claude/settings.json .claude/settings.json .claude/settings.local.json
Subagents ~/.claude/agents/ .claude/agents/ 無
MCP 伺服器 ~/.claude.json .mcp.json ~/.claude.json(依專案)
Plugins ~/.claude/settings.json .claude/settings.json .claude/settings.local.json
CLAUDE.md ~/.claude/CLAUDE.md CLAUDE.md 或 .claude/CLAUDE.md CLAUDE.local.md

需要特別注意的是,settings.json 與 ~/.claude.json 是兩個獨立的檔案。前者管理執行期行為(權限、沙盒、Hooks 等),後者儲存全域偏好設定:

~/.claude.json 設定鍵 說明
autoConnectIde 自動連線至偵測到的 IDE
autoInstallIdeExtension 自動安裝 IDE 擴充功能
editorMode 編輯器模式偏好
showTurnDuration 顯示每輪對話耗時
terminalProgressBarEnabled 終端機進度條
teammateMode Agent 團隊成員顯示模式:auto(tmux/iTerm2 分割視窗或 in-process)、in-process、tmux

2.2 settings.json 核心設定項

一份完整的專案級 settings.json 範例如下:

1
2
3
4
5
6
7
8
9
10
11
{
"$schema": "https://json.schemastore.org/claude-code-settings.json",
"permissions": {
"allow": ["Bash(npm run lint)", "Bash(npm run test *)"],
"deny": ["Bash(curl *)", "Read(./.env)", "Read(./.env.*)", "Read(./secrets/**)"]
},
"env": {
"CLAUDE_CODE_ENABLE_TELEMETRY": "1"
},
"companyAnnouncements": ["Welcome to Acme Corp!"]
}

$schema 欄位值得特別一提——加上它之後,VS Code、Cursor 等編輯器便會自動提示可用欄位、型別與範例值,大幅降低設定寫錯的機率。

settings.json 可用設定項一覽:

鍵 描述 範例
permissions 權限控制,含 allow/ask/deny 見 2.3 節
env 注入至每個工作階段的環境變數 {"FOO": "bar"}
sandbox 沙盒設定,用來隔離 Bash 指令 見 2.3 節
attribution git 提交與 PR 的歸屬資訊 {"commit": "", "pr": ""}
hooks 工具執行前後的自訂指令 見 Hooks 文件
model 覆寫預設使用的模型 "claude-sonnet-4-6"
availableModels 限制使用者可選的模型清單 ["sonnet", "haiku"]
modelOverrides 模型 ID 至供應商特定 ID 的對應表 {"sonnet": "bedrock-id"}
effortLevel 持久化思考投入層級 "low" / "medium" / "high"
language 首選回應語言 "japanese"
cleanupPeriodDays 工作階段保留天數,預設 30 天,最小 1 天,設為 0 會觸發驗證錯誤 30
autoUpdatesChannel 更新通道 "stable" 或 "latest"
disableAllHooks 停用所有 Hooks false
companyAnnouncements 啟動時顯示的企業公告 ["Welcome!"]
enabledPlugins 控制外掛啟用/停用 見 2.4 節
worktree.symlinkDirectories worktree 中以符號連結掛載的目錄 ["node_modules"]
worktree.sparsePaths sparse-checkout 簽出的目錄 ["src/core"]
autoMode 自訂自動模式分類器規則 —
fileSuggestion 自訂 @ 檔案自動補全指令 —
statusLine 自訂狀態列設定 —
agent 以指定 Subagent 名義執行主執行緒 "code-reviewer"
apiKeyHelper 自訂認證腳本,其輸出將作為 API Key /bin/generate_temp_api_key.sh
autoMemoryDirectory 自訂自動記憶儲存目錄(僅限 policy/local/user) "~/my-memory-dir"
alwaysThinkingEnabled 預設啟用延伸思考 true
channelsEnabled (Managed)允許 Team/Enterprise 使用者使用 channels true
defaultShell 預設 shell,"bash" 或 "powershell" "powershell"
disableAutoMode 設為 "disable" 以禁止自動模式 "disable"
disableDeepLinkRegistration 禁止註冊 claude-cli:// 協定處理常式 "disable"
disableSkillShellExecution 停用技能中的 shell 指令執行 true
enableAllProjectMcpServers 自動核准專案中所有 MCP 伺服器 true
fastModePerSessionOptIn 快速模式不跨工作階段持久化 true
feedbackSurveyRate 品質問卷出現機率(0-1) 0.05
forceLoginMethod 限制登入方式(claudeai 或 console) "claudeai"
forceRemoteSettingsRefresh (Managed)啟動時強制更新遠端設定 true
includeGitInstructions 在系統提示詞中包含 git 工作流程指令 false
outputStyle 設定輸出風格 "Explanatory"
plansDirectory 自訂計畫檔案儲存目錄 "./plans"
showThinkingSummaries 顯示延伸思考摘要 true
voiceEnabled 啟用語音聽寫 true

2.3 權限規則與 Sandbox 設定

權限是 settings.json 最核心的部分。規則格式為 Tool 或 Tool(specifier),評估順序是 deny > ask > allow,第一條符合的規則勝出。

1
2
3
4
5
6
7
8
9
{
"permissions": {
"allow": ["Bash(git status *)", "Bash(git diff *)", "Bash(git log *)"],
"ask": ["Bash(git push *)", "Bash(rm *)"],
"deny": ["WebFetch", "Bash(curl *)", "Read(./.env)", "Read(./secrets/**)"],
"additionalDirectories": ["../docs/"],
"defaultMode": "acceptEdits"
}
}
規則 效果
Bash 比對所有 Bash 指令
Bash(npm run *) 比對以 npm run 開頭的指令
Read(./.env) 比對讀取 .env 檔案
WebFetch(domain:example.com) 比對向 example.com 發出的請求

⚠️ Bash 權限規則採用前綴比對,而非正規表示式。檔案路徑(Read/Write)則支援 glob 模式。

權限相關額外設定:

鍵 描述 範例
disableBypassPermissionsMode 設為 "disable" 以停用 --dangerously-skip-permissions,適合用於 Managed 設定 "disable"
skipDangerousModePermissionPrompt 跳過進入 bypass 模式時的確認提示(專案級設定會忽略此項) true

Sandbox(沙盒)可將 Bash 指令與檔案系統及網路隔離,提供額外的安全防護層:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"sandbox": {
"enabled": true,
"autoAllowBashIfSandboxed": true,
"excludedCommands": ["docker"],
"filesystem": {
"allowWrite": ["/tmp/build", "~/.kube"],
"denyRead": ["~/.aws/credentials"]
},
"network": {
"allowedDomains": ["github.com", "*.npmjs.org"],
"allowUnixSockets": ["/var/run/docker.sock"],
"allowLocalBinding": true
}
}
}
鍵 描述 預設值
enabled 啟用 Bash 沙盒 false
autoAllowBashIfSandboxed 在沙盒內自動核准 Bash 指令 true
excludedCommands 在沙盒外執行的指令 []
filesystem.allowWrite 允許寫入的額外路徑 []
filesystem.denyRead 禁止讀取的路徑 []
filesystem.denyWrite 禁止寫入的路徑(如 ["/etc"]) []
filesystem.allowRead 在 denyRead 區域內重新允許讀取(如 ["."]) []
filesystem.allowManagedReadPathsOnly (Managed)僅使用 Managed 的 allowRead false
network.allowedDomains 允許對外流量的網域(支援萬用字元) []
network.allowLocalBinding 允許綁定 localhost 連接埠(僅限 macOS) false
network.allowAllUnixSockets 允許所有 Unix socket 連線 false
network.httpProxyPort 自訂 HTTP 代理埠(如 8080) 未設定
network.socksProxyPort 自訂 SOCKS5 代理埠(如 8081) 未設定
network.allowManagedDomainsOnly (Managed)僅使用 Managed 設定中的 allowedDomains false
failIfUnavailable 沙盒無法使用時直接報錯(而非降級為非沙盒執行) false
allowUnsandboxedCommands 允許指令退回至沙盒外執行(設為 false 可完全禁止沙盒逃逸) true
enableWeakerNestedSandbox 在非特權 Docker 中使用較弱沙盒(⚠️ 會降低安全性,請依需求明確開啟) false
enableWeakerNetworkIsolation (macOS)允許存取系統 TLS 服務(⚠️ 會降低安全性,請依需求明確開啟) false

路徑前綴分為三種:/ 代表絕對路徑、~/ 代表家目錄、./ 或無前綴則代表專案根目錄。檔案系統限制與權限規則這兩種機制會合併生效。

2.4 Managed 企業設定與外掛系統

Managed 設定提供三種部署交付方式:

方式 說明
伺服器管理 透過 Claude.ai 管理員主控台,由 Anthropic 伺服器直接發布
MDM/OS 政策 macOS plist(Jamf/Kandji)、Windows 登錄檔(HKLM\SOFTWARE\Policies\ClaudeCode,使用者級 HKCU\SOFTWARE\Policies\ClaudeCode)
檔案型設定 系統目錄下的 managed-settings.json,支援 managed-settings.d/ 片段合併

進行片段合併時,會以 managed-settings.json 為基礎,將 managed-settings.d/ 中的所有 *.json 依字母順序疊加——純量值覆寫、陣列串接並去除重複、物件深度合併。以 . 開頭的隱藏檔會被忽略。建議使用數字前綴來控制載入順序,例如 10-telemetry.json 會先於 20-security.json。除了 managed-settings.json 之外,也支援 managed-mcp.json 來集中管理 MCP 伺服器組態。

外掛系統透過 enabledPlugins 控制啟用/停用,並以 extraKnownMarketplaces 註冊額外市集:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"enabledPlugins": {
"formatter@acme-tools": true,
"deployer@acme-tools": true,
"analyzer@security-plugins": false
},
"extraKnownMarketplaces": {
"acme-tools": {
"source": "github",
"repo": "acme-corp/claude-plugins"
},
"security-plugins": {
"source": "git",
"url": "https://git.example.com/security/plugins.git"
}
}
}

⚠️ 注意:extraKnownMarketplaces 的格式已從陣列改為物件格式。每個鍵為市集名稱,值包含 source 欄位。支援的 source 類型包含:github(使用 repo)、git(使用 url)、directory(本機開發)、hostPattern(正規表示式比對)、settings(內嵌宣告)。

Managed 設定還可透過 strictKnownMarketplaces 限制使用者僅能使用指定的市集(支援 github、git、url、npm、file、directory、hostPattern 七種 source 類型),並搭配 allowManagedHooksOnly、allowedHttpHookUrls、httpHookAllowedEnvVars 來嚴格控管 Hook 的安全邊界。

2.5 檔案建議與 Subagent 設定

檔案建議(File Suggestion)允許自訂 @ 檔案路徑自動補全的邏輯,特別適合大型 monorepo 搭配預先建置的索引使用:

1
2
3
4
5
6
{
"fileSuggestion": {
"type": "command",
"command": "~/.claude/file-suggestion.sh"
}
}

腳本透過 stdin 接收 JSON(包含 query 欄位),並透過 stdout 輸出以換行分隔的檔案路徑(最多 15 筆)。

Subagent 設定以 Markdown 檔案形式存放在 agents 目錄中,支援使用者級與專案級:

  • 使用者級:~/.claude/agents/ — 跨所有專案皆可使用
  • 專案級:.claude/agents/ — 專案專屬,可提交並與團隊共用

Subagent 檔案包含 YAML front matter,用來客製化系統提示詞與工具權限,詳細用法請參閱 Subagent 文件。

2.6 互動式設定指令與歸屬設定

日常檢視與調整設定時,目前官方推薦兩種方式:

  • 互動式指令:在工作階段內使用 /config 開啟互動式設定介面、以 /permissions 管理權限規則、以 /status 檢視所有生效中的設定來源與衝突;
  • **直接編輯 settings.json**:搭配前述的 $schema 自動補全,或透過 claude --settings ./custom.json 臨時覆寫單次工作階段的組態。

💡 版本變更說明:早期版本曾提供 claude config list/set/add 子指令,但在新版 Claude Code CLI 中已逐步棄用並退場,不再作為主要推薦方式。若你仍在使用舊版 CLI,對應指令參考如下:

1
2
3
4
5
# 舊版 CLI 參考(新版請改用 /config、/status 或直接編輯 settings.json)
claude config list
claude config set permissions.allow '["Bash(git *)"]'
claude config set -g theme dark
claude config add permissions.allow "Bash(npm *)"

歸屬設定(Attribution)控制 git 提交與 PR 中的 Claude 署名資訊。提交預設採用 git trailer 格式(例如 Co-Authored-By),PR 描述則為純文字:

1
2
3
4
5
6
{
"attribution": {
"commit": "Generated with AI\n\nCo-Authored-By: AI <noreply@anthropic.com>",
"pr": ""
}
}
鍵 描述
commit git 提交歸屬資訊(支援 git trailer),設為空字串即可隱藏
pr PR 描述歸屬資訊(純文字),設為空字串即可隱藏

💡 includeCoAuthoredBy 已棄用,請改用 attribution。


三、hippo 的實戰經驗

💬 hippo:以下是我實際使用過程中踩過的坑與心得:

3.1 設定分層踩過的坑

剛開始使用時,我把所有組態都塞在 ~/.claude/settings.json 裡,結果一換電腦設定全丟,團隊協作時大家的權限規則也不一致。後來我調整了策略:

  • 專案級 .claude/settings.json:放置團隊共用的權限規則與 Hooks 設定,提交至 Git
  • 使用者級 ~/.claude/settings.json:放置個人偏好,例如 cleanupPeriodDays、autoUpdatesChannel
  • **敏感操作用 ask 而非 deny**:例如 git push,我選擇每次執行前都跳出確認而非直接封鎖——實務上還是有不少情境確實需要推送

3.2 $schema 的實際價值

加上一行 "$schema": "https://json.schemastore.org/claude-code-settings.json" 後,編輯器就會自動補全所有可用欄位。這東西看似不起眼,但實際體驗是——設定寫錯的機率從「經常發生」降到「幾乎歸零」。Claude Code 的設定項超過 50 個,靠手動輸入真的非常容易拼錯。

3.3 我的建議

  1. 優先在專案級設定權限:團隊成員應遵循一致的規則,避免出現「你本機能跑但 CI 掛掉」的窘境
  2. 善用 /config 與 $schema 降低手寫出錯率:修改 settings.json 後可直接在工作階段內即時核對,無須依賴舊版 claude config 子指令
  3. 用 /status 排除組態問題:如果某條權限規則沒有生效,/status 會清楚列出所有設定來源與衝突狀況

四、常見問題

Q: settings.json 與 settings.local.json 有什麼差別?

A: settings.json 會提交至 Git 與團隊共用;settings.local.json 會被 Git 忽略,專門放置個人實驗性組態。兩者合併時,local 的同名設定會覆寫共用設定。

Q: 權限規則是正規表示式比對嗎?

A: 不是。Bash 權限規則採用前綴比對——Bash(git *) 會比對所有以 git 開頭的指令。檔案路徑(Read/Write)則支援 glob 模式,例如 Read(./secrets/**)。

Q: 如何檢視目前生效的完整組態?

A: 在 Claude Code 中執行 /status,即可看到所有啟用中的設定來源、各自來自哪個檔案,以及是否存在衝突或錯誤。

Q: 如何停用 git 提交中的 Claude 署名?

A: 將 attribution.commit 設為空字串即可:

1
2
3
4
5
6
{
"attribution": {
"commit": "",
"pr": ""
}
}

五、小結

Claude Code 的設定系統透過五層優先順序,實現了「企業管控 + 團隊協作 + 個人客製化」的平衡。記住三個重點:

  1. 作用域優先順序:Managed > 命令列 > Local > Project > User,越具體的設定永遠勝出
  2. **善用 $schema**:一行設定換來編輯器自動補全,CP 值極高
  3. 權限評估順序:deny 優先於 ask,ask 優先於 allow,第一條符合的規則勝出

系列導覽


本文精讀自 Claude Code 設定 - Claude Code 官方文件

最後更新:2026-04-09