How to Secure SSH Access on Ubuntu Server: Step-by-Step Hardening Guide Introduction
Securing SSH (Secure Shell) access is the most critical first step after deploying an Ubuntu Linux server. Since SSH runs on default settings on most fresh installations, it becomes an attractive target for automated brute-force attacks, botnets, and unauthorized login attempts.
In this comprehensive guide, we will walk you through essential SSH hardening practices to secure your Ubuntu server and protect your system infrastructure from potential security breaches.
1. Backup Your Existing SSH Configuration
Before making any changes to system files, always create a backup copy of your configuration file.sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
2. Generate and Use SSH Key Authentication
Password-based authentication is vulnerable to brute-force attacks. Switching to SSH Key-based authentication ensures that only authorized devices with matching private keys can gain entry.
Step 1: Generate SSH Keys on Your Local Machine
Run the following command on your local terminal:ssh-keygen -t ed25519 -C "admin@yourdomain.com"
Step 2: Copy the Public Key to Your Ubuntu Server
Upload your public key to your remote server using ssh-copy-id:ssh-copy-id username@your_server_ip
3. Disable Password Authentication and Root Login:
Once your SSH key is tested and working, disable root logins and password authentication entirely.
Open the SSH config file:sudo nano /etc/ssh/sshd_config
Locate and modify the following parametersPermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
MaxAuthTries 3
PermitRootLogin no: Prevents attackers from targeting the root account directly.
PasswordAuthentication no: Forces the server to accept SSH keys only.
MaxAuthTries 3: Drops connections after three failed authentication attempts.
4. Change the Default SSH Port
By default, SSH listens on Port 22. Changing this to a non-standard port reduces log noise from automated scanning scripts.
Inside /etc/ssh/sshd_config, update the port directive:Port 2222
Replace 2222 with any preferred custom port number between 1024 and 65535).
5. Configure Firewall Rules (UFW)
Ensure your firewall allows traffic on your new SSH port before restarting the SSH service, otherwise you may lock yourself out.
sudo ufw allow 2222/tcp
sudo ufw enable
sudo ufw status
6. Install and Configure Fail2ban
Fail2ban monitors log files for repeated login failures and automatically blocks abusive IP addresses using firewall rules.
Install Fail2ban:sudo apt update && sudo apt install fail2ban -y
Create a Local Configuration File:sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo nano /etc/fail2ban/jail.local
Enable SSH Protection in Jail.local:[sshd]
enabled = true
port = 2222
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 3600
Restart Fail2ban to apply changes:sudo systemctl restart fail2ban
7. Restart and Test SSH Configuration
Test your configuration for syntax errors before restarting the SSH daemon:sudo sshd -t
If no errors are reported, restart the SSH service:sudo systemctl restart sshd
Important Note: Keep your current terminal session open! Open a new terminal window and verify that you can successfully log in using your SSH key and custom port:ssh -p 2222 username@your_server_ip
Conclusion
By enforcing SSH key authentication, disabling root login, changing the default port, and deploying Fail2ban, you have significantly reduced your server's attack surface. Implementing these basic server hardening steps is an industry best practice for maintaining production-grade Linux systems.
Comments
Post a Comment