What is the Deploy key?
Deploy key is an SSH key configured in your repository to grant read
or write
access without logging into your GitHub account.
Why should I use it?
Using Deploy Keys can come in handy when you don't want to login to your GitHub account on a machine or just wanted to configure a single repository in that machine.
As the name says, its primary function is to be used in places where read-only
access is needed. Therefore keep the repository safe from the attack, in case the server-side is fallen.
Before we start first, let's create a new Github Repository if you don't have 1 already.
- โ๏ธ Generating SSH Keys
- ๐ฉ Configuring Local Machine
- ๐ฉ Configuring Github Repository
- ๐งช Testing
โ๏ธ Generating SSH Keys
- Open Terminal / Open Git Bash if you are on windows
- Run
ssh-keygen -t ed25519 -C "your_email@example.com"
make sure to replaceyour_email@example.com
with your email which is associated GitHub.com
When you're prompted to "Enter a file in which to save the key," press Enter. This accepts the default file location. You also save it in any location
Enter a file in which to save the key (/xyz/xyz/.ssh/id_ed25519):
At the prompt, type a secure passphrase
> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]
๐ฉ Configuring Local Machine
Add your SSH private key to the ssh-agent. If you created your key with a different name, in a different location, or if you are adding an existing key that has a different name, replace id_rsa in the command with the name of your private key file.
Before adding a new SSH key to the ssh-agent make sure the ssh-agent is running.
eval "$(ssh-agent -s)"
The above cmd will output similar to Agent pid 111931
which means the agent is up & running
ssh-add {your-key-location}
Replace
{your-key-location}
with the actual path for that key file
๐ฉ Configuring Github Repository
Copy public key from the {key-file-name}.pub
file
- Navigate to your repository
- Click
โ๏ธ Settings
- Click
Deploy keys
- Click
Add deploy key
๐งช Testing
To Test if your key is configured correctly run the below cmds
Read Only Test
git clone git@github.com:{username}/{repository}.git
Above cmd should output something similar to below output
Write Test
Navigate Into The Folder
cd your-repository-path
Configure Git identity
git config user.email "your-github-useremail@example.com"
git config user.name "Your Name"
Create A New File
touch test-file.md
Add Some Content To The File
echo "## Hello World" >> test-file.md
Add & Commit The File
git add test-file.md
git commit -m "๐ Successfully Pushed Using Deploy Keys"
Push To Github
git push
๐๐๐ Congrats you have successfully configured the repository with SSH Deploy key ๐๐๐
Questions or feedback? Please comment below.
See all my projects at Github.
Follow me on Twitter for updates