devAlice
← Mac

Mac에서 GitHub 여러 계정 SSH 분리 — 회사/개인 키 한 번에 관리

회사 GitHub + 개인 GitHub + 외부 클라이언트 등 여러 호스트·계정을 ~/.ssh/config로 깔끔하게 분리하는 정공법.

회사 GitHub 계정과 개인 GitHub 계정을 한 머신에서 동시에 쓰면 첫날부터 막힌다 — git push가 잘못된 계정으로 가거나, 클론한 직후 권한 부족으로 거부되거나, gh CLI 인증이 계속 한 계정으로만 들어가거나. 이 가이드는 macOS 14+ / OpenSSH 9.x 기준으로 여러 계정·호스트를 ~/.ssh/config로 분리하는 정공법을 정리한다.

Mac 초기 셋업으로 ed25519 키 1개를 만든 다음 단계. 회사·개인뿐 아니라 외부 클라이언트의 GitLab·Bitbucket까지 확장 가능.

TL;DR

  1. 계정마다 별도 SSH 키 (ed25519) 생성 + 다른 파일명
  2. ~/.ssh/config에 호스트별 IdentityFile + Host 별칭 명시
  3. clone URL을 git@github.com:user/repo.git 대신 git@github-work:user/repo.git별칭으로
  4. 작업 폴더별 .gitconfig includeIf로 user.email 자동 분기
  5. gh CLI는 여러 계정 동시 인증 가능 (1.x+)

사전 조건

  • macOS 14+, OpenSSH 9.x (ssh -V)
  • 각 계정에 GitHub/GitLab 계정 존재
  • 기존 Mac 초기 셋업 또는 동등한 1개 키 셋업 완료

1. 계정별 SSH 키 생성 — 5분

키 파일명을 의도적으로 다르게.

# 회사 계정
ssh-keygen -t ed25519 -C "work@example.com" -f ~/.ssh/id_ed25519_work
 
# 개인 계정
ssh-keygen -t ed25519 -C "personal@example.com" -f ~/.ssh/id_ed25519_personal
 
# 외부 클라이언트 (선택)
ssh-keygen -t ed25519 -C "client-foo@example.com" -f ~/.ssh/id_ed25519_client

각 GitHub/GitLab 계정에 공개키만 등록:

pbcopy < ~/.ssh/id_ed25519_work.pub       # 회사 GitHub
pbcopy < ~/.ssh/id_ed25519_personal.pub   # 개인 GitHub

브라우저에서 https://github.com/settings/ssh/new → 각 계정 로그인 후 등록.

💡 한 키를 두 계정에 등록하면 GitHub가 거부한다 ("Key is already in use"). 그래서 키 분리가 필수.


2. ~/.ssh/config 작성 — 10분

호스트 별칭으로 분리. macOS Keychain 통합 + Touch ID 확장 가능.

# ~/.ssh/config
 
# 회사 GitHub
Host github-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_work
  IdentitiesOnly yes
  AddKeysToAgent yes
  UseKeychain yes
 
# 개인 GitHub
Host github-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_personal
  IdentitiesOnly yes
  AddKeysToAgent yes
  UseKeychain yes
 
# 외부 클라이언트 GitLab
Host gitlab-client
  HostName gitlab.com
  User git
  IdentityFile ~/.ssh/id_ed25519_client
  IdentitiesOnly yes
  AddKeysToAgent yes
  UseKeychain yes

핵심 옵션:

  • IdentitiesOnly yes — agent에 다른 키가 등록돼 있어도 명시 키만 사용. 이게 빠지면 다른 키로 시도되고 GitHub가 403 반환.
  • AddKeysToAgent yes + UseKeychain yes — macOS Keychain 통합. passphrase 1회 후 매번 입력 안 함.

키 ssh-agent에 등록

ssh-add --apple-use-keychain ~/.ssh/id_ed25519_work
ssh-add --apple-use-keychain ~/.ssh/id_ed25519_personal
ssh-add --apple-use-keychain ~/.ssh/id_ed25519_client

검증

ssh -T git@github-work
# Hi work-username! You've successfully authenticated...
 
ssh -T git@github-personal
# Hi personal-username! You've successfully authenticated...

다른 계정으로 인사받으면 §3.1 트러블슈팅.


3. clone URL 별칭 사용 — 매일 쓰는 패턴

회사 repo:

# 일반 GitHub URL — IdentitiesOnly로 키가 분기되지만 깔끔히 별칭 사용 권장
git clone git@github.com:org/repo.git  # IdentitiesOnly + 첫 매칭 키 — 모호
 
# 별칭 사용 — 명시적
git clone git@github-work:org/repo.git

개인 repo:

git clone git@github-personal:me/myrepo.git

이미 클론된 repo는 remote 변경:

cd ~/work/repo
git remote set-url origin git@github-work:org/repo.git
 
cd ~/personal/myrepo
git remote set-url origin git@github-personal:me/myrepo.git

git remote -v로 확인.


4. user.email 자동 분기 — includeIf — 5분

폴더 위치로 git config user.email을 자동 분기. 회사 폴더에서는 회사 이메일, 개인 폴더에서는 개인 이메일 강제.

~/.gitconfig 메인

[user]
  name = Your Name
  # 기본 이메일은 비워두거나 가장 자주 쓰는 것
 
[includeIf "gitdir:~/work/"]
  path = ~/.gitconfig-work
 
[includeIf "gitdir:~/personal/"]
  path = ~/.gitconfig-personal

⚠️ gitdir: 다음 경로는 반드시 trailing slash 필요. ~/work 가 아니라 ~/work/.

~/.gitconfig-work

[user]
  email = you@company.com
  name = Your Name (Company)
 
[commit]
  gpgsign = true   # 회사 정책상 서명 필수면
 
[gpg]
  format = ssh
 
[gpg "ssh"]
  defaultKeyCommand = ssh-add -L
  program = ssh-keygen

~/.gitconfig-personal

[user]
  email = you@personal.com
  name = Your Name

검증

cd ~/work/some-repo
git config user.email
# you@company.com
 
cd ~/personal/some-repo
git config user.email
# you@personal.com

자동 분기 동작. 잘못된 이메일로 커밋되는 사고를 구조적으로 차단.


5. gh CLI 여러 계정 — 동시 인증

gh 2.40+부터 multi-account 지원. 한 머신에서 회사·개인 GitHub.com 동시 인증.

추가 인증

# 첫 계정 (이미 인증돼 있다면 건너뜀)
gh auth login
 
# 두 번째 계정
gh auth login    # 같은 GitHub.com 다른 계정 입력

활성 계정 전환

gh auth status
# 현재 인증된 계정 목록 + 활성 계정 표시
 
gh auth switch
# 인터랙티브 — 다른 계정으로 전환
 
gh auth switch -u personal-username

Per-repo 활성 계정 (선택)

# 회사 repo에서 자동으로 회사 계정 활성
cd ~/work/repo
gh auth switch -u work-username

gh 명령은 활성 계정 기준. SSH는 §2 config로 분기되므로 별도 충돌 없음.


6. 자주 막히는 패턴

6.1 Personal Access Token (PAT) 충돌

gh auth login 시 HTTPS 방식 선택하면 OS Keychain에 PAT 저장. 이 PAT가 다른 계정의 작업 시 부적절하게 사용되는 경우:

gh auth status                    # 어떤 PAT가 어디 저장됐는지
security find-internet-password -s github.com -a "your-username"
security delete-internet-password -s github.com -a "wrong-username"
gh auth login                      # 재인증

6.2 같은 organization을 두 계정이 접근

회사 GitHub의 org에 개인 계정도 collaborator로 들어가 있는 경우. clone URL이 같아서 어떤 키가 쓰일지 모호:

# 명시 별칭 사용
git clone git@github-work:org/shared-repo.git work-side
git clone git@github-personal:org/shared-repo.git personal-side

별도 디렉토리로 두면 user.email · 키 모두 분리.

6.3 macOS Keychain에서 passphrase 1회 입력 후 잊혀짐

UseKeychain yes~/.ssh/config에 있어도 macOS 15+ 일부 환경에서 무시. 해결:

# ssh-agent를 명시적으로 시작 + 키 등록
eval "$(ssh-agent -s)"
ssh-add --apple-use-keychain ~/.ssh/id_ed25519_work

.zshrc에 자동 등록 코드 추가 가능 (필요하면).


7. 보안 체크리스트

  • 개인키는 절대 공유 안 함pub 만 등록
  • passphrase 사용 — 키 파일 자체 암호화. macOS Keychain이 1회 입력 후 캐시
  • 회사·개인 키 분리 — 한 키 노출 시 영향 범위 격리
  • ~/.ssh/config 권한chmod 600 ~/.ssh/config
  • ~/.ssh 디렉토리 권한chmod 700 ~/.ssh
  • Hardware key (선택) — YubiKey + FIDO2 SSH 사용 시 ssh-keygen -t ed25519-sk 별도 가이드

8. 트러블슈팅

Permission denied (publickey) — 명시 키인데 거부

IdentitiesOnly yes가 없으면 ssh-agent의 다른 키가 먼저 시도되어 거부됨. config에 추가.

ssh -vT git@github-work 2>&1 | grep -E "(Offering|identity file)"

여러 키를 시도하면 IdentitiesOnly yes 누락.

잘못된 계정으로 인사

ssh -T git@github-work가 personal 계정으로 인사. 원인:

  • ssh-agent의 잘못된 키가 우선 사용
  • ~/.ssh/configHost github-work 블록 순서 또는 IdentityFile 경로 오타
ssh-add -L                          # agent에 어떤 키들이 있는지
ssh-add -D                          # 모두 제거 후
ssh-add --apple-use-keychain ~/.ssh/id_ed25519_work
ssh -T git@github-work

git push가 personal 계정으로

URL이 git@github.com:org/repo.git로 되어있고 ssh-agent의 personal 키가 우선. URL을 git@github-work:...로 변경.

git remote set-url origin git@github-work:org/repo.git

includeIf가 작동 안 함

  • 경로 끝에 trailing slash 빠짐 (~/work X, ~/work/ O)
  • 폴더 경로가 symlink — gitdir 대신 gitdir/i(case-insensitive)는 그대로지만 symlink 해석은 OS 따라 다름. 실제 경로 사용 권장.

gh auth switch가 안 보임

gh 2.40 미만. 업그레이드:

brew upgrade gh
gh --version

다음 단계

참고 링크

변경 이력

  • 2026-05-12 — 초안 (devAlice M3 시드 확장)

댓글