|
| 1 | +# SSH Multi-Account Setup (macOS + GitHub) |
| 2 | + |
| 3 | +Safe, script-first tooling to manage multiple GitHub accounts on one machine with separate SSH keys, host aliases, and dry-run defaults. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Audits and backs up existing `~/.ssh` state before changes. |
| 8 | +- Generates separate Ed25519 keys per account. |
| 9 | +- Adds keys to `ssh-agent` and macOS keychain. |
| 10 | +- Optionally uploads keys through `gh` after explicit confirmation. |
| 11 | +- Writes a managed block in `~/.ssh/config` with account aliases. |
| 12 | +- Validates aliases using `ssh -T git@<alias>`. |
| 13 | +- Updates existing repo remotes with a safe dry-run workflow. |
| 14 | + |
| 15 | +## Repository Layout |
| 16 | + |
| 17 | +- `scripts/setup.sh`: Main idempotent setup flow. |
| 18 | +- `scripts/update-remotes.sh`: Convert remotes to alias-based SSH URLs. |
| 19 | +- `scripts/backup-keys.sh`: Back up SSH files. |
| 20 | +- `scripts/generate-key.sh`: Standalone key creation helper. |
| 21 | +- `scripts/generate-gh-actions-key.sh`: Optional CI key helper. |
| 22 | +- `examples/owner-map.conf.example`: Owner-to-alias mapping example. |
| 23 | +- `examples/gitconfig-includeIf.example`: Git identity routing example. |
| 24 | +- `docs/audit-and-verify.md`: Manual audit and verification steps. |
| 25 | +- `tests/lint.sh`: Shell lint script. |
| 26 | + |
| 27 | +## Prerequisites |
| 28 | + |
| 29 | +- macOS with OpenSSH |
| 30 | +- Bash 4+ |
| 31 | +- `git` |
| 32 | +- Optional: `gh` (GitHub CLI) for key upload |
| 33 | +- Optional: `shellcheck` for lint checks |
| 34 | + |
| 35 | +## Quick Start |
| 36 | + |
| 37 | +```bash |
| 38 | +chmod +x scripts/*.sh tests/lint.sh |
| 39 | +scripts/setup.sh --dry-run |
| 40 | +scripts/setup.sh --apply |
| 41 | +``` |
| 42 | + |
| 43 | +Interactive defaults: |
| 44 | + |
| 45 | +- accounts: `personal,work` |
| 46 | +- key names: `id_ed25519_personal`, `id_ed25519_work` |
| 47 | +- aliases: `github-personal`, `github-work` |
| 48 | + |
| 49 | +Explicit example: |
| 50 | + |
| 51 | +```bash |
| 52 | +scripts/setup.sh --apply \ |
| 53 | + --accounts "personal,work" \ |
| 54 | + --email-personal "you@personal.email" \ |
| 55 | + --email-work "you@work.email" |
| 56 | +``` |
| 57 | + |
| 58 | +Non-interactive example: |
| 59 | + |
| 60 | +```bash |
| 61 | +scripts/setup.sh --apply --yes \ |
| 62 | + --accounts "personal,work" \ |
| 63 | + --email-personal "you@personal.email" \ |
| 64 | + --email-work "you@work.email" \ |
| 65 | + --key-personal "id_ed25519_personal" \ |
| 66 | + --key-work "id_ed25519_work" \ |
| 67 | + --alias-personal "github-personal" \ |
| 68 | + --alias-work "github-work" |
| 69 | +``` |
| 70 | + |
| 71 | +## Update Remotes |
| 72 | + |
| 73 | +Dry-run by default: |
| 74 | + |
| 75 | +```bash |
| 76 | +scripts/update-remotes.sh --root "$HOME/code" --map examples/owner-map.conf.example --dry-run |
| 77 | +``` |
| 78 | + |
| 79 | +Apply changes (with confirmation): |
| 80 | + |
| 81 | +```bash |
| 82 | +scripts/update-remotes.sh --root "$HOME/code" --map ~/.ssh/owner-map.conf --apply |
| 83 | +``` |
| 84 | + |
| 85 | +## Manual Commands (Reference) |
| 86 | + |
| 87 | +Audit: |
| 88 | + |
| 89 | +```bash |
| 90 | +ls -la ~/.ssh |
| 91 | +ls -1 ~/.ssh/*.pub 2>/dev/null || echo "no .pub files found" |
| 92 | +ssh-add -l || echo "no keys loaded" |
| 93 | +gh ssh-key list |
| 94 | +``` |
| 95 | + |
| 96 | +Backup: |
| 97 | + |
| 98 | +```bash |
| 99 | +mkdir -p ~/ssh-backups |
| 100 | +cp -v ~/.ssh/id_* ~/ssh-backups/ 2>/dev/null || echo "copied any id_* keys" |
| 101 | +ls -la ~/ssh-backups |
| 102 | +``` |
| 103 | + |
| 104 | +Generate keys: |
| 105 | + |
| 106 | +```bash |
| 107 | +ssh-keygen -t ed25519 -C "you@personal.email" -f ~/.ssh/id_ed25519_personal |
| 108 | +ssh-keygen -t ed25519 -C "you@work.email" -f ~/.ssh/id_ed25519_work |
| 109 | +``` |
| 110 | + |
| 111 | +Add to agent/keychain: |
| 112 | + |
| 113 | +```bash |
| 114 | +eval "$(ssh-agent -s)" |
| 115 | +ssh-add --apple-use-keychain ~/.ssh/id_ed25519_personal |
| 116 | +ssh-add --apple-use-keychain ~/.ssh/id_ed25519_work |
| 117 | +ssh-add -l |
| 118 | +``` |
| 119 | + |
| 120 | +Upload with `gh` (optional): |
| 121 | + |
| 122 | +```bash |
| 123 | +gh auth login |
| 124 | +gh ssh-key add ~/.ssh/id_ed25519_personal.pub --title "MacBook Personal $(date +%F)" |
| 125 | +gh auth login |
| 126 | +gh ssh-key add ~/.ssh/id_ed25519_work.pub --title "MacBook Work $(date +%F)" |
| 127 | +``` |
| 128 | + |
| 129 | +Use aliases in remotes: |
| 130 | + |
| 131 | +```bash |
| 132 | +git remote set-url origin git@github-personal:your-personal-username/repo.git |
| 133 | +git remote set-url origin git@github-work:your-work-username/repo.git |
| 134 | +``` |
| 135 | + |
| 136 | +Validate: |
| 137 | + |
| 138 | +```bash |
| 139 | +ssh -T git@github-personal |
| 140 | +ssh -T git@github-work |
| 141 | +ssh -vT git@github-personal 2>&1 | sed -n '1,200p' |
| 142 | +``` |
| 143 | + |
| 144 | +## Safety Rules |
| 145 | + |
| 146 | +- Never delete keys silently. |
| 147 | +- Keep backups until all fetch/push checks are successful. |
| 148 | +- `update-remotes.sh` is dry-run by default and confirms before apply. |
| 149 | +- `setup.sh` prompts before risky operations unless `--yes` is used. |
| 150 | + |
| 151 | +## Verification Checklist |
| 152 | + |
| 153 | +- [ ] `ssh -T git@github-personal` authenticates to the personal username. |
| 154 | +- [ ] `ssh -T git@github-work` authenticates to the work username. |
| 155 | +- [ ] Critical repositories can fetch and push. |
| 156 | +- [ ] `gh ssh-key list` reflects intended keys. |
| 157 | +- [ ] Backups exist under `~/ssh-backups/<timestamp>`. |
| 158 | + |
| 159 | +## Caution About Old Keys |
| 160 | + |
| 161 | +Only remove old keys after successful verification across all repos. Prefer staged cleanup and revoke old keys in GitHub first. |
0 commit comments