5 Phút Biến Server Thành Pháo Đài!

Comment: Bài này mượn của bác Nguyễn Anh Văn vì thấy hữu ích. Cá nhân mình dùng Debian, aapanel (quản lý site, php và nginx) và Docker cho các service như Manticore, Melisearch.

Dạo này nghe tin vài ngàn website bị hack mà run tay run chân? 😱 Shared hosting thì đành chịu, nhưng VPS thì khác – đây là lãnh thổ của bạn! Thế mà nhiều bạn vẫn để SSH như cửa nhà không khóa, password 123456… Hacker nhìn vào chỉ muốn cười!

Bài sau sẽ giúp anh em bảo mật vps ở múc cơ bản nhưng ko hề yếu.

5 phút đọc bài này = 1 năm yên tâm! 

 Checklist Bảo Mật Cơ Bản (Copy-Paste Là Xong!)

1.  Update Hệ Thống – Lỗi Cũ Là Cửa Cho Hacker

# Ubuntu/Debian
sudo apt update && sudo apt upgrade -y

# CentOS/AlmaLinux/RHEL
sudo dnf update -y

Tip: Đặt cronjob chạy hàng tuần cho khỏe! Hoặc có một số tool auto như unattended-upgrades (Ubuntu) hoặc dnf-automatic (AlmaLinux)

2.  Bye Bye Root Login – SSH Key Là Vua!

Tạo user mới (không ai hack được root nếu root không tồn tại ):

# Tạo user mới
sudo useradd -m -s /bin/bash your_username
sudo passwd your_username

# Thêm quyền sudo
# Ubuntu/Debian:
sudo usermod -aG sudo your_username
# CentOS/RHEL:
sudo usermod -aG wheel your_username

Tạo SSH Key (trên máy local):

# Tạo key pair
ssh-keygen -t rsa -b 4096 -C “your_email@example.com”

# Copy public key lên server
ssh-copy-id your_username@your_server_ip

Khóa cửa SSH cho chắc:

sudo nano /etc/ssh/sshd_config

Sửa các dòng sau:

PasswordAuthentication no
PermitRootLogin no
PubkeyAuthentication yes
ChallengeResponseAuthentication no
Port 2222 # Đổi port khác 22

sudo systemctl restart sshd

3.  Firewall – Tường Lửa Bảo Vệ Server

Ubuntu (UFW – dễ như ăn kẹo):

sudo ufw enable
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 2222/tcp # SSH port mới
sudo ufw allow 80/tcp # HTTP
sudo ufw allow 443/tcp # HTTPS

CentOS/AlmaLinux (Firewalld):

sudo systemctl enable firewalld
sudo systemctl start firewalld
sudo firewall-cmd –permanent –remove-service=ssh
sudo firewall-cmd –permanent –add-port=2222/tcp
sudo firewall-cmd –permanent –add-service=http
sudo firewall-cmd –permanent –add-service=https
sudo firewall-cmd –reload

4.  Swap – Cứu Tinh Khi RAM Cạn

Kiểm tra swap hiện tại:

sudo swapon –show
free -h

Tạo swap file (2GB ví dụ):

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
# Làm permanent
echo ‘/swapfile none swap sw 0 0’ | sudo tee -a /etc/fstab

5.  Fail2Ban – Thần Chắn Hack SSH

Cài đặt Fail2Ban:

# Ubuntu/Debian
sudo apt install fail2ban -y

# CentOS/AlmaLinux
sudo dnf install epel-release -y
sudo dnf install fail2ban -y

Cấu hình cho SSH:

sudo nano /etc/fail2ban/jail.local

[DEFAULT]
bantime = 3600 # Ban 1 giờ
findtime = 600 # Trong 10 phút
maxretry = 5 # Thử sai 5 lần
[sshd]
enabled = true
port = 2222 # Port SSH mới
logpath = /var/log/auth.log
maxretry = 3
bantime = 3600

sudo systemctl enable fail2ban
sudo systemctl start fail2ban

 Bonus: Level Up Cho Pro Player (phần này khuyến khích cho ae tìm hiểu sâu hơn chút, không cần thiết thì ko cần đụng)

 Kernel Hardening

sudo nano /etc/sysctl.conf

Thêm vào:

net.ipv4.tcp_syncookies = 1
net.ipv4.ip_forward = 0
net.ipv4.conf.all.rp_filter = 1
kernel.randomize_va_space = 2

 Filesystem Security

# Mount /tmp an toàn
sudo mount -o remount,noexec,nosuid,nodev /tmp

 Auto Updates

# Ubuntu
sudo apt install unattended-upgrades -y
sudo dpkg-reconfigure unattended-upgrades

# AlmaLinux
sudo dnf install dnf-automatic -y
sudo systemctl enable –now dnf-automatic.timer

 Tổng Kết: An Toàn Chỉ Trong 5 Phút!

✅ Update system

✅ SSH key + disable root

✅ Firewall + đổi port

✅ Swap cho đỡ treo

✅ Fail2Ban chống brute force

Nhớ nhé: Hacker như muỗi, phòng bệnh hơn chữa bệnh! 

P/S: Ai lười gõ lệnh thì để mình viết script auto setup, comment bên dưới nhé! 

 Bài hay thì share cho anh em cùng biết! Security is not a product, it’s a process!

Nguồn Nguyễn Anh Văn, link https://www.facebook.com/groups/vpshostingngon/posts/565908279610547

Update file gist bash script dùng để automate các việc trên: https://gist.github.com/yeungon/8e0b8d54bbb8812497cae620173bfb1a

Leave a Comment