🔐 Secure Your Linux Server (Beginner Guide)

Learn how to protect your server from hackers using simple steps 🚀


📌 What You Will Learn

  • Why server security is important
  • Basic protection steps
  • Real-world setup (OCI / VPS / Ubuntu)

⚠️ Why Security Matters?

If your server is open: - Hackers can access your data 😨 - Your server can be used for attacks - Your project can go down anytime

👉 Securing your server is must, not optional!


🧰 Step 1: Update Your System

sudo apt update && sudo apt upgrade -y

✔ Fixes security vulnerabilities
✔ Keeps system up to date


🔥 Step 2: Enable Firewall (UFW)

UFW = Uncomplicated Firewall

sudo apt install ufw -y
sudo ufw allow OpenSSH
sudo ufw enable

Check status:

sudo ufw status

👉 Blocks unwanted traffic


🔑 Step 3: Disable Root Login

Edit SSH config:

sudo nano /etc/ssh/sshd_config

Find:

PermitRootLogin yes

Change to:

PermitRootLogin no

Restart SSH:

sudo systemctl restart ssh

👉 Root login is a major security risk


🔐 Step 4: Use SSH Key Authentication

Generate key (on local PC):

ssh-keygen

Copy key to server:

ssh-copy-id username@your_server_ip

👉 Login without password (more secure)


🚫 Step 5: Disable Password Login

Edit config again:

sudo nano /etc/ssh/sshd_config

Find:

PasswordAuthentication yes

Change:

PasswordAuthentication no

Restart SSH:

sudo systemctl restart ssh

👉 Only SSH key login allowed 🔒


🧠 Step 6: Change Default SSH Port (Optional)

Change port:

Port 2222

Allow in firewall:

sudo ufw allow 2222
sudo systemctl restart ssh

👉 Adds extra protection


🛡️ Step 7: Install Fail2Ban

sudo apt install fail2ban -y
sudo systemctl enable fail2ban

Check status:

sudo systemctl status fail2ban

👉 Protects from brute-force attacks


❌ Common Errors & Fixes

SSH not working after changes

sudo ufw allow 22

👉 Ensure SSH port is allowed


Locked out of server

👉 Always keep one session open before changes


SSH key not working

chmod 600 ~/.ssh/authorized_keys

🎯 Final Result

  • Your server is secure 🔒
  • Reduced hack attempts
  • Safe for hosting projects

💡 Pro Tip

Never expose your server without: - Firewall
- SSH key
- Disabled root login

👉 These are basic DevOps rules