The real hardening happens in /etc/ssh/sshd_config. I’ll walk you through each critical setting.
Backup Original Configuration
Always create a backup before making changes:
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.backup.$(date +%F)Edit SSH Configuration
sudo nano /etc/ssh/sshd_configEssential Security Settings
Here’s my production-ready configuration:
# Network SettingsPort 2222 # Change from default port 22AddressFamily inet # IPv4 only (use 'any' for IPv4+IPv6)ListenAddress 0.0.0.0 # Or specify exact IP
# Authentication SettingsPermitRootLogin no # Never allow direct root loginPubkeyAuthentication yesPasswordAuthentication no # Disable password authPermitEmptyPasswords noChallengeResponseAuthentication noUsePAM yes
# Key Types (ED25519 preferred)PubkeyAcceptedKeyTypes ssh-ed25519,rsa-sha2-512,rsa-sha2-256
# Limit user accessAllowUsers deployer sysadmin # Only specific users# AllowGroups ssh-users # Or use groups
# Session SettingsMaxAuthTries 3 # Limit authentication attemptsMaxSessions 2 # Limit concurrent sessionsLoginGraceTime 30 # Timeout for authenticationClientAliveInterval 300 # Keep-alive messagesClientAliveCountMax 2 # Disconnect after 2 missed keep-alives
# Disable Dangerous FeaturesX11Forwarding noPermitUserEnvironment noAllowAgentForwarding noAllowTcpForwarding noPermitTunnel no
# LoggingSyslogFacility AUTHLogLevel VERBOSE # Detailed logging for security analysis
# Modern Cryptography (2025 standards)
# Security HeadersHostbasedAuthentication noIgnoreRhosts yesValidate Configuration
Before restarting SSH, validate your configuration:
sudo sshd -tIf there are no errors, restart the SSH service:
# SystemD systemssudo systemctl restart sshd
# Check statussudo systemctl status sshdDanger
Critical: Keep your current SSH session open. Open a NEW terminal and test the connection. Only after confirming the new session works should you close the original.
Update Firewall Rules
If you changed the SSH port, update your firewall:
# UFW (Ubuntu/Debian)sudo ufw allow 2222/tcpsudo ufw delete allow 22/tcpsudo ufw reload
# firewalld (RHEL/CentOS)sudo firewall-cmd --permanent --add-port=2222/tcpsudo firewall-cmd --permanent --remove-service=sshsudo firewall-cmd --reloadNext Steps
With SSH daemon hardened, proceed to the Two-Factor Authentication guide to add an additional security layer beyond SSH keys.