第六部分:首次启动与验证
6.1 启动 Claude Code 的三种方式
方式1:标准交互模式(最常用)
bash
# 在任意目录启动
claude
# 启动流程:
# 1. 检测当前目录
# 2. 加载CLAUDE.md(如果存在)
# 3. 进入交互式对话界面方式2:单次命令模式
bash
# 执行单个命令后退出
claude "你的问题或指令"
# 示例:
claude "What is 2 + 2?"
claude "List files in current directory"
claude "Explain this code: app.js"方式3:打印模式(脚本友好)
bash
# 只输出AI响应,不显示格式
claude -p "你的问题"
# 示例:
claude -p "hello" > output.txt
echo "分析这段代码" | claude -p6.2 首次启动的初始化流程
当你第一次运行 claude 时,会经历一个交互式配置向导。
配置步骤1:选择主题
? Choose your theme:
❯ Light (浅色主题,适合白天)
Dark (深色主题,适合夜晚)
System (跟随系统设置,推荐)使用 ↑/↓ 箭头键选择,按回车确认。
主题说明:
| 主题 | 特点 | 适用场景 |
|---|---|---|
| Light | 浅色背景,深色文字 | 光线充足的环境 |
| Dark | 深色背景,浅色文字 | 长时间编程,护眼 |
| System | 自动跟随系统 | 推荐选择 |
配置步骤2:安全须知确认
╭─────────────────────────────────────────────────────────╮
│ Safety Notice │
├─────────────────────────────────────────────────────────┤
│ Claude Code will operate in the current directory: │
│ /Users/yourname/projects/my-app │
│ │
│ This means Claude can: │
│ ✓ Read files in this directory and subdirectories │
│ ✓ Create new files │
│ ✓ Modify existing files (with your confirmation) │
│ ✓ Run commands (with your confirmation) │
│ │
│ Claude will NOT: │
│ ✗ Access files outside this directory │
│ ✗ Access your personal data │
│ ✗ Execute commands without permission │
╰─────────────────────────────────────────────────────────╯
? Do you understand and accept these conditions?
❯ Yes, I understand and accept
No, exit and reconsider重要理解 - Claude Code的权限模型:
- 沙盒隔离 - 只能访问当前目录
- 确认机制 - 危险操作需要你确认
- 只读优先 - 默认只读,修改需授权
- 审计日志 - 所有操作都有记录
配置步骤3:目录信任确认
? Trust this directory?
/Users/yourname/projects/my-app
❯ Yes, trust this directory
No, exit
Trust this directory and all parent directories不要信任以下目录:
- ✗ 下载目录(可能有恶意代码)
- ✗ 临时目录(不需要持久访问)
- ✗ 系统目录(危险!)
- ✗ 不明来源的代码目录
配置步骤4:认证方式选择
? How would you like to authenticate?
❯ API Key (recommended for API users)
Use environment variable: ANTHROPIC_API_KEY
Most flexible and secure
Claude App Login (for Pro/Max subscribers)
Login via browser
Uses your subscription quota
Manual Entry
Enter API key now
Stored in config file认证方式对比:
| 方式 | 优点 | 缺点 | 推荐度 |
|---|
第三方平台与 AWS Bedrock(v2.1.92,摘自官方 release):v2.1.92 写明,在登录界面选择 「3rd-party platform」 时,可使用 interactive Bedrock setup wizard,引导完成 AWS authentication, region configuration, credential verification, and model pinning。菜单文案与步骤顺序以你安装的 CLI 版本为准;与仅使用 API Key / modelOverrides 走 Bedrock 的路径是否等价,请按官方文档区分场景。
配置步骤5:完成初始化
╭─────────────────────────────────────────────────────────╮
│ Setup Complete! 🎉 │
├─────────────────────────────────────────────────────────┤
│ Configuration summary: │
│ ✓ Theme: System │
│ ✓ Authentication: API Key (environment variable) │
│ ✓ Trusted directory: /Users/yourname/projects/my-app │
│ ✓ Model: claude-sonnet-4 (default) │
│ │
│ Quick start: │
│ • Type your message to chat with Claude │
│ • Use /help to see available commands │
│ • Use /exit to quit │
╰─────────────────────────────────────────────────────────╯
Claude Code v2.1.92
Working directory: /Users/yourname/projects/my-app
You: █6.3 配置文件结构
Claude Code的配置分为全局和项目两级:
~/.claude/ ← 全局配置目录
├── config.json ← 全局配置文件
├── auth-token.json ← 认证令牌
├── trusted-directories.json ← 信任的目录列表
├── cache/ ← 缓存目录
└── logs/ ← 日志目录
项目目录/.claude/ ← 项目级配置
├── config.json ← 项目配置(覆盖全局)
├── commands/ ← 自定义命令
├── skills/ ← 自定义技能
└── hooks/ ← 自定义钩子