In this article, we’ll look at how to configure a secure SSH connection to GitHub on Unix-like systems (macOS, Linux). Setting this up allows you to push and pull code without constantly entering your username and password.
1. Generate the SSH Keys
First, we need to generate a new SSH key pair. SSH keys are typically stored in the .ssh directory in your user profile.
- Open your terminal and navigate to the SSH directory:
1 | cd ~/.ssh |
- Generate a pair of RSA keys with 4096-bit encryption:
1 | ssh-keygen -t rsa -b 4096 |
Name your key:
The tool will ask you to input a<filename>. It is best to give it a specific name (e.g.,id_rsa_github) to avoid overwriting any existing default keys (likeid_rsa).Warning: If you enter the name of a file that already exists, it will be overwritten. This action cannot be undone!
Set a passphrase:
Next, you will be asked for a passphrase. While you can leave this empty, it is highly recommended to set a passphrase for added security.
Once finished, you will have two new files:
<filename>: Your Private Key. Never share this with anyone.<filename>.pub: Your Public Key. This is what you will share with GitHub.
2. Add the Public Key to GitHub
Now that you have your keys, you need to authorize the public key on your GitHub account.
- Copy the contents of your public key file (
.pub). You can view it usingcat <filename>.pubin your terminal. - Visit the GitHub SSH Keys Settings page.
- Click the green New SSH key button.
- Give the key a name (e.g., “My Laptop”) and paste your public key into the “Key” field.
- Click Add SSH key.
3. Configure Your System
Finally, tell your system to use this specific key whenever you interact with GitHub. You can do this by editing the config file located in your .ssh folder.
Create or edit the file ~/.ssh/config and add the following block though nano, vim or any editor you like:
1 | # GitHub |
What this does:
- Host: Identifies that these rules apply when you connect to
github.com. - IdentityFile: Tells SSH specifically which private key (
<filename>) to use for this connection.
Or you could directly execute:
1 | mkdir -p ~/.ssh && cat <<EOF >> ~/.ssh/config |
You are now ready to clone and push repositories using the SSH URL!
