2021年6月3日 星期四

[github, deploy key] Deploy ssh key to the github repository but get permission denied

Deploy ssh key to the github repository but get permission denied

井民全, Jing, mqjing@gmail.com


1. Weird Situation 

To deploy a public ssh key to GitHub for the management of your source repository is quite easy. However, did you ever get a weird situation that everything was set done, but you cannot pull the code? Whatever, you check and check, re-generate the key, again and again, you always get the permission denied message, like this.

 Fig. The permission denied message after git clone command.

1.1. Root Cause

  1. When you copy the public/private key from the other computer, the permission of the key changed. That caused the issue.


1.2. Solution

  1. Set the correct permission to the keys.

    1. Private key: 600

    2. Public key: 660

1.3. Reference

The .ssh directory permissions should be 700 (drwx------).  The public key (.pub file) should be 644 (-rw-r--r--). The private key (id_rsa) on the client host, and the authorized_keys file on the server, should be 600 (-rw-------). If you don't follow this, you will get this.



2. Detail

2.1. Deploy key to your repository

Step 1: GitHub, [Your project] -> [Settings] -> [Deploy keys]


Fig. The repository settings.


Fig. The Deploy keys function.


Step 2: Deploy key


  1. Generate your public/private key pair

Command: ssh-keygen -t rsa

Fig. Generated key-pair.




  1. Add new deploy key: Paste your public key to the github

cat .ssh/id_rsa.pub

Fig. Add new deploy key.


2.2. Note

2.2.1. Error in SSH directory permissions

1, The .ssh directory permissions should be 700 (drwx------).  The public key (.pub file) should be 644 (-rw-r--r--). The private key (id_rsa) on the client host, and the authorized_keys file on the server, should be 600 (-rw-------). If you don't follow this, you will get this.

Fig. Permission denied if the private key permission is not 600.




3. Reference

  1. https://community.perforce.com/s/article/6210#:~:text=ssh%20directory%20permissions%20should%20be,%2D%2D%2D%2D%2D).