8/21/25

How to Create SSH Keys on Linux and Connect to GitHub

How to Create SSH Keys on Linux and Connect to GitHub

Setting up SSH keys for GitHub is one of the most secure and convenient ways to authenticate with your repositories. This guide will walk you through creating an SSH key pair on Linux and configuring it for use with GitHub.

What You'll Learn

  • How to generate an SSH key pair on Linux
  • How to add your SSH key to the SSH agent
  • How to add your public key to your GitHub account
  • How to test your SSH connection to GitHub

Prerequisites

  • A Linux system with terminal access
  • A GitHub account
  • Basic familiarity with the command line

Step 1: Check for Existing SSH Keys

Before creating a new SSH key, let's check if you already have one:

📋
ls -la ~/.ssh
[Screenshot placeholder: Terminal showing output of ls -la ~/.ssh command]

If you see files named id_rsa and id_rsa.pub (or id_ed25519 and id_ed25519.pub), you already have SSH keys. You can either use the existing key or create a new one following the steps below.

Step 2: Generate a New SSH Key

We'll use the Ed25519 algorithm for our SSH key, as it's more secure and performant than the older RSA algorithm:

📋
ssh-keygen -t ed25519 -C "your_email@example.com"

Replace your_email@example.com with your actual GitHub email address.

[Screenshot placeholder: Terminal showing ssh-keygen command execution]

When prompted:

  1. Enter file location: Press Enter to accept the default location (~/.ssh/id_ed25519)
  2. Enter passphrase: Choose a strong passphrase for additional security (recommended)
  3. Confirm passphrase: Re-enter the same passphrase
📋 Copy
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/username/.ssh/id_ed25519): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again:

Alternative: Using RSA Keys

If your system doesn't support Ed25519, use RSA with at least 4096 bits:

📋 Copy
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Step 3: Add Your SSH Key to the SSH Agent

First, start the SSH agent in the background:

📋 Copy
eval "$(ssh-agent -s)"
[Screenshot placeholder: Terminal showing SSH agent starting with process ID]

Then add your SSH private key to the SSH agent:

📋 Copy
ssh-add ~/.ssh/id_ed25519

If you used RSA, replace id_ed25519 with id_rsa.

[Screenshot placeholder: Terminal showing successful key addition to SSH agent]

Step 4: Copy Your Public Key

Display and copy your public key to your clipboard:

📋 Copy
cat ~/.ssh/id_ed25519.pub
[Screenshot placeholder: Terminal displaying the public key content]

The output will look similar to this:

📋 Copy
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... your_email@example.com

Select and copy the entire key, including the ssh-ed25519 prefix and your email address.

Step 5: Add Your SSH Key to GitHub

  1. Log in to your GitHub account
  2. Click on your profile picture in the top right corner
  3. Select Settings from the dropdown menu
[Screenshot placeholder: GitHub profile dropdown menu]
  1. In the left sidebar, click SSH and GPG keys
  2. Click New SSH key or Add SSH key
[Screenshot placeholder: GitHub SSH keys settings page]
  1. In the "Title" field, add a descriptive label for your key (e.g., "Personal Linux Laptop")
  2. In the "Key" field, paste your public key
  3. Click Add SSH key
[Screenshot placeholder: GitHub add SSH key form filled out]
  1. If prompted, confirm your GitHub password

Step 6: Test Your SSH Connection

Test your SSH connection to GitHub:

📋
ssh -T git@github.com
[Screenshot placeholder: Terminal showing SSH connection test]

You should see a message like:

📋
Hi username! You've successfully authenticated, but GitHub does not provide shell access.

If this is your first time connecting, you may see a warning about the authenticity of the host. Type yes to continue.

Step 7: Configure Git to Use SSH

If you haven't already, configure Git with your GitHub credentials:

📋
git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"

Using Your SSH Key

Now you can clone repositories using SSH URLs instead of HTTPS:

📋
# SSH URL format
git clone git@github.com:username/repository.git

# Instead of HTTPS
git clone https://github.com/username/repository.git

For existing repositories, you can change the remote URL:

📋
git remote set-url origin git@github.com:username/repository.git

Troubleshooting

Permission Denied Error

If you get a "Permission denied" error, check:

1. Your SSH key is added to the SSH agent:

📋
ssh-add -l

2. Your public key is correctly added to GitHub
3. You're using the correct SSH URL format

SSH Agent Not Running

If the SSH agent isn't running, start it manually:

📋
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

Wrong Permissions on SSH Files

Fix SSH file permissions if needed:

📋
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub

Security Best Practices

  • Use passphrases: Always protect your private key with a strong passphrase
  • Keep private keys private: Never share your private key (id_ed25519) with anyone
  • Regular key rotation: Consider generating new keys periodically
  • Use different keys: Consider using separate SSH keys for different services
  • Monitor key usage: Regularly review your SSH keys in GitHub settings

Conclusion

You've successfully created an SSH key pair and configured it for use with GitHub. This setup provides a secure, password-free way to interact with your GitHub repositories. Remember to keep your private key secure and consider using SSH agent forwarding or SSH config files for more advanced setups.

For more advanced SSH configuration options, check out the SSH config documentation and GitHub's SSH troubleshooting guide.


Need help with Git workflows? Check out our guide on Git best practices for developers.

6/20/25

SSH Key Setup: A Practical Guide for Linux Systems


SSH keys provide a more secure and convenient way to authenticate with remote servers compared to passwords. This guide covers the essential steps to generate, configure, and use SSH keys on Linux systems, with specific notes for Fedora users.

What You'll Need

  • A Linux system (examples use Fedora 42)
  • Terminal access
  • Basic command line familiarity

1. Generating Your SSH Key Pair

Modern systems should use Ed25519 keys for better security and performance:

ssh-keygen -t ed25519 -C "your.email@example.com"

For systems requiring RSA compatibility:

ssh-keygen -t rsa -b 4096 -C "your.email@example.com"

When prompted:

  • File location: Press Enter to use default (~/.ssh/id_ed25519)
  • Passphrase: Use a strong passphrase for security

Fedora Note: SELinux is enabled by default. Your keys will automatically get the correct context, but if you encounter issues:

restorecon -R ~/.ssh

2. Starting the SSH Agent

One-time startup:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

Persistent setup (recommended):

Create a systemd user service for automatic ssh-agent management:

systemctl --user enable --now ssh-agent

Add to your ~/.bashrc or ~/.zshrc:

export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/ssh-agent.socket"

3. Adding Your Public Key to Remote Servers

Copy your public key to a remote server:

ssh-copy-id username@remote-server.com

Or manually:

cat ~/.ssh/id_ed25519.pub | ssh username@remote-server.com "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

4. SSH Configuration for Multiple Keys

Create or edit ~/.ssh/config:

# Work server
Host work-server
    HostName work.company.com
    User myusername
    IdentityFile ~/.ssh/id_work
    Port 2222

# Personal server
Host personal
    HostName personal.example.com
    User admin
    IdentityFile ~/.ssh/id_personal

Now connect simply with:

ssh work-server

5. Security Best Practices

File Permissions

Ensure correct permissions (critical for SSH to work):

chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
chmod 600 ~/.ssh/config
chmod 600 ~/.ssh/authorized_keys

Additional Security

  • Always use passphrases on private keys
  • Regularly rotate keys (annually recommended)
  • Use different keys for different purposes
  • Consider using hardware security keys for critical systems

6. Fedora-Specific Considerations

Firewall Configuration

If running SSH server on Fedora:

sudo firewall-cmd --permanent --add-service=ssh
sudo firewall-cmd --reload

SELinux Context Issues

If you encounter permission denied errors:

sudo setsebool -P ssh_sysadm_login on
restorecon -R ~/.ssh

Troubleshooting Common Issues

Debug SSH connections:

ssh -v username@server.com

Test key authentication:

ssh -o PasswordAuthentication=no username@server.com

Check SSH agent:

ssh-add -l

Quick Reference

Command Purpose
ssh-keygen -t ed25519 Generate new key pair
ssh-add ~/.ssh/keyname Add key to agent
ssh-copy-id user@host Copy public key to server
ssh -v user@host Debug connection issues

Further Reading


This guide covers the essentials for most use cases. SSH key management can be much more complex in enterprise environments with certificate authorities, hardware security modules, and centralized key management systems.