
Discover the best practices for securing SSH connections on Linux servers. Learn essential security tips like disabling root login, using SSH keys, and more for
Learn how to harden SSH on Ubuntu 24.04. Disable root login, enforce SSH key authentication, and restrict IP access for maximum protection.
Secure Shell (SSH) is the backbone of remote system management, but an exposed or misconfigured SSH service is a prime target for attackers. Ubuntu 24.04, as a modern Linux distribution, offers powerful features to harden SSH access, but the defaults may not be strict enough for production environments.
In this guide, you’ll learn how to disable root login, enforce SSH key authentication, and restrict access by IP to reduce your system’s attack surface significantly.
Even a freshly installed Ubuntu server can become a target within minutes of being online. Common SSH threats include:
| Threat | Description |
|---|---|
| Brute-force attacks | Automated attempts to guess credentials using known usernames. |
| Root login abuse | If root SSH is enabled, a successful login grants full system access. |
| Password cracking | Password-based logins are vulnerable to dictionary attacks. |
| Open access | Unrestricted IP access invites probing and exploitation. |
Ensure you have:
|
|
|
Why: Prevent attackers from targeting the highly privileged root account directly.
🔹Open the SSH config file: |
sudo vim /etc/ssh/sshd_config
🔹Find or add the following line: |
PermitRootLogin no
🔒 Tip: Ensure this line is not commented out ( |
🔹Restart SSH: |
sudo systemctl restart ssh
🧪 Test: Try to SSH into the server using the root account: |
ssh root@your-server-ip
You should receive a “Permission denied” message.
Why: SSH keys are virtually impossible to brute-force and eliminate password-based login risks.
🔹Generate an SSH Key (on your client machine): |
ssh-keygen -t ed25519 -C "your_email@example.com"
Use a strong passphrase when prompted.
🔹Upload the public key to the server: |
ssh-copy-id username@your-server-ip
Alternatively, manually copy the key:
cat ~/.ssh/id_ed25519.pub | ssh username@your-server-ip 'mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys'
🔹On the server, disable password authentication: |
sudo vim /etc/ssh/sshd_config
Set or update the following directives:
PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM yes
🔹Restart SSH: |
sudo systemctl restart ssh
🔹Test Your Login |
Open a new terminal session and try logging in:
ssh username@your-server-ip
If successful, try the same without your private key (rename it temporarily) to confirm password access is blocked:
mv ~/.ssh/id_ed25519 ~/.ssh/id_ed25519.bak
ssh username@your-server-ip
You should see an authentication failure.
Why: Limiting IPs helps prevent unauthorized users from even reaching the login prompt. There are two common methods:
🔹Option 1: Use the UFW Firewall |
sudo ufw allow from YOUR.IP.ADDRESS to any port 22 && sudo ufw enable
Block all other SSH traffic:
sudo ufw default deny incoming
🔹Option 2: Use |
Add allowed IPs:
sudo vim /etc/hosts.allow
Replace with XXX.XXX.XX.XXX your IP address:
sshd: XXX.XXX.XX.XXX
Deny all others:
sudo vim /etc/hosts.deny
Add the following:
sshd: ALL
| Method | Pros | Cons |
|---|---|---|
| UFW | Easier to manage, logs blocked connections | Less granular, needs firewall awareness |
| hosts.allow | Native to SSH, doesn’t need firewall tools | Can be overridden by other access rules |
🔹Use a Non-Standard Port |
sudo vim /etc/ssh/sshd_config
Change default port (22) entry:
Port 2222
Update your firewall rules accordingly:
sudo vim /etc/ssh/sshd_config
🛑 NOTE: This won’t stop serious attackers but can reduce noise in your logs. |
🔹Limit User Logins |
AllowUsers username
🔹Enable Rate Limiting with |
Install fail2ban:
sudo apt install fail2ban
Create or edit jail config:
sudo vim /etc/fail2ban/jail.local
Example SSH section:
[sshd]
enabled = true
port = ssh
maxretry = 3
findtime = 10m
bantime = 1h
Restart fail2ban:
sudo systemctl restart fail2ban
🔹Common Fixes: |
| Issue | Fix |
|---|---|
| Locked out due to misconfig | Access via cloud provider console or local terminal |
Wrong permissions on .ssh/ | Ensure ~/.ssh is 700 and authorized_keys is 600 |
| UFW blocks your IP | Temporarily disable: sudo ufw disable |
| Fail2ban blocks IP | Unban IP: sudo fail2ban-client set sshd unbanip YOUR.IP.ADD.RESS |
Hardening SSH on Ubuntu 24.04 should be a priority, not an afterthought. By disabling root login, enforcing key authentication, and restricting access by IP, you significantly reduce the risk of unauthorized access. Combined with firewall rules, rate limiting, and basic user access controls, your server will be far more resilient against attack.
Security is not a one-time task—monitor logs, update regularly, and review access policies periodically. |
Did you find this article helpful? Your feedback is invaluable to us! Feel free to share this post with those who may benefit, and let us know your thoughts in the comments section below.

Discover the best practices for securing SSH connections on Linux servers. Learn essential security tips like disabling root login, using SSH keys, and more for

Learn about securing SSH connections on RHEL 9 and CentOS 9 with Ansible roles. This guide covers key SSH security practices, Ansible playbook setup, and

Learn how to list and secure your SSH MACs, Ciphers, and KexAlgorithms for enhanced security. This guide provides step-by-step instructions for checking and configuring these
