Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

SSH Hardening: 12 Tips to Secure Your Linux Server

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Securing SSH on your Linux server or desktop is one of the most effective ways to block unauthorized access. By default, SSH listens on port 22 and accepts password logins, which invites brute-force attacks. This guide walks you through practical, low-risk hardening steps that dramatically reduce your attack surface. You'll need root or sudo access to the machine, and a way to edit system files (nano or vim).

What You'll Need

  • A Linux server or desktop with SSH installed (OpenSSH is standard).
  • Root or sudo privileges.
  • A text editor (nano, vim, or similar).
  • Optional: a second SSH session already open, to avoid locking yourself out.

Step-by-Step

  1. Back up your SSH configuration. Before making changes, copy the main config file: sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak. This lets you revert if something goes wrong.
  2. Update your system and SSH package. Run sudo apt update && sudo apt upgrade (Debian/Ubuntu) or sudo dnf update (Fedora/RHEL). Keeping OpenSSH current ensures you have the latest security patches.
  3. Change the default SSH port. Edit /etc/ssh/sshd_config and find the line #Port 22. Change it to a high, unused port like 2222 (avoid common ports). Uncomment and set Port 2222. Remember to update firewall rules accordingly.
  4. Disable root login. In the same file, set PermitRootLogin no. This forces attackers to guess a username as well as a password. If you need admin access, use a regular user with sudo.
  5. Use SSH key authentication instead of passwords. Generate a key pair on your local machine: ssh-keygen -t ed25519. Then copy the public key to the server: ssh-copy-id -p 2222 user@your-server. After testing, set PasswordAuthentication no in sshd_config.
  6. Disable empty passwords. Ensure PermitEmptyPasswords no is set (it is by default on most systems).
  7. Limit which users can log in. Add AllowUsers yourusername (or AllowGroups sudo) to the config. This whitelists only the accounts that need SSH access.
  8. Set a login grace time and max auth tries. Add LoginGraceTime 30 and MaxAuthTries 3 to slow down brute-force attempts.
  9. Enable key-based authentication only after testing. After making changes, restart SSH with sudo systemctl restart sshd. Keep your current session open and test a new connection. Only then set PasswordAuthentication no and restart again.
  10. Install and configure fail2ban. Install it via your package manager (sudo apt install fail2ban). It automatically blocks IPs that show malicious behavior. Create a local jail file /etc/fail2ban/jail.local with [sshd] enabled = true and restart fail2ban.
  11. Use a firewall to restrict SSH access. If you use UFW: sudo ufw allow 2222/tcp and sudo ufw deny 22/tcp (if you changed the port). For firewalld: sudo firewall-cmd --permanent --add-port=2222/tcp then reload. Limit source IPs if possible, e.g., sudo ufw allow from 192.168.1.100 to any port 2222.
  12. Consider disabling SSH protocol 1 and unused ciphers. Modern OpenSSH already disables protocol 1. For extra security, you can add KexAlgorithms curve25519-sha256 and Ciphers chacha20-poly1305@openssh.com, but only if you know your clients support them.

Common Mistakes

  • Changing the port but forgetting to update the firewall, which locks you out.
  • Disabling password authentication before testing key-based login, leaving you unable to connect.
  • Setting PermitRootLogin no but then forgetting to create a sudo user.
  • Using a weak passphrase for your SSH key, or leaving it empty.
  • Editing sshd_config and making a syntax error, which can prevent SSH from starting. Always test with sudo sshd -t before restarting.

Tips

  • Use a passphrase on your private key and consider adding it to an SSH agent so you don't type it every time.
  • Set up a cron job to update fail2ban and system packages regularly.
  • Monitor your SSH logs with sudo journalctl -u ssh or tail -f /var/log/auth.log to spot suspicious activity.
  • If you need remote access from anywhere, consider using a VPN (like WireGuard) and only allow SSH from the VPN's IP range.
  • For desktop machines that don't need remote access, disable the SSH service entirely: sudo systemctl disable --now sshd.

Troubleshooting

If you get "Connection refused" after changing the port: Check your firewall rules. Run sudo ufw status or sudo firewall-cmd --list-all. Ensure the new port is allowed. Also verify SSH is running: sudo systemctl status sshd.

If you get "Permission denied" when trying to log in: Make sure your key is in the correct location on the server (~/.ssh/authorized_keys) and has correct permissions (the file should be 600, the .ssh directory 700). Check that PasswordAuthentication is still set to yes if you're trying password login.

If SSH service fails to restart: Run sudo sshd -t to test the config for syntax errors. If it reports an error, fix it and try again. If you can't fix it, restore the backup: sudo cp /etc/ssh/sshd_config.bak /etc/ssh/sshd_config.

If fail2ban is not banning IPs: Check the fail2ban log: sudo tail -f /var/log/fail2ban.log. Ensure the jail is enabled and the log path for sshd is correct (it usually is). If you're behind a proxy or VPN, fail2ban may see the proxy's IP, so adjust accordingly.

Frequently Asked Questions

Is changing the SSH port really necessary?

It's not a silver bullet, but it drastically reduces automated brute-force scans that target port 22. It's a simple step that adds a layer of obscurity.

Can I use both password and key authentication?

Yes, you can set PasswordAuthentication yes and still use keys. However, for maximum security, disable passwords after confirming keys work.

What if I lose my SSH key?

If you lose the private key and have disabled passwords, you'll need to access the server via a console (like a cloud provider's web console or a physical terminal) to re-enable password auth or add a new key.

Does fail2ban work on all Linux distributions?

Fail2ban is available in most package repositories. The configuration is similar, but the service name and log paths may differ slightly. Check your distribution's documentation.

Should I use ed25519 or RSA keys?

Ed25519 is faster and more secure for modern systems. Use RSA only if you have older clients that don't support ed25519 (e.g., some legacy embedded devices).

Can I secure SSH on a desktop without a server?

Yes, the same steps apply. If you don't need remote SSH access, simply disable the service. If you do, follow the guide; the only difference is you might be connecting from the same machine, so test carefully.