Lesson 7 of 10
SSH Security & Remote Access Hardening
SSH (Secure Shell) is the primary tool for remote system administration. Default SSH configurations leave many unnecessary attack surfaces open. Properly hardened SSH significantly reduces exposure to brute force attacks and unauthorized access.
SSH Key-Based Authentication Setup
# SSH key authentication is dramatically more secure than passwords
# Keys are mathematically unforgeable; the private key never leaves your machine
# 1. Generate an SSH key pair on your LOCAL machine
ssh-keygen -t ed25519 -C "your@email.com"
# -t ed25519: Modern elliptic curve algorithm (better than RSA 2048)
# Enter a strong passphrase when prompted (protects if key file is stolen)
# Creates: ~/.ssh/id_ed25519 (private) and ~/.ssh/id_ed25519.pub (public)
# 2. Copy public key to the remote server
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server
# OR manually append to ~/.ssh/authorized_keys on the server
# 3. Verify key authentication works
ssh -i ~/.ssh/id_ed25519 user@server
# 4. After verifying - disable password authentication on server
# Edit /etc/ssh/sshd_config:
# PasswordAuthentication no
# Then restart: systemctl restart sshdSSH Server Hardening (sshd_config)
# /etc/ssh/sshd_config - Production-ready secure configuration
# Basic settings
Port 2222 # Non-default port (reduces automated scanning noise)
Protocol 2 # SSH protocol 2 only (protocol 1 is broken)
AddressFamily inet # IPv4 only (reduce attack surface if IPv6 unused)
# Authentication hardening
PermitRootLogin no # Never allow direct root SSH login
PasswordAuthentication no # Keys only - no password brute force possible
PubkeyAuthentication yes # Enable key authentication
AuthorizedKeysFile .ssh/authorized_keys
MaxAuthTries 3 # Disconnect after 3 failed attempts
LoginGraceTime 20 # Disconnect unauthenticated sessions after 20s
# Session limits
MaxSessions 3 # Max concurrent sessions per connection
ClientAliveInterval 300 # Send keepalive every 5 minutes
ClientAliveCountMax 2 # Disconnect after 2 missed keepalives
# Restrict users
AllowUsers deployer admin # Only specific users can SSH
AllowGroups sshusers # OR only specific group members
# Cryptography - use only strong algorithms
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com
MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com
KexAlgorithms curve25519-sha256,diffie-hellman-group16-sha512
# Disable insecure features
X11Forwarding no # Disable X11 (unnecessary on servers)
AllowAgentForwarding no # Limit agent forwarding (pivoting risk)
PermitEmptyPasswords no # Never allow empty password accounts
# Apply: systemctl restart sshd
# Test in new terminal before closing current session!fail2ban - Automated Brute Force Protection
# fail2ban monitors logs and bans IPs exceeding failure thresholds
apt install fail2ban
systemctl enable fail2ban
# /etc/fail2ban/jail.local
[DEFAULT]
bantime = 3600 # Ban for 1 hour
findtime = 600 # Evaluate failures in 10-minute windows
maxretry = 3 # Ban after 3 failures
[sshd]
enabled = true
port = 2222 # Match your SSH port
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
# View active bans
fail2ban-client status sshd
fail2ban-client status sshd | grep "Banned IP"
# Unban an IP (if you accidentally lock yourself out)
fail2ban-client set sshd unbanip 192.168.1.100Common Mistakes
- Leaving root login enabled - attackers know root exists on every Linux system; prohibit direct root SSH and require sudo for privilege escalation
- Not using a passphrase on SSH private keys - a key without a passphrase is a password written in a file
- Restarting sshd without testing in a second terminal first - an sshd config error will prevent future SSH connections, locking you out of the server
- Using the same SSH key for all environments - a compromised key on a dev machine shouldn't provide access to production systems
Tip
Tip
Practice SSH Security Remote Access Hardening in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
The foundational security model: Confidentiality (privacy), Integrity (trust), and Availability (uptime).
Practice Task
Note
Practice Task - (1) Write a working example of SSH Security Remote Access Hardening from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with SSH Security Remote Access Hardening is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready cybersecurity code.
Key Takeaways
- SSH (Secure Shell) is the primary tool for remote system administration.
- Leaving root login enabled - attackers know root exists on every Linux system; prohibit direct root SSH and require sudo for privilege escalation
- Not using a passphrase on SSH private keys - a key without a passphrase is a password written in a file
- Restarting sshd without testing in a second terminal first - an sshd config error will prevent future SSH connections, locking you out of the server
Finished this lesson?