Introduction:
If you’re tired of entering your GitHub password every time you pull code from a repository on your Lightsail server, it’s time to enhance your workflow by leveraging SSH keys for authentication. This step-by-step guide will walk you through the process, ensuring a seamless and secure connection to your GitHub repositories.
Step 1: Generate SSH Key
If you haven’t already set up an SSH key pair on your Lightsail server, initiate the process with the following command:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Accept the default file location by pressing Enter and set a secure passphrase when prompted.
Step 2: Add SSH Key to SSH Agent
To ensure smooth authentication, start the SSH agent and add your private key:
eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_rsa
Step 3: Copy SSH Key to Clipboard
For macOS users, copy the public key to your clipboard with the following command:
cat ~/.ssh/id_rsa.pub
Copy the contents of the file.
Step 4: Add SSH Key to GitHub
Navigate to your GitHub account settings, specifically the “SSH and GPG keys” section. Create a new SSH key and paste your public key into the designated field.
Step 5: Test the Connection
Verify that the connection to GitHub is successful by running the following command:
ssh -T git@github.com
A confirmation message should appear, indicating that you’ve authenticated successfully.
Step 6: Update Git Config
Adjust the Git configuration on your Lightsail server to use SSH for GitHub:
git config --global url."git@github.com:".insteadOf "https://github.com/"
This configuration directs Git to use the SSH URL for GitHub rather than the HTTPS URL.
Conclusion:
With these steps completed, you’ve successfully set up SSH key authentication for your GitHub repositories on your Lightsail server. Going forward, Git commands requiring authentication will seamlessly use the SSH key, eliminating the need to repeatedly enter your GitHub password.
Note: Adjust the commands if you’ve used a different SSH key file or path.
Hope this article helped you. If it did do not forget to let me know in the comments. Have a great time.