How to setup SSH between your local machine and GitHub

How to setup SSH between your local machine and GitHub
Photo by rc.xyz NFT gallery / Unsplash

Step-by-Step Explanation and Guide: Setting Up SSH Keys on Your System

1. Introduction to SSH

SSH (Secure Shell) is a cryptographic protocol used for secure remote login and
other secure network services. It is widely used for connecting to servers,
especially in DevOps and software development environments. SSH keys provide an
additional layer of security beyond traditional password authentication.

2. Creating the .ssh Directory

  • Objective: Create a hidden directory called .ssh in your home folder to
    store your SSH keys.
  • Explanation: This command creates a new directory named .ssh in your home
    directory. The tilde ~ represents your user's home directory.

Command:

mkdir ~/.ssh/

3. Setting Proper Permissions for the .ssh Directory

  • Objective: Ensure the .ssh directory has secure permissions, allowing
    only the owner to read, write, and execute files within it.
  • Explanation: The chmod 700 command sets the file permissions so that only
    the owner (you) can access the .ssh directory. This is a security measure to
    prevent unauthorized access.

Command:

chmod 700 ~/.ssh/

4. Generating SSH Keys

  • Objective: Generate an SSH key pair (public and private keys) using the
    Ed25519 algorithm, which is recommended for its cryptographic strength.
  • Explanation: This command generates a new SSH key pair. You will be
    prompted to enter a passphrase (a password for your private key) and to specify
    the location of the keys. If you do not provide a location, the keys will be
    saved in the default .ssh directory.

Command:

ssh-keygen -t ed25519

5. Securing Your Private Key

  • Objective: Ensure the private key has appropriate permissions to prevent
    unauthorized access.
  • Explanation: This command sets the permissions for your private key file
    (id_ed25519) so that only you can read and write to it. This is crucial for
    maintaining security.

Command:

chmod 600 ~/.ssh/id_ed25519

6. Displaying Your Public Key

  • Objective: View or copy your public key for addition to your GitHub
    account.
  • Explanation: The cat command displays the contents of the file. This will
    show your public key, which you can then copy and paste into your GitHub account
    settings.

Command:

cat ~/.ssh/id_ed25519.pub

7. Adding Your Public Key to GitHub

  • Objective: Add your public SSH key to your GitHub account to enable SSH
    authentication.
  • Steps:
    1. Go to your GitHub profile.
    2. Navigate to Settings > SSH and GPG Keys.
    3. Click on Add SSH Key.
    4. Provide a title for your key (e.g., "My Laptop").
    5. Paste your public key into the text box.
    6. Click Add Key.

8. Verifying SSH Connection to GitHub

  • Objective: Test if your SSH setup works correctly by connecting to GitHub.
  • Explanation: This command attempts a test connection to GitHub using SSH.
    If successful, you will see a welcome message and be authenticated without
    needing to enter a password.

Command:

ssh -T git@github.com

9. Cloning a Repository Using SSH

  • Objective: Clone a repository from GitHub using your SSH key.
  • Explanation: Replace username with your GitHub username and repository
    with the name of the repository you wish to clone. This command clones the
    repository over SSH, leveraging your private key for authentication.

Command:

git clone git@github.com:username/repository.git

10. Common Troubleshooting Tips

  • Issue: If key generation fails or permissions are incorrect.
    • Ensure you have administrative privileges on your system.
    • Check file permissions using ls -la ~/.ssh/ to verify that only you have
      access to the .ssh directory and keys.
  • Issue: If SSH connection fails after adding the public key.
    • Double-check that the public key was added correctly to GitHub.
    • Ensure the correct passphrase is being used when prompted for the private
      key.
  • Issue: If you encounter a "Permission denied (publickey)" error.
    • Verify that your public key is correctly added to GitHub.
    • Check that you are using the correct SSH port or configure an SSH config file
      if necessary.

11. Conclusion

By following these steps, you have successfully set up SSH keys on your system,
enhancing the security of your connections to remote servers and repositories
like those on GitHub. This method streamlines authentication, reduces the need
for frequent password entries, and provides a more secure alternative to
traditional password-based logins.

12. Additional Considerations

  • Alternative Algorithms: While ed25519 is recommended, you can also
    generate keys using other algorithms like RSA or ECDSA by replacing ed25519
    with -t rsa or -t ecdsa in the ssh-keygen command.
  • Windows Users: If you're on Windows, tools like Git Bash or the Windows
    Subsystem for Linux (WSL) allow you to use these commands natively. Ensure your
    environment supports SSH key generation and management.
  • Backup and Security: Consider backing up your private keys in a secure
    location. Losing access to your private key can lock you out of systems that rely
    on it for authentication.
  • Multiple Devices: If you set up multiple devices with different public
    keys, ensure each is added to GitHub or the relevant service provider. This
    allows you to authenticate from any of your devices.

By adhering to best practices and regularly reviewing your security measures, you
can maintain a robust and secure environment for your development work.