Etc

Github 계정 여러 개를 쓰고 싶어요! (for mac)

iseolin 2023. 2. 22. 11:25

배경

  • Github 계정이 여러 개인 사람이 있는가? (바로 나다)
  • Github 계정 다수를 한 노트북에서 쓰고 싶은데 하는 방법을 헤맨 적이 있는가? (바로 나다)

이런 사람들을 위한 포스팅!

환경 설정

SSH Key 생성하기

우린 Github의 SSH 방식을 사용해 멀티 계정을 사용할 겁니다.

이를 위해 Github 계정 별 SSH Key가 필요합니다. 지금 당장 생성해봅시다.

  1. 먼저 터미널을 켜서 ~/.ssh 로 이동합니다.
mkdir ~/.ssh # ~/.ssh 경로가 없다면 생성
cd ~/.ssh
  1. SSH key를 생성한다.
ssh-keygen -t rsa -C [깃허브 이메일]
  1. 이후 메세지가 나오고
Generating public/private rsa key pair.
  1. 이름을 입력하라는 메세지가 연이어 출력됩니다.
Enter file in which to save the key (/Users/[맥 유저 이름]/.ssh/[ssh키 이름]): # 여기서 이름을 입력하지 않으면 id_rsa로 생성된다.
  1. 비밀번호 설정 문구가 나옵니다. 그냥 엔터 눌러도 됩니다.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
  1. 성공 문구
Your identification has been saved in [ssh키 이름]
Your public key has been saved in [ssh키 이름].pub
The key fingerprint is:
SHA256:sha256sha256sha256sha256 [이메일]
The key's randomart image is:
+---[RSA 4096]----+
|       .+*SO=    |
|         ..oM=.  |
|        o. ==+ . |
|       . Jan. .  |
|      o G o      |
|   o o + o .     |
|  o o = * +      |
|   o.. = G .     |
|   oo .   d      |
+----[SHA256]-----+
  1. 목록보기
    정상적으로 파일 두개가 생성된 걸 볼 수 있습니다.
    (비공개키, 공개키가 한 쌍으로 생성됩니다.)
$ ls
> [ssh키 이름].pub	[ssh키 이름]
# 공개 키, 비공개 키

SSH Key 등록

  1. 터미널 상에서 등록
ssh-add ~/.ssh/[ssh키 이름]
  1. 성공 문구
Identity added: /Users/donghyunjang/.ssh/id_rsa_somjang (jjdh11117@gmail.com)

2-1. 실패문구

Could not open a connection to your authentication agent.

2-2. 해결 (ssh-agent 재실행)

eval ${ssh-agent)
ssh-add ~/.ssh/[ssh키 이름]

SSH config 파일 작성

  1. 파일 생성
vi ~/.ssh/config
  1. 파일 내용 입력

편의상 work 계정 personal 계정을 생성하겠습니다.

#  깃헙계정 work
#  -------------------
Host github.com-work
  HostName github.com
  IdentityFile ~/.ssh/work
  User [github work 계정 이름]

# 깃헙계정 personal
#  -------------------
Host github.com-personal
  HostName github.com
  IdentityFile ~/.ssh/personal
  User [github personal 계정 이름]

! ! host 이름은 기억해야합니다. ! !

Github에 SSH 설정하기

  1. Github Setting 에 들어갑니다.
  2. SSH 설정에 들어갑니다.
  3. 내용 입력
    key 에는
cat [키이름].pub

결과값을 입력합니다.
Title은 원하는 값을 입력합니다.

  1. 완료!
  1. 확인하기
$ ssh -T git@github.com-personal
The authenticity of host 'github.com (15.164.81.167)' can't be established.
RSA key fingerprint is SHA256:sha256sha256sha256sha256sha256sha256sha256
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])?

에서 yse 를 입력합니다.(연결하겠냐는 질문)

성공 문구

Warning: Permanently added 'github.com' (RSA) to the list of known hosts.
Hi SOMJANG! You've successfully authenticated, but GitHub does not provide shell access.

Github Repository 연결하기

의 SSH 을 복사해줍니다.

git@[설정했던 ssh host 이름]:[레포].git
# 예시: git@github.com-personal:[레포].git

처럼 바꿔 origin 연결 또는 clone을 진행합니다.

git config user.name [사용할 유저 이름]
git config user.email [사용할 유저 이메일]

# 확인
git config user.name
git config user.email

축하합니다! 모든 설정이 끝났습니다.

즐거운 개발 되세요!