Skip to content
Network Security

How to Secure Your SSH Server

Practical ways to protect your SSH server from unauthorized access

2 min read
Secure Your SSH Server

Securing Your SSH Server: Best Practices with Practical Insights

SSH servers are critical for secure remote administration, but they are often targeted by attackers. This guide outlines practical steps to fortify your SSH server against unauthorized access.

Note: No system connected to the internet is 100% secure, but these practices can significantly reduce vulnerabilities.


1. Use SSH Keys

SSH keys provide stronger authentication than passwords.

Practical Steps:

  1. Generate an SSH key pair on your local machine:
    ssh-keygen -t ed25519
    
  2. Copy the public key to your server:
    ssh-copy-id -i ~/.ssh/id_ed25519.pub username@<server_ip>
    
  3. Test the connection:
    ssh username@<server_ip>
    

Benefits: Passwordless login, enhanced security.


2. Disable SSH Password Authentication

Eliminating password-based logins prevents brute-force attacks.

Configuration:

Edit the SSH configuration file (/etc/ssh/sshd_config):

PasswordAuthentication no
PermitEmptyPasswords no
UsePAM no

Restart the SSH service:

systemctl restart sshd

Caution: Ensure SSH key-based authentication is set up before disabling passwords.


3. Disallow Root Login

Restricting root access minimizes the risk of privilege escalation.

Configuration:

In /etc/ssh/sshd_config, update:

PermitRootLogin no

Restart the SSH service:

systemctl restart sshd

Tip: Use a regular user account with sudo for administrative tasks.


4. Change the Default SSH Port

Moving away from the default port (22) can reduce automated attacks.

Steps:

  1. Edit /etc/ssh/sshd_config:
    Port 5823
    
  2. Restart SSH:
    systemctl restart sshd
    
  3. Connect using the new port:
    ssh -p 5823 username@<server_ip>
    

Note: Ensure the chosen port is not in use by another service.


5. Perform an SSH Audit

Regular audits help identify weaknesses in your SSH configuration.

Steps:

  1. Visit SSH Audit to check your server.
  2. Review the provided score and recommendations.
  3. Apply the fixes from the SSH Hardening Guide.

Re-audit: Repeat the scan to confirm improvements.


6. Implement Rate Limiting

Limit the number of connection attempts to deter brute-force attacks.

Configuration:

Use tools like fail2ban to block IPs after repeated failed attempts:

sudo apt-get install fail2ban

Create a custom jail configuration for SSH in /etc/fail2ban/jail.local:

[sshd]
enabled = true
port = 5823
logpath = /var/log/auth.log
maxretry = 3

Restart fail2ban:

systemctl restart fail2ban

7. Enable Two-Factor Authentication (2FA)

Adding a second layer of authentication enhances security.

Setup:

  1. Install the Google Authenticator PAM module:
    sudo apt-get install libpam-google-authenticator
    
  2. Configure it on your server:
    google-authenticator
    
  3. Update /etc/ssh/sshd_config:
    ChallengeResponseAuthentication yes
    
  4. Restart SSH:
    systemctl restart sshd
    

Tip: Use a 2FA app like Google Authenticator to generate time-based codes.


8. Monitor SSH Logs

Regularly review SSH logs to detect suspicious activity.

Commands:

  • View recent SSH login attempts:
    sudo tail -f /var/log/auth.log
    
  • Search for failed login attempts:
    sudo grep "Failed password" /var/log/auth.log
    

Actionable Insight: Investigate and block IPs showing repeated unauthorized access attempts.


Summary

Securing your SSH server involves multiple layers of defense, from key-based authentication to regular audits and monitoring. By implementing these best practices, you can significantly reduce the risk of unauthorized access.

Stay Vigilant, Stay Secure.


Share article

Subscribe to my newsletter

Receive my case study and the latest articles on my WhatsApp Channel.