7 Quick and Easy Ways to Secure SSH on a Linux Server
Learn how to secure your Linux server's SSH access with seven essential security measures, including key-based authentication, two-factor authentication, firewall configuration, and more.
Secure Shell (SSH) is a fundamental tool for remotely managing Linux servers, providing encrypted communication channels for secure remote access and file transfers.
While SSH is inherently secure due to its encryption protocols, its widespread use across millions of servers makes it a prime target for cybercriminals and automated attacks. Every day, servers face countless unauthorized access attempts through SSH, ranging from simple brute-force attacks to sophisticated exploitation techniques.
Default SSH configurations, while functional, often prioritize convenience over security. This can leave your server vulnerable to various attack vectors, potentially compromising your entire system if breached.
Basically, as a system administrator or server owner, implementing proper SSH security measures isn't just best practice – it's essential for protecting your infrastructure, data, and services from unauthorized access. Otherwise, it's only a matter of time.
That said, in this article, we'll be sharing seven quick and easy methods to improve SSH security on your Linux server. These techniques are proven, and practical, and can be implemented within minutes by nearly anyone. Let's dive in.
1. Disable root login
Allowing the root user to log in directly via SSH poses a significant security risk.
If an attacker gains access to the root account, they have unrestricted control over your server.
How to disable root login
Open the SSH configuration file:
sudonano /etc/ssh/sshd_config
Note: While the underlying information is very similar, some steps throughout may slightly differ depending on your Linux distribution. For example, your default editor may differ from Nano, such as Vim. Also, if you're using a RHEL-based distribution (Red Hat Enterprise Linux, CentOS Stream, Fedora, AlmaLinux, Rocky Linux, etc.), you'll need to use dnf instead of apt.
Find the line that says PermitRootLogin and change it to:
PermitRootLogin no
Save the file and restart SSH:
sudo systemctl restart sshd
By disabling root login, users must log in with a non-privileged account and escalate privileges using sudo, adding an extra layer of security.
2. Use SSH key-based authentication and disable passwords
Password-based authentication is susceptible to brute-force attacks.
SSH key-based authentication uses cryptographic keys, which are significantly more secure.
How to set up SSH keys and disable password authentication
Note: As mentioned earlier, FirewallD is the default firewall management tool on RHEL-based systems, while UFW is more common on Debian-based systems (Ubuntu, Debian). Choose the appropriate firewall tool based on your distribution.
5. Use strong passwords and implement Fail2Ban
How to install and configure Fail2Ban
Install Fail2Ban:
sudoapt-getinstall fail2ban
Note: Remember, if you're using a RHEL-based distribution, you'll need to use dnf.