- This topic is empty.
-
AuthorPosts
-
-
January 17, 2025 at 3:15 am #5344
Kris Marker
Keymaster
SSH Hardening – The Basics SSH Hardening – The Basics
Comprehensive Guide to Hardening SSH on Debian-Based Systems
Table of Contents
- Introduction to SSH
- Update SSH Software
- Disable Root Login
- Use Key-Based Authentication
- Change the Default SSH Port
- Restrict User Access
- Configure Idle Timeout
- Disable Unnecessary Features
- Use Strong Encryption Algorithms
- Enable Fail2Ban
- Implement Two-Factor Authentication (2FA)
- Monitor and Audit SSH Access
- Regularly Rotate SSH Keys
- Use a Bastion Host
- Test Your Configuration
- Advanced sshd_config Settings
Introduction to SSH

SSH (Secure Shell) is a cryptographic network protocol used for secure communication over an unsecured network. It allows users to securely access and manage remote systems, transfer files, and execute commands. SSH was developed in 1995 by Tatu Ylönen as a replacement for insecure protocols like Telnet and rsh.
SSH is widely used in system administration, cloud computing, and DevOps. However, its popularity also makes it a common target for attackers. By hardening your SSH configuration, you can significantly reduce the risk of unauthorized access and ensure the security of your systems.
Update SSH Software
Keeping your SSH software up to date is the first step in securing your system. Updates often include security patches for known vulnerabilities.
sudo apt update sudo apt upgrade openssh-serverAdditional Considerations:
Enable automatic security updates to ensure SSH stays up to date:sudo apt install unattended-upgrades sudo dpkg-reconfigure --priority=low unattended-upgradesDisable Root Login
Allowing root login over SSH is a significant security risk. Disable it to force attackers to guess both a username and password.
sudo nano /etc/ssh/sshd_configFind the line
#PermitRootLogin yesand change it to:PermitRootLogin noSave and exit the file, then restart the SSH service:
sudo systemctl restart sshdAdditional Considerations:
Use a non-root user withsudoprivileges for administrative tasks.Use Key-Based Authentication
Key-based authentication is more secure than passwords because it uses cryptographic keys instead of easily guessable passwords.
Generate an SSH key pair on the client machine:
ssh-keygen -t ed25519 -a 100Copy the public key to the server:
ssh-copy-id username@server_ipDisable password authentication:
sudo nano /etc/ssh/sshd_config PasswordAuthentication noRestart the SSH service:
sudo systemctl restart sshdAdditional Considerations:
Use a passphrase for your private key and consider using a hardware security key (e.g., YubiKey).Change the Default SSH Port
Changing the default SSH port (22) reduces exposure to automated scans and brute-force attacks.
sudo nano /etc/ssh/sshd_config Port 2222 # Replace 2222 with a non-standard portUpdate firewall rules:
sudo ufw allow 2222/tcp sudo ufw reloadRestart the SSH service:
sudo systemctl restart sshdAdditional Considerations:
Choose a port number above 1024 to avoid conflicts with well-known ports.Restrict User Access
Limit SSH access to specific users or groups to minimize the attack surface.
sudo nano /etc/ssh/sshd_config AllowUsers user1 user2 AllowGroups sshusersRestart the SSH service:
sudo systemctl restart sshdAdditional Considerations:
Create a dedicated group for SSH users:sudo groupadd sshusers sudo usermod -aG sshusers user1Configure Idle Timeout
Automatically disconnect idle sessions to reduce the risk of unauthorized access.
sudo nano /etc/ssh/sshd_config ClientAliveInterval 300 # 5 minutes ClientAliveCountMax 2 # Disconnect after 2 intervals (10 minutes)Restart the SSH service:
sudo systemctl restart sshdDisable Unnecessary Features
Disable features like X11 forwarding, TCP forwarding, and agent forwarding unless explicitly needed.
sudo nano /etc/ssh/sshd_config X11Forwarding no AllowTcpForwarding no AllowAgentForwarding noRestart the SSH service:
sudo systemctl restart sshdUse Strong Encryption Algorithms
Configure SSH to use only secure cryptographic algorithms.
sudo nano /etc/ssh/sshd_config Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com KexAlgorithms curve25519-sha256@libssh.org,diffie-hellman-group-exchange-sha256Restart the SSH service:
sudo systemctl restart sshdEnable Fail2Ban
Fail2Ban automatically blocks IP addresses that repeatedly fail SSH authentication.
sudo apt install fail2ban sudo nano /etc/fail2ban/jail.localAdd the following:
[sshd] enabled = true maxretry = 3 bantime = 1hRestart Fail2Ban:
sudo systemctl restart fail2banImplement Two-Factor Authentication (2FA)
Add an extra layer of security with 2FA using Google Authenticator.
sudo apt install libpam-google-authenticator google-authenticatorConfigure SSH to use 2FA:
sudo nano /etc/pam.d/sshd auth required pam_google_authenticator.so sudo nano /etc/ssh/sshd_config ChallengeResponseAuthentication yesRestart the SSH service:
sudo systemctl restart sshdMonitor and Audit SSH Access
Regularly monitor SSH logs for suspicious activity.
sudo tail -f /var/log/auth.logAdditional Considerations:
Use a centralized logging solution (e.g., SIEM) for advanced monitoring.Regularly Rotate SSH Keys
Periodically generate and replace SSH keys to minimize the impact of key compromise.
ssh-keygen -t ed25519 -a 100Replace the old public key on the server with the new one.
Use a Bastion Host
A bastion host acts as a single entry point for SSH access, reducing the attack surface.
Set up a dedicated server as the bastion host and restrict SSH access to it.
Test Your Configuration
After making changes, test your SSH configuration to ensure everything works as expected.
sudo sshd -tAttempt to connect to the server using the new settings.
Advanced sshd_config Settings
Here are additional settings to further harden your SSH server:
Setting Insecure Value Secure Value Why? Protocol12SSHv1 is outdated and insecure. MaxAuthTries63Limits brute-force attempts. LoginGraceTime2m1mReduces time for brute-force attacks.
-
-
AuthorPosts
- You must be logged in to reply to this topic.