Skip to content

Latest commit

 

History

History
303 lines (214 loc) · 5.77 KB

File metadata and controls

303 lines (214 loc) · 5.77 KB

Codex 多账号管理器 🚀

中文 | English

一个跨平台的 Codex CLI 多账号管理工具,让你在多个 OpenAI Codex 账号之间秒速切换,无需重复登录!

✨ 特性

  • 🔄 秒速切换:在多个账号之间即时切换,无需重新登录
  • 🎯 自动命名:自动生成 codex1, codex2, codex3... 默认账号名
  • 🌍 跨平台:支持 macOS, Linux, Windows (Git Bash/WSL)
  • 💾 零敏感信息:只备份 auth.json,token 会自动刷新
  • 📦 便携性强:克隆到新机器直接用,无需重新登录
  • 🎨 彩色输出:清晰的视觉反馈

📋 使用场景

  • ✅ 绕过单账号的 rate limit
  • ✅ 工作账号和个人账号分离
  • ✅ 多个项目使用不同的 Codex 订阅
  • ✅ 在多台机器间同步账号配置

🚀 快速开始

1. 克隆仓库

cd ~
git clone https://github.com/zongmin-yu/codex-switch.git
cd codex-switch
chmod +x codex-switch

2. 保存第一个账号

# 确保已经登录了第一个账号
codex login

# 保存为 codex1(自动命名)
./codex-switch save

# 或者使用自定义名字
./codex-switch save work

3. 保存第二个账号

# 登出当前账号
codex logout

# 登录第二个账号
codex login

# 保存为 codex2(自动命名)
./codex-switch save

# 或者使用自定义名字
./codex-switch save personal

4. 在账号间切换

# 切换到 codex1
./codex-switch switch codex1

# 切换到 work 账号
./codex-switch switch work

# 查看所有账号
./codex-switch list

📖 命令详解

save [name] - 保存当前登录

# 自动命名(codex1, codex2, codex3...)
./codex-switch save

# 自定义名字
./codex-switch save my-work-account
./codex-switch save school

switch <name> - 切换账号

./codex-switch switch codex1
./codex-switch switch work

# 短命令
./codex-switch sw codex1

list - 列出所有账号

./codex-switch list

# 输出示例:
# Available profiles:
#   codex1 (active) - alice@example.com
#   codex2 - bob@company.com

delete <name> - 删除账号

./codex-switch delete codex3
./codex-switch rm old-account

rename <old> <new> - 重命名账号

./codex-switch rename codex1 work
./codex-switch mv codex2 personal

🔧 高级用法

添加到 PATH(推荐)

# 添加到 ~/.zshrc 或 ~/.bashrc
echo 'export PATH="$HOME/codex-switch:$PATH"' >> ~/.zshrc
source ~/.zshrc

# 现在可以在任何地方使用
codex-switch list
codex-switch switch work

创建别名(更方便)

# 添加到 ~/.zshrc 或 ~/.bashrc
alias cx='codex-switch'

# 使用
cx list
cx sw work

自定义备份目录

# 临时修改
CODEX_ACCOUNT_BACKUP_DIR=/path/to/custom/dir ./codex-switch save

# 永久修改(添加到 ~/.zshrc)
export CODEX_ACCOUNT_BACKUP_DIR="$HOME/Dropbox/codex-backups"

🌍 在新机器上使用

方法 1:Git 同步(推荐)

# 在机器 A 上
cd ~/codex-switch
git add backups/*.json
git commit -m "Add codex accounts"
git push

# 在机器 B 上
cd ~
git clone https://github.com/zongmin-yu/codex-switch.git
cd codex-switch
chmod +x codex-switch

# 直接切换,无需登录!
./codex-switch switch codex1

方法 2:手动复制

# 只需复制 backups/ 目录
scp -r ~/codex-switch/backups user@remote:~/codex-switch/

⚠️ 安全注意事项

什么可以安全地提交到 GitHub?

可以提交:

  • codex-switch 脚本本身
  • README.md 文档
  • .gitignore 文件

不要提交:

  • backups/*.json 文件(包含你的 auth tokens)
  • .env 或其他包含密钥的文件

推荐的 .gitignore

# 不要提交账号备份
backups/*.json

# 但保留目录结构
!backups/.gitkeep

私有仓库方案

如果你想用 Git 同步备份,有两个选择:

  1. 使用私有仓库(推荐)

    # 创建私有仓库同步账号配置
    git remote add origin git@github.com:zongmin-yu/my-codex-accounts.git
  2. 使用加密

    # 使用 git-crypt 或 GPG 加密 backups/ 目录
    git-crypt init
    echo "backups/*.json filter=git-crypt diff=git-crypt" >> .gitattributes

🐛 故障排查

错误:Codex config directory not found

原因:未安装 Codex 或从未登录过

解决

# 安装 Codex
npm install -g @openai/codex

# 或使用 bun
bun install -g @openai/codex

# 登录
codex login

切换后显示 "Unauthorized"

原因:Token 已过期(通常是 30 天后)

解决

# 重新登录并保存
codex login
./codex-switch save codex1

无法识别邮箱和账号类型

原因:系统未安装 jq 工具

解决(可选,脚本功能不受影响):

# macOS
brew install jq

# Ubuntu/Debian
sudo apt install jq

# CentOS/RHEL
sudo yum install jq

📂 目录结构

~/codex-switch/
├── codex-switch          # 主脚本
├── README.md             # 英文文档
├── README_CN.md          # 中文文档
├── backups/              # 账号备份目录
│   ├── .gitkeep         # 保持目录结构
│   ├── codex1.json      # 第一个账号
│   ├── codex2.json      # 第二个账号
│   └── work.json        # 自定义名字的账号
└── .gitignore           # Git 忽略配置

🤝 贡献

欢迎提交 Issue 和 Pull Request!

📜 许可证

MIT License - 随便用!

🙏 致谢

灵感来源于 Reddit 上的 这个帖子


Made with ❤️ for Codex power users

如有问题,欢迎提 Issue!