본문으로 바로가기

Git clone 에러 Permission denied (publickey)

category 서버&시스템/Linux 2023. 9. 15. 14:23

 

에러 메시지

git clone 시도 시 아래처럼 실패 메시지 발생.

접속을 시도하고 있는 현재 시스템의 공개키(publickey) 를 읽는데 실패하였다는 내용이다.

 

$ git clone git@github.com:xxxxxxxxxxxx.git
Cloning into 'xxxxxxxxxxxx'...
The authenticity of host 'github.com (20.200.245.247)' can't be established.
ECDSA key fingerprint is SHA256:p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,20.200.245.247' (ECDSA) to the list of known hosts.
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

 

점검 방법

git 과 무관하게 ssh 접속 자체가 안되는 것인지 확인.

 

$ ssh -T git@github.com
Hi xxx! You've successfully authenticated, but GitHub does not provide shell access. # 정상
Permission denied (publickey). # 실패

 

git ls-remote 명령을 통해 저장소 정보를 불러올 수 있는지 확인.

 

$ git ls-remote git@github.com:xxxxxxxxxxxx.git HEAD
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

 

해결 방법

해결 방법은 당연히 공개키를 생성하는 것이다.

git 계정을 확인해 두고 절차를 진행하자.

 

$ git config user.name
xxx
$ git config user.email
xxx@gmail.com

 

git 공식 문서에서는 OS 가 Ed25519 알고리즘을 지원하지 않는 경우 RSA 를 사용하라고 되어 있다.

명령어 후에 전부 엔터 입력.

 

$ ssh-keygen -t rsa -b 4096 -C xxx@gmail.com
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:yZt37D013vBqRqxghzbgRmgh21gEF7Kg9NeAtk6JLwQ xxx@gmail.com
The key's randomart image is:
+---[RSA 4096]----+
| .. +++.         |
|E..+.+=          |
|o o.+B +         |
| o ++ +.o.       |
|. +  . oS. . .   |
| . o    oo*.. +..|
|  .    .oo.+oo.+o|
|         . o..+.o|
|            .ooo |
+----[SHA256]-----+

 

생성된 id_rsa.pub 파일의 텍스트를 전부 복사하여 본인의 git 계정으로 로그인된 페이지에 붙여넣어야 한다.

 

$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQCUQqH+YUF3mFIwVhxA...(생략)...

 

Settings - SSH and GPG keys 메뉴에서 New SSH key 버튼을 클릭, 제목(아무거나)과 함께 입력 후 저장한다.

 

 

다시 터미널로 돌아와서 확인해 보면 정상 접속된다.

 

$ ssh -T git@github.com
Hi xxx! You've successfully authenticated, but GitHub does not provide shell access.