SSH Key Setup Guide for Git and Remote Access
This guide helps you generate and use an SSH key for secure authentication with GitHub and remote servers.
What is an SSH Key, and Why Use It?
SSH (Secure Shell) keys are a secure way to authenticate without using a password. Like GPG keys, SSH uses a key pair:
- Your private key stays on your computer.
- Your public key is shared with remote services (like GitHub or a server).
Why Use SSH Keys?
- Secure logins without typing passwords
- Authenticate Git operations (clone, push, pull) securely
- Access remote servers with one command
GitHub and many servers prefer SSH over HTTPS for better security and ease of use.
1. Generate a New SSH Key
ssh-keygen -t ed25519 -C "[email protected]"
- Use
ed25519
(modern, faster, more secure than RSA) - When prompted, save to the default path:
~/.ssh/id_ed25519
- Optionally set a passphrase for extra protection
2. Add SSH Key to Agent
Start the agent and add your key:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
If using a custom filename, replace with your actual key path.
3. Add SSH Key to GitHub
-
Copy the public key:
cat ~/.ssh/id_ed25519.pub
-
Click "New SSH Key", give it a title, and paste your public key
4. Test GitHub SSH Connection
ssh -T [email protected]
If successful, you'll see a message like:
Hi your-username! You've successfully authenticated.
5. Connect to Remote Server
To SSH into a server:
ssh user@host
Replace user
with your server username and host
with IP or domain name.
6. Copy SSH Key to Server
Quickly add your public key to a remote server:
ssh-copy-id user@host
Adds your key to the server’s
~/.ssh/authorized_keys
file
7. Use SSH Config File for Convenience
Set up shortcuts and default identities.
Edit: ~/.ssh/config
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
Host my-server
HostName example.com
User ubuntu
IdentityFile ~/.ssh/id_ed25519
Now you can connect with:
ssh github.com
ssh my-server
SSH Cheatsheet
Command | Description |
---|---|
ssh-keygen -t ed25519 | Create new SSH key pair |
ssh-add <key> | Add key to SSH agent |
ssh -T [email protected] | Test GitHub SSH auth |
ssh user@host | Connect to remote server |
ssh-copy-id user@host | Install public key on server |
cat ~/.ssh/id_ed25519.pub | View public key content |
~/.ssh/config | Define shortcuts and defaults |