Home SSH key로 깃허브 계정 여러개 사용하기
Post
Cancel

SSH key로 깃허브 계정 여러개 사용하기

깃헙 계정을 여러개 사용하면서 SSH key 설정의 필요성을 느끼게 됐다. 설정해 본 과정을 기록해두려고 한다.

SSH란?

SSH는 Secure Shell의 줄임말로, 원격 호스트에 접속하기 위해 사용되는 보안 프로토콜이다. AWS와 같은 클라우드 서비스에서 제공하는 서버는 원격 접속을 해서 사용하기 때문에 SSH 설정이 필요하다.

SSH key가 있는지 확인하기

터미널을 열고 아래 명령어를 실행하면 현재 있는 SSH key를 확인할 수 있다.

1
2
$ ls -al ~/.ssh
# Lists the files in your .ssh directory, if they exist

아래 SSH key가 존재하면 GitHub에서 바로 사용할 수 있다.

  • id_rsa.pub
  • id_ecdsa.pub
  • id_ed25519.pub

SSH key 생성하기

SSH key가 없다면 아래 명령어를 통해서 생성할 수 있다. 파일명을 알아보기 쉽게 계정 이름으로 생성해주었다. passphrase 를 입력하라고 나오는데 따로 입력하지 않아도 생성할 수 있다.(엔터 누르면 됨)

1
2
3
4
5
6
$ ssh-keygen -t ed25519 -C "your_email@example.com" -f "user_name"
$ ssh-keygen -t ed25519 -C "your_second_email@example.com" -f "second_user_name"

# ed25519 암호화 방식을 지원하지 않는 다면 rsa 방식을 사용해서 생성할 수도 있음
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -f "user_name"
$ ssh-keygen -t rsa -b 4096 -C "your_second_email@example.com" -f "second_user_name"
  • 간단한 옵션 설명
    • -b: 생성할 키 비트수 지정. 디폴트 값으로 2048 비트로 설정되어 있음
    • -t: 어떠한 암호화 방식을 사용 할 것인지를 지정
    • -C: 주석 입력 가능
    • -f: 저장할 파일명 지정. 경로 지정 가능

깃헙 계정에 SSH key 연결하기

SSH 공개키를 클립보드에 복사한다.

1
2
# Copies the contents of the user_name.pub file to your clipboard
$ pbcopy < ~/.ssh/user_name.pub

깃헙 세팅 페이지에 들어가서 New SSH Key 생성 버튼을 누른다. Github Settings New SSH Key

Title 필드에는 어디에서 사용하는 SSH 키인지 명시해두면 나중에 쉽게 확인할 수 있다. Key 유형은 인증키로 설정해주고 복사된 키를 붙여 넣어주고 Add SSH key 버튼을 눌러서 등록하면 된다. Add SSH key

ssh-agent에 SSH key 추가하기

백그라운드에서 ssh-agent를 실행해준다.

1
2
3
$ eval "$(ssh-agent -s)"
# 출력 결과
> Agent pid 59566

SSH 관련 환경 설정을 해준다. config 파일이 있으면 수정해주고 없으면 생성하면 된다.

1
2
3
4
5
6
7
8
9
# config 파일 있는지 확인
$ ls -al ~/.ssh

# config 파일이 있는 경우
$ open ~/.ssh/config

# config 파일이 없는 경우
$ touch ~/.ssh/config
$ open ~/.ssh/config

기본 계정을 설정해놓고 두번째 계정을 아래와 같이 설정해주었다. IdentityFile에 SSH key 파일 경로를 넣어주면 된다.

1
2
3
4
5
6
7
8
9
10
11
12
# Default Account
Host *.github.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/user_name

# Second Account
Host github-second
  HostName github.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/second_user_name

아래 명령어를 사용해서 SSH key를 추가해준다.

1
2
3
$ ssh-add --apple-use-keychain ~/.ssh/user_name
# 키체인 옵션(UseKeychain)을 추가하지 않았을 경우
$ ssh-add ~/.ssh/user_name

아래 명령어로 SSH 연결을 테스트 해볼 수 있다. github.com 호스트의 신뢰성을 확인할 수 없습니다. 연결하시겠습니까? 라는 문구가 나오는데 연결을 진행하면 된다.

1
2
3
4
5
# Default Account
$ ssh -T git@github.com

# Second Account
$ ssh -T git@github-second

지금까지 잘 설정됐다면 아래 메시지가 출력된다.

1
Hi UserName! You've successfully authenticated, but GitHub does not provide shell access.

이제 설정한 Host를 사용해서 깃헙 저장소를 클론할 수 있다. 로컬 저장소에서 따로 이메일과 이름을 미리 추가해두면 편하다.

1
2
3
4
5
6
7
8
9
# Use Default Account
$ git git@github.com:UserName/repository.git

# Use Second Account
$ git git@github-second:SecondUserName/repository.git

# Additional Info
$ git config user.email "your_email@example.com"
$ git config user.name  "User Name"

이미 있는 로컬 저장소를 변경하려면 remote URL을 변경해주면 된다.

1
2
3
4
5
6
7
8
9
10
11
# 현재 remote URL 확인
$ git remote -v

# remote URL 변경
$ git remote set-url origin git@github.com:UserName/repository.git

# 확인
$ git remote -v
# should show:
origin  git@github.com:UserName/repository.git (fetch)
origin  git@github.com:UserName/repository.git (push)

참고문서

This post is licensed under CC BY 4.0 by the author.

-

-

Trending Tags