SSH Keys — Secure Passwordless Authentication in Linux¶
SSH keys provide a secure and convenient way to authenticate users without using passwords. Instead of transmitting passwords over the network, SSH uses public-key cryptography to verify identities. SSH key authentication is the industry standard for Linux administration, cloud computing, DevOps, Git repositories, CI/CD pipelines, and enterprise infrastructure.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand SSH key authentication
- Differentiate public and private keys
- Generate SSH key pairs
- Configure passwordless login
- Use SSH Agent
- Secure SSH keys
- Authenticate with GitHub and GitLab
- Apply SSH security best practices
Prerequisites¶
Complete:
- Module 1 – Linux Fundamentals
- Module 2 – Linux Command Line Essentials
- Module 3 – Text Processing
- Module 4 – File Management and Permissions
- Module 5 Lessons 1–7
Why Learn SSH Keys?¶
Imagine you manage:
- 50 Linux servers
- AWS EC2 instances
- Azure Virtual Machines
- Google Cloud VMs
- Kubernetes nodes
- GitHub repositories
Typing passwords hundreds of times every day is:
- Slow
- Inconvenient
- Less secure
SSH keys solve this problem.
What is an SSH Key?¶
SSH authentication uses two keys:
and
Authentication succeeds only when the two keys match.
How SSH Authentication Works¶
Your Laptop
──────────────
Private Key
id_ed25519
│
│ Authentication Request
▼
Linux Server
────────────────────
authorized_keys
Contains Public Key
If the public key matches your private key, access is granted.
Public Key vs Private Key¶
| Public Key | Private Key |
|---|---|
| Safe to share | Never share |
| Stored on servers | Stored on your computer |
| Used for verification | Used for authentication |
| Can be copied | Must remain secret |
Supported Key Types¶
Modern SSH supports:
- Ed25519 (recommended)
- RSA
- ECDSA
For new deployments, prefer Ed25519 because it provides strong security with shorter keys and faster operations.
Generate an SSH Key Pair¶
Recommended:
For compatibility with older systems:
Generation Process¶
Example:
Press Enter to use the default location.
A passphrase is recommended for better security.
Default Key Location¶
Contents:
or
Understanding the Files¶
Private key:
Never share.
Public key:
Safe to copy to servers.
View Your Public Key¶
Example:
Copy Public Key to a Server¶
Example:
This automatically adds your public key to:
Manual Installation¶
If ssh-copy-id is unavailable:
Create the directory.
Append your public key.
Set permissions.
Connect Without a Password¶
If configured correctly:
No password is required.
SSH Agent¶
SSH Agent securely stores decrypted private keys in memory.
Start the agent.
Add your key.
List loaded keys.
SSH Configuration¶
Configuration file:
Example:
Connect using:
GitHub Authentication¶
Generate a key.
Copy the public key.
Add it to your GitHub account.
Test:
GitLab Authentication¶
The same SSH key can be added to GitLab.
Test:
SSH Permissions¶
Secure permissions are critical.
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
chmod 600 ~/.ssh/authorized_keys
Incorrect permissions may prevent SSH authentication.
Common Commands¶
Generate key.
Copy key.
Start SSH Agent.
Load key.
Test connection.
Real Production Examples¶
GitHub authentication.
GitLab authentication.
Production server.
AWS EC2.
Google Cloud VM.
Azure VM.
Production Perspective¶
SSH keys are widely used in:
- Linux servers
- Cloud virtual machines
- GitHub
- GitLab
- Bitbucket
- CI/CD pipelines
- Kubernetes administration
- Bastion hosts
- Infrastructure automation
Password-based authentication is often disabled in enterprise environments in favor of SSH keys.
Hands-on Lab¶
Task 1¶
Generate an Ed25519 key pair.
Task 2¶
List the SSH directory.
Task 3¶
View the public key.
Task 4¶
Start the SSH Agent.
Task 5¶
Add the private key.
Task 6¶
List loaded keys.
Task 7¶
Create an SSH configuration file.
Task 8¶
Verify file permissions.
Command Deep Dive¶
| Command | Purpose | Production Example |
|---|---|---|
ssh-keygen | Generate SSH keys | Authentication |
ssh-copy-id | Install public key | Server access |
ssh-agent | Manage private keys | Passwordless workflows |
ssh-add | Load keys into the agent | Git authentication |
ssh | Connect to remote hosts | Administration |
Production Troubleshooting Scenario¶
Scenario
A DevOps engineer cannot log in using SSH keys.
Error:
Investigation:
Findings:
The .ssh directory permissions are:
SSH rejects insecure permissions.
Solution:
Retry:
Authentication succeeds.
Best Practices¶
- Prefer Ed25519 keys for new systems.
- Protect private keys with a strong passphrase.
- Never share your private key.
- Use SSH Agent to avoid repeated passphrase prompts.
- Disable password authentication on production servers where appropriate.
- Regularly rotate SSH keys.
- Remove unused public keys from servers.
Common Mistakes¶
❌ Uploading the private key instead of the public key.
✅ Prefer the public key rather than uploading the private key.
❌ Setting incorrect permissions on the .ssh directory.
✅ Avoid this mistake: setting incorrect permissions on the .ssh directory.
❌ Storing private keys in shared repositories.
✅ Avoid this mistake: storing private keys in shared repositories.
❌ Using the same SSH key for personal and production environments.
✅ Avoid using the same SSH key for personal and production environments when a safer approach exists.
Interview Questions¶
Beginner¶
- What is an SSH key?
- What is the difference between a public key and a private key?
- Which command generates an SSH key pair?
- What is
authorized_keys?
Intermediate¶
- Why is Ed25519 recommended over RSA for new deployments?
- What is the purpose of SSH Agent?
- How do you configure passwordless SSH?
- Why are file permissions important for SSH authentication?
Architect Level¶
- How would you manage SSH keys across thousands of Linux servers?
- Why is key-based authentication preferred over passwords?
- How would you secure SSH access for cloud infrastructure and CI/CD pipelines?
Summary¶
In this lesson, you learned:
- SSH key authentication
- Public and private keys
- Generating key pairs
- Passwordless SSH
- SSH Agent
- SSH configuration
- GitHub and GitLab authentication
- Production security best practices
SSH keys are the industry standard for secure remote access. They eliminate the need for passwords, improve security, support automation, and are widely used in Linux administration, cloud platforms, Git repositories, and enterprise infrastructure.
Key Takeaways¶
- SSH uses public-key cryptography for authentication.
- Never share your private key.
- Use
ssh-keygento generate key pairs. - Use
ssh-copy-idto install public keys on remote servers. - Protect the
.sshdirectory with the correct permissions. - Prefer Ed25519 keys for new deployments.
What's Next?¶
PAM (Pluggable Authentication Modules) — Understanding Linux Authentication
You'll explore:
- What PAM is
- How Linux authentication works
- PAM configuration files
- Authentication modules
- Password policies
- Multi-factor authentication (MFA)
- Enterprise authentication workflows
Understanding PAM will help you see how Linux authentication is centralized and how enterprise systems enforce consistent security policies.