# How to Set Up SSH Keys for GitHub: Step-by-Step Instructions

When working with GitHub, using SSH for authentication is a secure and efficient approach. Recently, I ran into an issue where my GitHub push attempts were met with a frustrating `Permission denied (publickey)` error. If you’re in the same boat, don’t worry! This article walks you through generating and adding an SSH key for GitHub, step by step.

### 1\. Checking the Current SSH Configuration

First, let’s check if there are any existing SSH keys:

```javascript
ls -al ~/.ssh
```

This command lists all the files in the `.ssh` directory. If you see files like `id_rsa` or `id_ed25519`, you likely already have SSH keys set up. In my case, the directory only contained `authorized_keys` and `known_hosts`, meaning no SSH key pair was available.

### 2\. Generating a New SSH Key Pair

Since there was no key, I generated a new SSH key pair using the ED25519 algorithm, which is more secure and faster than RSA:

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

You’ll be prompted for a file location — just press Enter to accept the default (`/home/yourusername/.ssh/id_ed25519`). Then, enter a passphrase for added security (or leave it empty for convenience). Once done, you’ll see output like this:

```javascript
Your identification has been saved in /home/yourusername/.ssh/id_ed25519
Your public key has been saved in /home/yourusername/.ssh/id_ed25519.pub
```

### 3\. Starting the SSH Agent

The SSH agent manages your SSH keys, so you don’t need to enter the passphrase every time. Start it like this:

```javascript
eval "$(ssh-agent -s)"
```

You’ll see a message like `Agent pid 54866`, confirming the agent is running.

### 4\. Adding the SSH Key to the Agent

Now, add the private key to the agent:

```javascript
ssh-add ~/.ssh/id_ed25519
```

If everything’s set up correctly, you won’t see any errors.

### 5\. Adding the SSH Key to GitHub

The next step is to add your **public key** (`id_ed25519.pub`) to GitHub. Display the key with this command:

```javascript
cat ~/.ssh/id_ed25519.pub
```

Copy the output and go to your GitHub account:

1. Go to **Settings** → **SSH and GPG keys**.
    
2. Click **New SSH key**.
    
3. Paste the copied key and give it a name.
    
4. Click **Add SSH key**.
    

### 6\. Testing the SSH Connection

Let’s confirm everything works:

```javascript
ssh -T git@github.com
```

If it’s successful, you’ll see a message like this:

```javascript
Hi your-github-username! You've successfully authenticated.
```

### 7\. Pushing to GitHub

Finally, try pushing your code again:

```javascript
git push -u origin main
```

Everything should work perfectly now! 🎉

---

**Final Thoughts** Setting up SSH for GitHub takes a few steps, but once it’s done, you’ll have a secure and password-free way to manage your repositories. If you run into any snags, double-check your SSH agent, key permissions, and GitHub key settings.

Happy coding! 🚀
