How to Secure SSH on a Linux Server (Beginner-Friendly Hardening Guide)
👉 Securing SSH — the main gateway into your server.
If your SSH is wide open, your server is basically standing on the street with the door unlocked.
Lucky for you, securing SSH isn’t difficult. You just need some guidance — and that’s what this tutorial is for.
Let’s walk through it step by step, in a very beginner-friendly way.
🛠️ Step 1: Update Your System First
Always a good first move:
This ensures your server has the latest security patches before you touch SSH.
🔑 Step 2: Create a New User (Don’t Use Root!)
Using the root account is dangerous — if someone cracks it, they own your server.
Let’s create a safer, normal user:
Give it a password, confirm details.
Now add it to sudo group:
This user will replace root for login purposes.
🔐 Step 3: Set Up SSH Key Authentication (More Secure Than Password)
SSH keys are like ultra-strong digital keys.
Even a powerful hacker can't brute-force them easily.
On your local machine (not the server):
Press Enter for defaults.
Now move your public key to the server:
Or manually:
Test login:
If it logs in without asking for a password — success!
🚪 Step 4: Disable Root Login
This is one of the most important security steps.
Open your SSH config:
Find this line:
Change to:
If the line doesn’t exist, add it at the bottom.
Restart SSH:
Now SSH root login is locked.
Hackers hate this step. Good job.
🧱 Step 5: Change the SSH Port (Optional But Recommended)
Bots constantly scan port 22.
Changing it adds a simple but effective layer of security.
Open SSH config again:
Find:
Change it to something uncommon:
Allow this port in UFW:
Then restart SSH:
From now on, connect like this:
🧹 Step 6: Disable Password Authentication (Only Keys Allowed)
This completely stops brute-force attacks.
Edit SSH config:
Find:
Change to:
Restart SSH:
Now only SSH keys can log in.
Very strong protection.
🛡️ Step 7: Use Fail2Ban to Ban Attackers
Fail2Ban watches for repeated failed login attempts and blocks attackers automatically.
Install it:
Start and enable:
Check status:
To protect SSH even better, create a local jail:
Add:
Restart Fail2Ban:
Now attackers will be automatically banned.
🧪 Step 8: Test Everything (Very Important!)
Try logging in from another SSH session before closing your active one.
Test:
✔ Login with new user
✔ Key authentication
✔ Custom SSH port
✔ Root login blocked
✔ Password login disabled
If all works — your server is now significantly more secure than before.
🧩 Common Problems and Fixes
❌ Can’t login after changing SSH port?
You forgot to allow the port in UFW:
❌ SSH says “Permission denied (publickey)”?
Make sure the permissions are correct:
❌ Fail2Ban not banning?
Check logs:
🎉 Conclusion
In this tutorial, you learned how to:
✔ Create a safe user
✔ Set up SSH key authentication
✔ Disable root login
✔ Change the SSH port
✔ Disable password login
✔ Use Fail2Ban
✔ Protect your server from brute-force attacks
Securing SSH is one of the best investments you can make in your server’s safety.
Your server is now significantly harder to break into — and you gained real sysadmin skills.