SSH hardening on Ubuntu 24.04 means disabling password authentication, restricting root login, changing the default port, and using key-based access. This guide shows the exact sshd_config changes, how to test before restarting, and what to avoid.
Why SSH hardening matters
SSH is the most common entry point to a Linux server. Default configurations prioritize convenience over security: password authentication is enabled, root login may be allowed, and the service listens on the well-known port 22. Attackers scan for port 22 and attempt password guesses constantly.
Hardening SSH reduces the attack surface by requiring cryptographic keys, blocking direct root access, and moving the service off the default port.
Step 1: Generate or use existing SSH keys
Before changing any SSH configuration, ensure you have a working SSH key pair. If you do not have one:
ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519 -C [email protected]
Copy the public key to the server:
ssh-copy-id -i ~/.ssh/id_ed25519.pub ubuntu@your-server-ip
Test that you can log in with the key before proceeding. If key-based login fails, you will be locked out when password authentication is disabled.
Step 2: Edit /etc/ssh/sshd_config
Open the SSH daemon configuration file:
sudo nano /etc/ssh/sshd_config
Make the following changes. Add them at the end of the file or uncomment and modify existing lines:
# Disable password authentication
PasswordAuthentication no
# Disable root login directly
PermitRootLogin no
# Disable empty passwords
PermitEmptyPasswords no
# Use protocol 2 only (default on modern OpenSSH, but confirm)
Protocol 2
# Limit authentication attempts
MaxAuthTries 3
# Reduce login grace time
LoginGraceTime 30
# Disable X11 forwarding unless needed
X11Forwarding no
# Allow only specific users
AllowUsers ubuntu
Step 3: Change the SSH port (optional)
Moving SSH off port 22 reduces automated scan noise. It is security through obscurity, not real security, but it cuts down on log clutter:
# Change from 22 to a high port
Port 2222
If you change the port, update your firewall rules:
sudo ufw allow 2222/tcp
sudo ufw delete allow 22/tcp
And update your SSH client config:
# ~/.ssh/config
Host my-server
HostName your-server-ip
Port 2222
User ubuntu
IdentityFile ~/.ssh/id_ed25519
Step 4: Test the configuration before restarting
OpenSSH includes a configuration test command. Run it before restarting the service:
sudo sshd -t
If the command returns nothing, the configuration is valid. If it reports an error, fix it before proceeding.
Then restart SSH:
sudo systemctl restart ssh
Keep your current terminal session open. Open a new terminal and verify you can log in with your key before closing the original session. If the new connection fails, you can still fix the configuration from the original session.
Step 5: Verify key-based login works
ssh -i ~/.ssh/id_ed25519 ubuntu@your-server-ip
If you are prompted for a password, password authentication is still enabled. Check sshd_config and restart again.
Step 6: Configure UFW firewall
On Ubuntu 24.04, UFW is the default firewall. Allow only the SSH port you are using:
sudo ufw allow 2222/tcp
sudo ufw enable
sudo ufw status
What to avoid
- Never disable
PubkeyAuthentication. This is the only secure authentication method left after disabling passwords. - Never set
PermitRootLogin yes. Use a regular user withsudoinstead. - Do not use
AllowGroupsunless you understand group membership on your system. A misconfigured group can lock out all users. - Do not restart SSH without keeping an active session open. If the new configuration breaks SSH, you lose access to the server.
Useful DevStackIO Tools
Generate secure SSH configs and validate keys with our free tools:
- SSH Hardening Guide – Interactive SSH hardening checklist and config generator
- JSON Formatter – Format and validate SSH config JSON output
- YAML Formatter – Format and validate Ansible SSH playbooks
Related Reading
- SSH Key Permissions Too Open: Why SSH Rejects Your Key – Fix key permission problems
- systemd Service Hardening – Harden SSH systemd service
- Linux File Permissions Reference – Understand file permissions for SSH keys
- Docker Volumes vs Bind Mounts – Secure container storage
- eBPF for Security Monitoring – Detect container escapes and privilege escalation
Frequently Asked Questions
- What if I lock myself out after changing SSH config?
- Always keep your current terminal session open when restarting SSH. If locked out, use your cloud provider console (AWS EC2 Serial Console, DigitalOcean Droplet Console, Oracle Cloud Console) to access the server and revert changes.
- Should I use ed25519 or RSA keys?
- Ed25519 is preferred: smaller keys, faster operations, and no known practical attacks. RSA 4096-bit is acceptable for legacy compatibility.
- Does changing the SSH port actually improve security?
- It reduces automated scan noise in logs but is not a security control. Real security comes from key-based auth and disabling root/password login.
- How do I allow SSH access for multiple users?
- Use
AllowUsers user1 user2orAllowGroups ssh-usersand add users to that group. Test each user before closing your admin session.