How to Secure Github Access With an SSH Key
Securing GitHub access with an SSH key is a powerful way to enhance the security of your repositories. SSH keys offer a more secure alternative to traditional password-based authentication by establishing a private, encrypted channel between your computer and the GitHub servers. Here’s how to set it up:
Step 1: Check for Existing SSH Keys
Before generating a new SSH key, check if you already have any existing keys that you might want to use.
“`
ls -al ~/.ssh
“`
Look for files named `id_rsa.pub` or `id_ecdsa.pub`. If you see such files, skip to Step 4. If not, proceed to step 2.
Step 2: Generate a New SSH Key
To create a new SSH key, run the following command, replacing “[email protected]” with your email associated with your GitHub account:
“`
ssh-keygen -t rsa -b 4096 -C “[email protected]”
“`
Press Enter to save the key in the default location, which is `~/.ssh/id_rsa`.
Step 3: Start the ssh-agent
Run the ssh-agent in the background and add your new key:
“`
eval “$(ssh-agent -s)”
ssh-add ~/.ssh/id_rsa
“`
Step 4: Add Your SSH Key to Your GitHub Account
First, copy your SSH key to your clipboard with pbcopy or xclip:
For macOS:
“`
pbcopy < ~/.ssh/id_rsa.pub
“`
For Linux:
“`
xclip -sel clip < ~/.ssh/id_rsa.pub
“`
Next, go to GitHub and navigate to Settings > SSH and GPG keys. Click on “New SSH Key”. Paste your copied key into the “Key” field. Add a descriptive title for the device or location where you’re using the key.
Step 5: Test Your Connection
To test that your setup was successful, attempt to SSH into GitHub:
“`
ssh -T [email protected]
“`
If everything is set up correctly, you will receive a message confirming that you’ve been authenticated.
By following these simple steps, you enhance both convenience and security when accessing your GitHub repositories. Using an SSH key means you won’t need to enter your credentials every time you push or pull from remote repositories, and it protects against certain types of cyber attacks targeting user passwords.