Securing SSH access is crucial to prevent unauthorized logins and brute-force attacks. In this guide, I’ll share 14 essential steps that I personally use to harden SSH security on my Linux servers.
1. Disable Root Logins
Logging in as root via SSH is a major security risk. Instead, I always use a regular user with sudo privileges.
To disable root login, edit the SSH configuration file:
sudo vim /etc/ssh/sshd_config
Find and modify:
PermitRootLogin no
Restart SSH:
sudo systemctl restart sshd
2. Restrict SSH Access to Specific Users
By default, any system user can attempt to connect via SSH. I prefer to restrict access to only specific users:
sudo vim /etc/ssh/sshd_config
Add:
AllowUsers myuser anotheruser
Restart SSH:
sudo systemctl restart sshd
3. Disable SSH Protocol 1
SSH Protocol 1 is outdated and insecure. I always ensure that my server only allows Protocol 2:
sudo vim /etc/ssh/sshd_config
Modify:
Protocol 2
Restart SSH:
sudo systemctl restart sshd
4. Change the Default SSH Port
To reduce automated attacks, I change the default SSH port (22) to something less common:
sudo vim /etc/ssh/sshd_config
Modify:
Port 2223
Restart SSH:
sudo systemctl restart sshd
If using SELinux, allow the new port:
sudo semanage port -a -t ssh_port_t -p tcp 2223
5. Set SSH Session Timeout
I prevent idle SSH sessions from staying open for too long by setting a timeout:
sudo vim /etc/ssh/sshd_config
Modify:
ClientAliveInterval 300
ClientAliveCountMax 0
Restart SSH:
sudo systemctl restart sshd
6. Limit Maximum Authentication Attempts
To reduce brute-force attack risks, I set a limit on the number of failed login attempts:
sudo vim /etc/ssh/sshd_config
Modify:
MaxAuthTries 5
Restart SSH:
sudo systemctl restart sshd
7. Restrict SSH to a Specific IP Address
If my server has multiple network interfaces, I make SSH listen only on a specific IP address:
sudo vim /etc/ssh/sshd_config
Modify:
ListenAddress 192.168.10.112
Restart SSH:
sudo systemctl restart sshd
8. Disable Rhosts Authentication
To prevent potential security risks, I disable .rhosts
authentication:
sudo vim /etc/ssh/sshd_config
Modify:
IgnoreRhosts yes
Restart SSH:
sudo systemctl restart sshd
9. Disable Empty Password Logins
I make sure no user can log in with an empty password, which is a huge security risk:
sudo vim /etc/ssh/sshd_config
Modify:
PermitEmptyPasswords no
Restart SSH:
sudo systemctl restart sshd
10. Disable Host-Based Authentication
Since host-based authentication can be risky, I always disable it:
sudo vim /etc/ssh/sshd_config
Modify:
HostbasedAuthentication no
Restart SSH:
sudo systemctl restart sshd
11. Enable Detailed SSH Logging
To monitor SSH activity, I set the logging level to INFO:
sudo vim /etc/ssh/sshd_config
Modify:
LogLevel INFO
Restart SSH:
sudo systemctl restart sshd
12. Limit Maximum Concurrent Connections
I reduce the number of simultaneous SSH connections to prevent brute-force attacks:
sudo vim /etc/ssh/sshd_config
Modify:
MaxStartups 4
Restart SSH:
sudo systemctl restart sshd
13. Reduce Login Grace Time
To minimize the time allowed for authentication attempts, I shorten the login grace period:
sudo vim /etc/ssh/sshd_config
Modify:
LoginGraceTime 1m
Restart SSH:
sudo systemctl restart sshd
14. Disable Password Authentication (Use SSH Keys Only)
For maximum security, I disable password authentication and only allow SSH key authentication:
sudo vim /etc/ssh/sshd_config
Modify:
PasswordAuthentication no
Restart SSH:
sudo systemctl restart sshd
If you’re not familiar with SSH key authentication, check out my guide on How to Set Up SSH Key-Based Authentication in Linux.
Try it out, and let me know if you run into any issues. Drop a comment below!
Related Posts:
How to Set Up SSH Key-Based Authentication in Linux
SSH into Raspberry Pi Zero over USB
Save, Load and Transfer Docker Images Easily
20 Linux Basic Commands You Must Know