[git]ssh로 github사용하기

코드깎는 노인·2021년 8월 4일
0
post-thumbnail

github정책변경으로 8/31이후 비밀번호로 github를 사용할 수 없다.
공개키와 개인키를 생성 후 공개키를 github에 등록해야 한다.

step1

$ cd ~
$ mkdir ~/.ssh
$ cd .ssh
$ ssh-keygen -t rsa -b 4096 -C "example@example.com" // passphrase(키 비밀번호)입력메세지 입력
$ chmod u=rw,g=,o= id_rsa

.ssh폴더에 공개키/개인키를 생성한다.파일권한은 유저 rw권한만 있어야 한다.

step2

# macOS
$ pbcopy < ~/.ssh/id_rsa.pub

# Windows
$ clip < ~/.ssh/id_rsa.pub

공개키 복사

step3


setting => SSH and GPC keys 에 공개키를 등록한다.

step4

Host github.com
  HostName github.com
  IdentityFile ~/.ssh/id_rsa_Username
  User git

~/.ssh에 config파일 생성하여 github.com에서 어떤키를 사용할것인지 정의한다.

step5

ssh -T git@github.com

ssh연결 테스트

step6

ssh-add ~/.ssh/id_rsa
ssh-add -l
ssh-add -D ~/.ssh/id_rsa

step7

git clone <ssh path>

ssh 경로로 클론한다.

다중사용자

#userOne account
Host github.com-userOne
	HostName github.com
	User git
	IdentityFile ~/.ssh/id_rsa_userOne

#userTwo account
Host github.com-userTwo
	HostName github.com
	User git
	IdentityFile ~/.ssh/id_rsa_userTwo
git clone git@github.com:personal_account_name/repo_name.git

git remote set-url origin-user1 git@github.com-worker_user1:worker_user1/repo_name.git

git remote set-url origin-user2 git@github.com-worker_user2:worker_user1/repo_name.git

하나의 호스트(ec2인스턴스등)에서 다중사용자가 ssh키로 git을 이용한다면 ~/.ssh/config파일에 다음과 같이 2개의 파일 모두 등록하여 사용한다. 주의할 점은 커밋시 어떤사용자로 설정되있는지 확인하여야 한다는 것인데 git config --list명령어로 user,email을 확인후 본인이 아니라면 git config user.name "username",git config user.email "useremail" 로 이름,메일설정을 해줘야 한다.
또한 remote를 각 유저별 별도로 설정해야 푸시시 해당 유저의 커밋으로 등록된다.

다중호스트에서 동일 키 사용

개인키를 복사하여 step4부터 진행하면 된다.

출처

https://gist.github.com/oanhnn/80a89405ab9023894df7
https://gist.github.com/jexchan/2351996

profile
내가 볼려고 만든 블로그

0개의 댓글