Troubleshooting Deploy Key Issues with Github Multiple Repositories on the Same Host
I have multiple sites running on the same server, corresponding to multiple private Github repositories. I use a deploy key for packaged deployment. Unfortunately, Github requires a unique key for each repository. If I use the default key on the server, I often get an error message:
1~ git clone git@github.com:kmephistoh/xxxx.git
2Cloning into 'xxxx'...
3ERROR: Repository not found.
4fatal: Could not read from remote repository.
5
6Please make sure you have the correct access rights
7and the repository exists.
To deploy multiple repositories using a deploy key on the same host
First, create multiple keys
For example, use ssh-keygen to create multiple key pairs. Place the corresponding public keys (xxx.pub files) into GitHub's deploy keys. Don't complain about not being able to find them...
1➜ ssh-keygen
2Generating public/private RSA key pair.
3Enter file to save the key (/home/mephisto/.ssh/id_rsa): keyone
4Enter passphrase (empty for no passphrase):
5Enter same passphrase again:
6Your identification has been saved in keyone
7Your public key has been saved in keyone.pub
8The key fingerprint is:
9SHA256:EMDob/6SzsSOUGIdEqEUYLeSAIdJczUbl6HeLDUnthY mephisto@workpc
10The key's randomart image is:
11+---[RSA 3072]----+
12|B@+=o=.oo |
13|B.B o *o |
14|.= o o.E . |
15| = o =.* |
16|...o o =S |
17|.o .o o |
18|.oo.|
19| . =+ |
20| ..+o. |
21+----[SHA256]-----+
22
23➜ ls -al keyone*
24-rw------- 1 mephisto mephisto 2602 Nov 4 18:19 keyone
25-rw-r--r-- 1 mephisto mephisto 569 Nov 4 18:19 keyone.pub
Use the same method to create another key, but I won't demonstrate this here.
Next, configure the ~/.ssh/config file.
Example:
1Host github.com-repo-1
2Hostname github.com
3IdentityFile=/home/user/.ssh/repo-1_deploy_key
4
5Host github.com-repo-2
6Hostname github.com
7IdentityFile=/home/user/.ssh/repo-2_deploy_key
- Host github.com-repo-1 # Repository alias (This alias is crucial)
- Hostname github.com # GitHub domain name
- IdentityFile=/home/user/.ssh/repo-1_deploy_key # Private key used by the repository
Finally, use the alias to pull code.
Note that github.com-repo-1 This is the alias above. Look closely, the string between @ and : is replaced. GitHub is quite clever. Here's an example:
1$ git clone git@github.com-repo-1:OWNER/repo-1.git
With this setup, you can deploy code normally. Official documentationhttps://docs.github.com/en/developers/overview/managing-deploy-keys
I want to use one key pair for everything. What should I do? In theory, I could put the key pair from my main computer on the server and use it just like I would on my local computer. However, this carries security risks, so I'll stick to the official documentation. Convenience and security are always a bit of a trade-off, unless you don't mind.
Copyright statement:
- All content that is not sourced is original., please do not reprint without authorization (because the typesetting is often disordered after reprinting, the content is uncontrollable, and cannot be continuously updated, etc.);
- For non-profit purposes, to deduce any content of this blog, please give the relevant webpage address of this site in the form of 'source of original text' or 'reference link' (for the convenience of readers).