Generate new SSH Key Pair
Categories:
3 minute read
The following steps should allow you to create a new SSH key pair, add it to the ssh-agent.
Step 1: Checking for Existing SSH Keys
Before we start creating a new SSH key, let’s check if you have any existing SSH keys. Open PowerShell and type:
ls ~/.ssh/id_*
If you see “No such file or directory” message, then you can proceed to Step 2. If not, you can either use the existing key or create a new one, maybe the existing one has expired.
Step 2: Creating Your SSH Keys
To generate a new SSH key pair, enter the following command in PowerShell:
ssh-keygen -t ed25519 -C "your_email@example.com"
Here, replace “your_email@example.com” with your email address. We’re using the Ed25519 algorithm here, as it’s the most modern and secure.
When asked to “Enter file in which to save the key”, you can type a full path with the filename you want to use, maybe you want to create multiple keys and name them per service for example:
~/.ssh/github_ed25519
Next, you’ll be asked to enter a passphrase. This provides an additional layer of security but is optional. You can press Enter to skip this, or enter a secure passphrase and confirm it by entering it again when prompted.
Step 3: Adding Your SSH Key to the ssh-agent
First, start the ssh-agent in the background:
Start-Service ssh-agent
Next, add your private key to the ssh-agent:
ssh-agent | Invoke-Expression
ssh-add ~/.ssh/github_ed25519
Adding Your SSH Key to Your GitHub Account
First, we need to copy your public key to the clipboard. Run the following command in PowerShell:
cat ~/.ssh/id_ed25519.pub | clip
Now, you can add this key to GitHub:
- In your GitHub account, go to your avatar or profile photo, then click Settings.
- In the user settings sidebar, click SSH and GPG keys.
- Click New SSH key or Add SSH key.
- In the “Title” field, add a descriptive label for the new key. This could be something like “Work PC Windows 11”.
- Paste your key into the “Key” field.
- Click Add SSH key.
Step 5: Testing Your SSH Connection
You can check that everything is working by trying to SSH to GitHub. In PowerShell, type:
ssh -T git@github.com
You should get a message like:
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
This means everything is working correctly.
If you see a message about “access denied,” then there may be an issue with your SSH key setup, the way your key was added to GitHub, or the way your SSH config is set up. If you encounter any issues, don’t hesitate to ask for help.