Beginner 2 min read

๐Ÿ” 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