Checklist 30 phút để secure Ubuntu server ngay sau khi cài mới
Server Ubuntu mới cài giống căn nhà vừa nhận chìa khóa: sạch, trống, chạy được — nhưng chưa khóa cửa, chưa gắn camera, chưa tắt lối phụ. Nếu đưa thẳng lên Internet, bot quét SSH, brute-force, khai thác CVE → xảy ra trong vài phút.
Bài này là checklist thực dụng, làm trong ~30 phút, ưu tiên giảm rủi ro lớn nhất: user, SSH, firewall, update, log, service, hardening nền.
Mục tiêu: server Ubuntu an toàn “đủ tốt” sau cài mới, trước khi deploy app.
0–3 phút: Cập nhật hệ thống ngay
Bản cài ISO thường lỗi thời. Update trước → vá CVE nền.
sudo apt update
sudo apt upgrade -y
sudo apt autoremove -yNếu kernel/libc/OpenSSL được cập nhật → reboot.
sudo rebootSau khi vào lại:
uptime
uname -a3–7 phút: Tạo user thường, khóa root login
Không vận hành bằng root. Tạo user có sudo.
adduser deploy
usermod -aG sudo deployKiểm tra:
su - deploy
sudo whoamiKết quả cần:
rootKhóa mật khẩu root:
sudo passwd -l root7–12 phút: Secure SSH
SSH là cửa chính. Bot sẽ scan port 22 liên tục. Hardening SSH → ưu tiên cao.
Tạo SSH key trên máy cá nhân
Trên laptop/dev machine:
ssh-keygen -t ed25519 -C "deploy@server"Copy key lên server:
ssh-copy-id deploy@SERVER_IPTest login key:
ssh deploy@SERVER_IPChỉ tiếp tục khi login bằng key OK.
Chỉnh cấu hình SSH
Mở file:
sudo nano /etc/ssh/sshd_configThiết lập:
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
MaxAuthTries 3
LoginGraceTime 30
X11Forwarding no
AllowUsers deployNếu muốn đổi port SSH, ví dụ 2222:
Port 2222Kiểm tra config trước khi reload:
sudo sshd -tReload:
sudo systemctl reload sshTest terminal mới, không đóng terminal cũ:
ssh -p 2222 deploy@SERVER_IP12–16 phút: Bật firewall UFW
Firewall mặc định nên deny inbound, allow outbound.
sudo ufw default deny incoming
sudo ufw default allow outgoingAllow SSH. Nếu dùng port 22:
sudo ufw allow OpenSSHNếu đổi port 2222:
sudo ufw allow 2222/tcpBật firewall:
sudo ufw enable
sudo ufw status verboseKhi deploy web:
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp16–19 phút: Cài Fail2ban chống brute-force
Fail2ban đọc log auth, thấy login fail nhiều → ban IP.
sudo apt install fail2ban -y
sudo systemctl enable --now fail2banTạo cấu hình local:
sudo nano /etc/fail2ban/jail.localNội dung cơ bản:
[sshd]
enabled = true
port = 2222
filter = sshd
logpath = %(sshd_log)s
maxretry = 3
findtime = 10m
bantime = 1hNếu SSH port 22 → port = ssh.
Restart:
sudo systemctl restart fail2ban
sudo fail2ban-client status sshd19–22 phút: Bật auto security update
Server chạy lâu → quên update → nguy hiểm. Bật unattended upgrades cho bản vá security.
sudo apt install unattended-upgrades apt-listchanges -y
sudo dpkg-reconfigure unattended-upgradesChọn Yes.
Kiểm tra file:
sudo nano /etc/apt/apt.conf.d/50unattended-upgradesĐảm bảo security repo được bật. Có thể bật auto reboot khi cần:
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "03:00";22–24 phút: Tắt service không cần
Liệt kê service đang listen port:
sudo ss -tulpnLiệt kê service enabled:
systemctl list-unit-files --type=service --state=enabledGỡ/tắt thứ không dùng. Ví dụ không dùng snap:
sudo systemctl disable --now snapdKhông dùng Apache vì dùng Nginx:
sudo systemctl disable --now apache224–26 phút: Cấu hình timezone, hostname, time sync
Log đúng giờ → điều tra sự cố nhanh.
sudo timedatectl set-timezone Asia/Ho_Chi_Minh
timedatectlĐặt hostname rõ nghĩa:
sudo hostnamectl set-hostname prod-web-01Kiểm tra time sync:
timedatectl statusNếu chưa có:
sudo apt install chrony -y
sudo systemctl enable --now chrony26–28 phút: Bật log, audit cơ bản
Ubuntu có journalctl. Kiểm tra auth log:
sudo journalctl -u ssh --since "1 hour ago"Hoặc:
sudo tail -f /var/log/auth.logCài auditd nếu cần audit command/system event:
sudo apt install auditd audispd-plugins -y
sudo systemctl enable --now auditdXem event:
sudo ausearch -m USER_LOGIN– SSH login fail/success – sudo usage – service restart bất thường – port mới mở – disk gần đầy – CPU/RAM spike
28–30 phút: Snapshot, backup, kiểm tra cuối
Sau khi hardening xong → snapshot ngay trên cloud/VPS provider. Đây là điểm rollback sạch.
Checklist kiểm tra nhanh
sudo ufw status verbose
sudo fail2ban-client status
sudo systemctl status ssh
sudo ss -tulpn
sudo apt updateKiểm tra SSH password đã tắt:
ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no deploy@SERVER_IPKết quả mong muốn: login bằng password bị từ chối.
Backup tối thiểu
Ít nhất backup:
– /etc
– app config
– DB dump
– SSH authorized_keys
– firewall rules
– cron/systemd timers
Ví dụ:
sudo tar czf /root/etc-backup-$(date +%F).tar.gz /etcLưu backup ngoài server. Backup cùng server → mất server là mất luôn.
Checklist tóm tắt 30 phút
– Update OS → vá lỗi nền. – Tạo user sudo → không dùng root. – SSH key-only → tắt password login. – Disable root SSH → chặn target phổ biến. – UFW deny inbound → chỉ mở port cần. – Fail2ban → giảm brute-force. – Auto security update → giảm quên vá. – Tắt service dư → giảm attack surface. – Time/log chuẩn → dễ audit. – Snapshot/backup → có đường lui.
Các lỗi phổ biến cần tránh
Tắt password SSH trước khi test key
Sai key → mất quyền truy cập. Luôn test session mới trước.
Mở toàn bộ port vì “debug”
ufw allow 0:65535/tcp → tự phá firewall. Chỉ allow port cụ thể.
Dùng mật khẩu yếu cho sudo user
SSH key không giúp nếu console/VNC/provider panel bị lộ. Password user vẫn cần mạnh.
Không backup
Security không chỉ chống hacker. Sai config, xóa nhầm, update lỗi → cũng là rủi ro.
Quên monitoring
Secure không phải làm 1 lần. Cần biết khi có login lạ, disk đầy, service chết.
Kết luận thực tế
30 phút đầu sau khi cài Ubuntu quyết định phần lớn mức độ an toàn ban đầu. Không cần công cụ phức tạp: update, user riêng, SSH key-only, firewall, Fail2ban, auto update, tắt service dư, snapshot. Những bước này chặn phần lớn bot scan, brute-force, lỗi cấu hình phổ biến.
Sau checklist này, server chưa “bất khả xâm phạm”, nhưng đã qua mức nguy hiểm nhất: mặc định trần trụi trên Internet. Bước tiếp theo: hardening theo workload — Nginx/TLS, DB private network, app secrets, container security, monitoring, alerting, backup định kỳ, incident response.
Bình luận (0)
Chưa có bình luận. Hãy là người đầu tiên!