How to Use Deploy Keys on Github
Introduction
Deploy keys are a feature of GitHub that grants read or write access to a specific repository. They allow external services (like servers, development environments, or automation tools) to interact with your repository without the need to share your personal credentials. In this article, we’ll walk you through setting up deploy keys for your GitHub repositories and discuss some best practices for using them.
Step 1: Generate an SSH Key Pair
First, you’ll need to generate an SSH key pair on your machine or the external service that requires access to your repository. If you already have an SSH key and want to use it as a deploy key, you can skip this step.
On your terminal (Linux/Mac) or Git Bash (Windows), run the following command:
“`
ssh-keygen -t rsa -b 4096 -C “[email protected]”
“`
This command generates a new 4096-bit RSA key pair with your email address as the comment. You’ll be prompted to enter a file path to save the key pair and choose a passphrase (optional but recommended). The output will be two files: the private key (`id_rsa`) and the public key (`id_rsa.pub`).
Step 2: Add the Public Key as a Deploy Key in GitHub
Next, you’ll need to add the generated public key as a deploy key for your GitHub repository. Follow these simple steps:
1. Go to the main page of your GitHub repository and click on “Settings.”
2. Scroll down to the “Deploy Keys” section and click on “Add Deploy Key.”
3. Fill in the necessary information in the form:
– Give your deploy key a descriptive title.
– Copy and paste the contents of your `id_rsa.pub` file into the “Key” field.
– Optionally, you can grant write access to the repository by checking the “Allow write access” box (only advisable for trusted services).
4. Click on “Add Key” to save your deploy key.
Step 3: Configure Your External Service or Application
Now that you’ve added the deploy key to your GitHub repository, you need to configure your external service or application to use the private key (`id_rsa`) for authentication. Remember, never share your private key; it should be securely stored on the machine or service requiring access to your repository.
Depending on the service or application you’re using, configuration steps may vary. Consult the relevant documentation to set up SSH authentication with your private key.
Best Practices for Using Deploy Keys
1. One Deploy Key Per Repository: Though it’s possible to reuse a single deploy key across multiple repositories, consider generating a unique key pair for each repository. It helps limit exposure if a key becomes compromised.
2. Grant Minimal Permissions: Only grant write access if strictly necessary; otherwise, leave it with read-only access by default.
3. Regularly Rotate Keys: Periodically rotate your deploy keys in case they are accidentally leaked or compromised.
Conclusion
Using deploy keys on GitHub is a powerful way to provide limited and secure access to your repositories. By following this guide and adhering to best practices, you can seamlessly integrate external services and applications without compromising security.