SSH (Secure Shell) — Secure Remote Access to Linux Systems¶
SSH (Secure Shell) is the standard protocol for securely accessing and managing remote Linux systems over a network. It encrypts all communication between the client and the server, making it the preferred method for remote administration, file transfers, automation, and infrastructure management. Every Linux administrator, DevOps engineer, Cloud Architect, Platform Engineer, Security Engineer, and Site Reliability Engineer (SRE) uses SSH daily.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand SSH
- Connect securely to remote Linux servers
- Configure SSH authentication
- Generate SSH key pairs
- Transfer files using SSH
- Configure SSH client and server
- Use SSH port forwarding
- Secure SSH in production
Prerequisites¶
Complete:
- Module 1 – Linux Fundamentals
- Module 2 – Linux Command Line Essentials
- Module 3 – Text Processing
- Module 4 – File Management
- Module 5 – Users and Groups
- Module 6 – Process Management
- Module 7 – Package Management
- Module 8 Lessons 1–10
Why Learn SSH?¶
Imagine:
- Managing a cloud server on AWS, Azure, or GCP.
- Deploying applications remotely.
- Connecting to Kubernetes nodes.
- Troubleshooting production servers.
- Automating infrastructure tasks.
SSH is the secure method used for all these tasks.
What is SSH?¶
SSH stands for:
It is a cryptographic network protocol that provides secure remote access to systems over untrusted networks.
SSH encrypts:
- Usernames
- Passwords
- Commands
- File transfers
- Session data
How SSH Works¶
All communication is encrypted before it travels across the network.
SSH Architecture¶
SSH Default Port¶
SSH uses:
Verify that SSH is listening.
Basic SSH Connection¶
Connect to a remote server.
Example:
Connect Using a Hostname¶
Connect Using a Different Port¶
If the SSH server listens on port 2222:
First-Time Connection¶
On the first connection, SSH displays the server's fingerprint.
Example:
Type:
The server's public key is then stored in:
SSH Authentication Methods¶
SSH supports:
- Password authentication
- Public key authentication
Public key authentication is recommended for production systems.
Generate an SSH Key Pair¶
Generate an Ed25519 key pair (recommended):
Or generate an RSA key pair:
Default location:
Generated files:
or
Copy Public Key to Server¶
After copying the key, future logins can use key-based authentication.
Login Using a Private Key¶
SSH Configuration File¶
Client configuration:
Example:
Connect using:
SSH Server Configuration¶
Server configuration file:
Common settings:
After modifying the configuration:
Check SSH Service¶
Ubuntu may use:
View SSH Logs¶
Systems using systemd:
Ubuntu systems may use:
Traditional authentication log (distribution-dependent):
SSH Port Forwarding¶
Local port forwarding:
This forwards:
Useful for securely accessing internal services.
Remote Port Forwarding¶
Allows the remote server to access a local service.
Dynamic Port Forwarding¶
Create a SOCKS proxy.
Useful for secure tunneling of network traffic.
Common Commands¶
Connect to a server.
Use a custom port.
Generate SSH keys.
Copy public key.
Check SSH service.
Real Production Examples¶
Access a cloud VM.
Connect to a Kubernetes node.
Access a database server.
Forward a local port.
Production Perspective¶
SSH is used for:
- Cloud administration
- Kubernetes management
- CI/CD deployments
- Infrastructure automation
- Remote troubleshooting
- Secure file transfers
- Database administration
- Server maintenance
It is the standard protocol for secure remote Linux administration.
Hands-on Lab¶
Task 1¶
Verify the SSH service.
Task 2¶
Generate an SSH key pair.
Task 3¶
Display the public key.
Task 4¶
Connect to a remote server.
Task 5¶
Create an SSH client configuration.
Task 6¶
Test the configuration.
Task 7¶
Verify that SSH is listening.
Task 8¶
Review SSH logs.
Command Deep Dive¶
| Command | Purpose | Production Example |
|---|---|---|
ssh user@host | Connect to a server | Remote administration |
ssh -p | Use custom port | Hardened servers |
ssh-keygen | Generate key pair | Secure authentication |
ssh-copy-id | Install public key | Passwordless login |
ssh -L | Local port forwarding | Database access |
ssh -R | Remote port forwarding | Remote tunneling |
ssh -D | Dynamic port forwarding | SOCKS proxy |
systemctl status sshd | Verify SSH service | Troubleshooting |
Common SSH Errors¶
| Error | Possible Cause |
|---|---|
Connection refused | SSH service not running or port blocked |
Permission denied (publickey) | Public key missing or incorrect permissions |
Permission denied (password) | Incorrect credentials or password authentication disabled |
Connection timed out | Firewall, routing, or network issue |
Host key verification failed | Server host key has changed |
Production Troubleshooting Scenario¶
Scenario
A DevOps engineer cannot SSH into a production server.
Investigation:
Verify network connectivity.
Check whether SSH is listening.
Verify the SSH service.
Review logs.
The logs indicate that the user's public key is not authorized.
Copy the public key to the server.
Reconnect.
Access is restored successfully.
Best Practices¶
- Use SSH key-based authentication instead of passwords.
- Disable direct root login.
- Disable password authentication when key-based authentication is fully deployed.
- Protect private keys with strong passphrases.
- Restrict SSH access using firewalls and security groups.
- Keep OpenSSH updated with security patches.
- Monitor SSH logs for unauthorized access attempts.
Common Mistakes¶
❌ Using weak passwords for SSH.
✅ Avoid using weak passwords for SSH when a safer approach exists.
❌ Enabling direct root login in production.
✅ Avoid this mistake: enabling direct root login in production.
❌ Sharing private SSH keys.
✅ Avoid this mistake: sharing private SSH keys.
❌ Leaving password authentication enabled unnecessarily.
✅ Do not leave password authentication enabled unnecessarily.
❌ Ignoring SSH log files after failed login attempts.
✅ Always review SSH log files after failed login attempts.
Interview Questions¶
Beginner¶
- What is SSH?
- Which port does SSH use by default?
- How do you connect to a remote server?
- What is the difference between a public key and a private key?
Intermediate¶
- Why is SSH key authentication preferred over passwords?
- What is the purpose of
ssh-copy-id? - What is local port forwarding?
- Where is the SSH server configuration stored?
Architect Level¶
- How would you secure SSH access for hundreds of production servers?
- How would you implement centralized SSH key management?
- How would you troubleshoot intermittent SSH connection failures in a cloud environment?
Summary¶
In this lesson, you learned:
- SSH fundamentals
- Secure remote access
- SSH authentication
- SSH key pairs
- SSH configuration
- Port forwarding
- SSH troubleshooting
- Production security best practices
SSH is the standard protocol for secure remote Linux administration. Mastering SSH enables administrators to manage servers, automate deployments, transfer files, and securely access infrastructure across on-premises and cloud environments.
Key Takeaways¶
- SSH provides encrypted remote access to Linux systems.
- SSH uses TCP port 22 by default.
- SSH key-based authentication is more secure than passwords.
- SSH configuration files simplify repeated connections.
- Port forwarding enables secure access to remote services.
- Securing SSH is critical for protecting production infrastructure.
What's Next?¶
SCP (Secure Copy Protocol) — Secure File Transfer Between Linux Systems
You'll explore:
- Secure file transfers using SCP
- Copying files between local and remote systems
- Recursive transfers
- Preserving permissions
- Production file transfer practices
Then you'll continue with rsync for efficient file synchronization and backups.