Claude Code 的权限系统让你精确控制 AI 可以执行哪些操作, 在自动化效率和操作安全之间取得平衡。
四种权限模式
通过 --permission-mode 或 settings.json 设置:
| 模式 | 说明 | 适用场景 |
|---|---|---|
default | 每次危险操作都需确认 | 日常交互开发(默认) |
acceptEdits | 自动接受文件编辑,其他操作仍需确认 | 大量文件修改任务 |
bypassPermissions | 跳过所有权限检查 | 完全信任的自动化脚本 |
plan | 只分析和规划,不执行任何操作 | 安全地探索和评审 |
bash
# 使用特定模式启动
claude --permission-mode acceptEdits
claude --permission-mode plan "分析这段代码有什么问题"
# 跳过所有确认(⚠️ 谨慎使用)
claude --dangerously-skip-permissions工具级别的精确控制
允许特定工具无需确认(--allowedTools)
bash
# 允许只读操作无需确认
claude --allowedTools "Read" "Bash(git log *)" "Bash(git diff *)"
# 允许特定测试命令
claude --allowedTools "Read" "Write" "Bash(npm test)" "Bash(pytest *)"
# 允许常用 Git 操作
claude --allowedTools "Bash(git status)" "Bash(git log *)" "Bash(git diff *)" "Bash(git add *)" "Bash(git commit *)"禁用特定工具(--disallowedTools)
bash
# 禁用文件编辑(只读模式)
claude --disallowedTools "Write" "Edit" "MultiEdit"
# 禁用 Bash 执行(只能读写文件,不能运行命令)
claude --disallowedTools "Bash"
# 禁用网络工具(离线模式)
claude --disallowedTools "WebFetch" "WebSearch"Bash 命令白名单语法
bash
# 精确匹配
"Bash(npm test)" # 只允许 npm test,不允许 npm install
# 通配符匹配
"Bash(npm *)" # 允许所有 npm 命令
"Bash(git *)" # 允许所有 git 命令
"Bash(pytest src/*)" # 允许 pytest 测试 src 目录
# 多个规则
claude --allowedTools "Bash(git *)" "Bash(npm test)" "Bash(npm run lint)" "Read" "Write"持久化配置:settings.json
不想每次启动都加参数?在 settings.json 中配置:
json
// ~/.claude/settings.json(全局生效)
// 或 my-project/.claude/settings.json(项目级)
{
"permissions": {
"allow": [
"Read",
"Write",
"Bash(git *)",
"Bash(npm test)",
"Bash(npm run *)"
],
"deny": [
"Bash(rm *)",
"Bash(sudo *)"
]
}
}CLAUDE.md 中声明权限期望
markdown
# CLAUDE.md
## 操作权限说明
### 允许直接执行(无需确认)
- 读取任何文件
- 编辑 src/ 目录下的文件
- 运行测试:`npm test`、`pytest`
- Git 只读操作:status、log、diff
### 需要确认后才能执行
- 删除文件
- 修改 package.json 依赖
- 运行 `npm install`
- 提交代码(`git commit`)
### 禁止执行(除非明确要求)
- `rm -rf` 等破坏性命令
- 修改 .env 文件
- 部署相关命令CI/CD 自动化场景配置
bash
# GitHub Actions 中的安全配置
claude --dangerously-skip-permissions --allowedTools "Read" "Write" "Bash(npm test)" "Bash(npm run build)" --disallowedTools "Bash(rm *)" "Bash(git push *)" -p "运行完整测试套件并输出结果"yaml
# .github/workflows/claude-review.yml
- name: Claude Code Review
run: |
claude --permission-mode plan --disallowedTools "Write" "Edit" "Bash" -p "审查这个 PR 的代码变更,输出审查意见"不同场景的推荐配置
| 场景 | 推荐配置 |
|---|---|
| 日常开发 | 默认模式,遇到危险操作再决定 |
| 大规模重构 | acceptEdits 模式,节省确认时间 |
| 代码审查 | plan 模式,只分析不修改 |
| CI 自动测试 | bypassPermissions + 严格 allowedTools 白名单 |
| 学习/探索 | plan 或禁用 Write/Bash 工具 |
来源:Claude Code 官方文档 - docs.anthropic.com/en/docs/claude-code