How to Use Deploy Keys on Github
Open the terminal program on your computer., Log into your server using your terminal program., Enter the command to generate the SSH key., Press .↵ Enter/⏎ Return when prompted to select a location., Create a passphrase., Copy the SSH key contents...
Step-by-Step Guide
-
Step 1: Open the terminal program on your computer.
The terminal program on your computer allows you to remotely access your server.
If you're using a Linux or Mac computer, you'll be using the built-in Terminal program.
If you're using Windows, you'll need to install a program like Cygwin or GitBash.
Linux
- Press Ctrl+Alt+T or search for "terminal." Mac
- You can find the Terminal program in the Utilities folder.
Windows
- You can download Cygwin from cygwin.com, or GitBash from git-scm.com/downloads. -
Step 2: Log into your server using your terminal program.
You'll be generating the deploy key on your server so that it can access your GitHub repository.
To do this, you'll need to log into your server, either remotely through your terminal or locally on the server.
In your terminal program, type ssh username@hostname to log in.
Enter your password if prompted., The following command will create a new key with your GitHub email address as the label: ssh-keygen
-t rsa
-b 4096
-C "[email protected]" An SSH key is a encrypted key pair that authenticates your identity.
In this case, you'll be assigning the key to your GitHub repository, allowing it to identify your server. , This will save the key to the default location, which is the .ssh directory in your User directory., This adds an additional layer of security to your key, as unknown users will need to enter the passphrase before the key will work.
You'll be prompted to confirm the passphrase when creating it. , Once the key has been created, you'll need to add it to your repository.
To do so, you'll need to copy the contents of the key.
The following command will copy the contents of the key to your clipboard:
Linux
- xclip
-sel clip < ~/.ssh/id_rsa.pub.
You may need to run sudo apt-get xclip first.
Windows
- clip < ~/.ssh/id_rsa.pub Mac
- pbcopy < ~/.ssh/id_rsa.pub , Make sure you log in with an account that can access the repository., This will open your GitHub profile page., This will display all of your repositories., This will grant your server access to the repository to automatically deploy builds., This will open your repository settings., This will display the deploy keys that are currently assigned to the repository., A text field for the key will appear., Click the field and press ⌘ Command/Ctrl+V to paste the copied deploy key into the field.
If you want the server to have write access to the repository, check the "Allow write access" box. , This will allow your server to access the repository and deploy builds from it., A "machine user" is an automated user that can access multiple repositories.
This is useful if your server needs access to multiple repositories, since deploy keys only grant access to a single repository.
You can create a new user by clicking the "Sign up" button on the GitHub homepage and following the prompts. , Follow the steps in the first section to generate a key on your server and copy it to your clipboard., You'll be assigning the newly-created key to this user., This will open the account settings for the machine user., This will display the keys currently assigned to the user., This will allow you to enter the SSH key., This will add the SSH key to the machine user's profile, allowing it to access your server., You can find your repositories in the "Repositories" tab on your Profile page., This will display the repository settings., This will allow you to add collaborators to the repository.
By adding your machine user as a collaborator, it will be able to push builds from your repository to your server., The machine user will be given read/write access to the repository. -
Step 3: Enter the command to generate the SSH key.
-
Step 4: Press .↵ Enter/⏎ Return when prompted to select a location.
-
Step 5: Create a passphrase.
-
Step 6: Copy the SSH key contents to your clipboard.
-
Step 7: Log into the GitHub website.
-
Step 8: Click your profile image in the upper-right corner and select "Your profile."
-
Step 9: Click the "Repositories" tab.
-
Step 10: Select the repository you want to add the key to.
-
Step 11: Click the "Settings" tab at the top of the screen.
-
Step 12: Click the "Deploy keys" button in the left menu.
-
Step 13: Click the "Add deploy key" button.
-
Step 14: Paste the copied deploy key into the field.
-
Step 15: Click "Add key" to add your deploy key.
-
Step 16: Create a dedicated GitHub account for the machine user.
-
Step 17: Generate an SSH key on your server.
-
Step 18: Sign into the GitHub website with the new machine user account.
-
Step 19: Click the machine user's profile picture and select "Settings."
-
Step 20: Click the "SSH and GPG keys" option in the left menu.
-
Step 21: Click the "New SSH key" button.
-
Step 22: Paste the key and click "Add SSH key."
-
Step 23: Open the first repository you want to give the machine user access to.
-
Step 24: Click the "Settings" tab on the repository page.
-
Step 25: Click the "Collaborators" option in the left menu.
-
Step 26: Enter the machine user's name and click "Add collaborator."
Detailed Guide
The terminal program on your computer allows you to remotely access your server.
If you're using a Linux or Mac computer, you'll be using the built-in Terminal program.
If you're using Windows, you'll need to install a program like Cygwin or GitBash.
Linux
- Press Ctrl+Alt+T or search for "terminal." Mac
- You can find the Terminal program in the Utilities folder.
Windows
- You can download Cygwin from cygwin.com, or GitBash from git-scm.com/downloads.
You'll be generating the deploy key on your server so that it can access your GitHub repository.
To do this, you'll need to log into your server, either remotely through your terminal or locally on the server.
In your terminal program, type ssh username@hostname to log in.
Enter your password if prompted., The following command will create a new key with your GitHub email address as the label: ssh-keygen
-t rsa
-b 4096
-C "[email protected]" An SSH key is a encrypted key pair that authenticates your identity.
In this case, you'll be assigning the key to your GitHub repository, allowing it to identify your server. , This will save the key to the default location, which is the .ssh directory in your User directory., This adds an additional layer of security to your key, as unknown users will need to enter the passphrase before the key will work.
You'll be prompted to confirm the passphrase when creating it. , Once the key has been created, you'll need to add it to your repository.
To do so, you'll need to copy the contents of the key.
The following command will copy the contents of the key to your clipboard:
Linux
- xclip
-sel clip < ~/.ssh/id_rsa.pub.
You may need to run sudo apt-get xclip first.
Windows
- clip < ~/.ssh/id_rsa.pub Mac
- pbcopy < ~/.ssh/id_rsa.pub , Make sure you log in with an account that can access the repository., This will open your GitHub profile page., This will display all of your repositories., This will grant your server access to the repository to automatically deploy builds., This will open your repository settings., This will display the deploy keys that are currently assigned to the repository., A text field for the key will appear., Click the field and press ⌘ Command/Ctrl+V to paste the copied deploy key into the field.
If you want the server to have write access to the repository, check the "Allow write access" box. , This will allow your server to access the repository and deploy builds from it., A "machine user" is an automated user that can access multiple repositories.
This is useful if your server needs access to multiple repositories, since deploy keys only grant access to a single repository.
You can create a new user by clicking the "Sign up" button on the GitHub homepage and following the prompts. , Follow the steps in the first section to generate a key on your server and copy it to your clipboard., You'll be assigning the newly-created key to this user., This will open the account settings for the machine user., This will display the keys currently assigned to the user., This will allow you to enter the SSH key., This will add the SSH key to the machine user's profile, allowing it to access your server., You can find your repositories in the "Repositories" tab on your Profile page., This will display the repository settings., This will allow you to add collaborators to the repository.
By adding your machine user as a collaborator, it will be able to push builds from your repository to your server., The machine user will be given read/write access to the repository.
About the Author
Brandon Robinson
Brandon Robinson is an experienced writer with over 1 years of expertise in education and learning. Passionate about sharing practical knowledge, Brandon creates easy-to-follow guides that help readers achieve their goals.
Rate This Guide
How helpful was this guide? Click to rate: