SSH Hardening: 12 Tips to Secure Your Linux Server
In this article
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
- 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. - Update your system and SSH package. Run
sudo apt update && sudo apt upgrade(Debian/Ubuntu) orsudo dnf update(Fedora/RHEL). Keeping OpenSSH current ensures you have the latest security patches. - Change the default SSH port. Edit
/etc/ssh/sshd_configand find the line#Port 22. Change it to a high, unused port like 2222 (avoid common ports). Uncomment and setPort 2222. Remember to update firewall rules accordingly. - 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. - 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, setPasswordAuthentication noin sshd_config. - Disable empty passwords. Ensure
PermitEmptyPasswords nois set (it is by default on most systems). - Limit which users can log in. Add
AllowUsers yourusername(orAllowGroups sudo) to the config. This whitelists only the accounts that need SSH access. - Set a login grace time and max auth tries. Add
LoginGraceTime 30andMaxAuthTries 3to slow down brute-force attempts. - 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 setPasswordAuthentication noand restart again. - 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.localwith[sshd] enabled = trueand restart fail2ban. - Use a firewall to restrict SSH access. If you use UFW:
sudo ufw allow 2222/tcpandsudo ufw deny 22/tcp(if you changed the port). For firewalld:sudo firewall-cmd --permanent --add-port=2222/tcpthen reload. Limit source IPs if possible, e.g.,sudo ufw allow from 192.168.1.100 to any port 2222. - Consider disabling SSH protocol 1 and unused ciphers. Modern OpenSSH already disables protocol 1. For extra security, you can add
KexAlgorithms curve25519-sha256andCiphers 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 nobut 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 -tbefore 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 sshortail -f /var/log/auth.logto 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.