ติดตั้ง Linux server เสร็จแล้ว ก็ไม่ได้แปลว่าปลอดภัยแล้ว ค่า default ของ distro ส่วนใหญ่ออกแบบมาเพื่อความสะดวกในการใช้งาน ไม่ใช่ security บทความนี้รวม checklist ที่ต้องทำหลังติดตั้ง server ใหม่ ครอบคลุมตั้งแต่ SSH ไปจนถึง kernel parameter
ทำตาม checklist นี้ก่อนเปิด service ใดๆ สู่ภายนอก โดยเฉพาะถ้าจะใช้ร่วมกับ Cloudflare Tunnel หรือ port forwarding
1. อัปเดตระบบก่อนเลย
# Ubuntu/Debian
sudo apt update && sudo apt upgrade -y && sudo apt autoremove -y
# RHEL/Rocky/AlmaLinux
sudo dnf update -y && sudo dnf autoremove -y
# รีบูตถ้ามี kernel update
sudo reboot
2. SSH Hardening
SSH เป็น attack surface อันดับหนึ่ง ต้องทำก่อนทำอย่างอื่น
สร้าง SSH Key บน Client
# บน client machine ของคุณ (ไม่ใช่ server)
ssh-keygen -t ed25519 -C "your-email@example.com"
# Copy public key ไป server
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server-ip
แก้ SSH Config
sudo nano /etc/ssh/sshd_config
# /etc/ssh/sshd_config
Port 2222 # เปลี่ยน port จาก 22 (security through obscurity แต่ลด noise)
Protocol 2
LoginGraceTime 30
PermitRootLogin no # ห้าม root login โดยตรง
StrictModes yes
MaxAuthTries 3 # ลองผิด 3 ครั้งก็ disconnect
MaxSessions 5
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication no # ปิด password login เด็ดขาด
PermitEmptyPasswords no
ChallengeResponseAuthentication no
UsePAM yes
X11Forwarding no # ปิด X11 forwarding
PrintMotd no
AcceptEnv LANG LC_*
AllowUsers your-username # อนุญาตแค่ user ที่กำหนด
# Algorithm ที่ปลอดภัย
KexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org
HostKeyAlgorithms ssh-ed25519
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com
MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com
# ตรวจสอบ config ก่อน restart
sudo sshd -t
# Restart SSH
sudo systemctl restart sshd
สำคัญ: อย่าปิด session เดิมก่อนทดสอบว่า login ด้วย key ได้ ถ้าทำ config ผิดจะล็อคตัวเองออก
3. Firewall ด้วย UFW
# ติดตั้ง UFW (Ubuntu มีมาแล้ว)
sudo apt install ufw -y
# Default policy: deny all incoming, allow all outgoing
sudo ufw default deny incoming
sudo ufw default allow outgoing
# เปิด port ที่จำเป็น (ใช้ port SSH ที่ตั้งไว้)
sudo ufw allow 2222/tcp comment 'SSH'
sudo ufw allow 80/tcp comment 'HTTP'
sudo ufw allow 443/tcp comment 'HTTPS'
# เปิดใช้ UFW
sudo ufw enable
# ดูสถานะ
sudo ufw status verbose
ใช้ nftables แทน UFW (สำหรับผู้ใช้ขั้นสูง)
sudo apt install nftables -y
# /etc/nftables.conf
table inet filter {
chain input {
type filter hook input priority 0; policy drop;
ct state established,related accept
iif lo accept
ip protocol icmp accept
ip6 nexthdr icmpv6 accept
tcp dport 2222 accept comment "SSH"
tcp dport { 80, 443 } accept comment "Web"
}
chain forward {
type filter hook forward priority 0; policy drop;
}
chain output {
type filter hook output priority 0; policy accept;
}
}
sudo systemctl enable nftables
sudo systemctl start nftables
4. Fail2ban
Fail2ban monitor log และ ban IP ที่ login ผิดซ้ำๆ
sudo apt install fail2ban -y
# สร้าง local config (อย่าแก้ไฟล์ .conf โดยตรง จะถูก overwrite เมื่ออัปเดต)
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo nano /etc/fail2ban/jail.local
[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 3
banaction = ufw
[sshd]
enabled = true
port = 2222
logpath = /var/log/auth.log
maxretry = 3
sudo systemctl enable fail2ban
sudo systemctl start fail2ban
# ดู banned IPs
sudo fail2ban-client status sshd
# Unban IP (ถ้า ban ตัวเอง)
sudo fail2ban-client set sshd unbanip 1.2.3.4
5. การจัดการ User
# สร้าง user สำหรับ admin แยกจาก root
sudo adduser adminuser
sudo usermod -aG sudo adminuser
# ล็อค root account
sudo passwd -l root
# ดู user ทั้งหมดที่มี shell
grep -v '/sbin/nologin\|/bin/false' /etc/passwd
# ลบ user ที่ไม่จำเป็น (ถ้ามี)
sudo userdel -r unused-user
# ตรวจสอบ user ที่มี sudo
getent group sudo
6. File Permissions
# ตรวจหาไฟล์ที่ world-writable
sudo find / -xdev -type f -perm -o+w 2>/dev/null | grep -v proc
# ตรวจหา SUID/SGID files
sudo find / -xdev \( -perm -4000 -o -perm -2000 \) -type f 2>/dev/null
# ตั้ง permission ไฟล์สำคัญ
sudo chmod 600 /etc/shadow
sudo chmod 644 /etc/passwd
sudo chmod 644 /etc/group
sudo chmod 700 /root
sudo chmod 700 /home/adminuser/.ssh
sudo chmod 600 /home/adminuser/.ssh/authorized_keys
7. Automatic Security Updates
sudo apt install unattended-upgrades -y
sudo dpkg-reconfigure unattended-upgrades
# แก้ config
sudo nano /etc/apt/apt.conf.d/50unattended-upgrades
Unattended-Upgrade::Allowed-Origins {
"${distro_id}:${distro_codename}";
"${distro_id}:${distro_codename}-security";
"${distro_id}ESMApps:${distro_codename}-apps-security";
"${distro_id}ESM:${distro_codename}-infra-security";
};
Unattended-Upgrade::AutoFixInterruptedDpkg "true";
Unattended-Upgrade::MinimalSteps "true";
Unattended-Upgrade::Remove-Unused-Kernel-Packages "true";
Unattended-Upgrade::Remove-Unused-Dependencies "true";
Unattended-Upgrade::Automatic-Reboot "false"; // อย่าให้ reboot อัตโนมัติ
Unattended-Upgrade::Mail "your@email.com";
8. Kernel Parameters
sudo nano /etc/sysctl.d/99-security.conf
# Network security
net.ipv4.ip_forward = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.all.accept_source_route = 0
net.ipv6.conf.all.accept_redirects = 0
net.ipv4.conf.all.log_martians = 1
net.ipv4.conf.default.log_martians = 1
net.ipv4.icmp_echo_ignore_broadcasts = 1
net.ipv4.icmp_ignore_bogus_error_responses = 1
net.ipv4.tcp_syncookies = 1
# Kernel security
kernel.randomize_va_space = 2
kernel.dmesg_restrict = 1
kernel.kptr_restrict = 2
fs.protected_hardlinks = 1
fs.protected_symlinks = 1
sudo sysctl --system
9. AppArmor
Ubuntu เปิด AppArmor มาแล้วโดยค่าเริ่มต้น ตรวจสอบสถานะและเพิ่ม profile ถ้าจำเป็น
# ดูสถานะ AppArmor
sudo apparmor_status
# เปิดใช้ AppArmor (ถ้ายังไม่ได้เปิด)
sudo systemctl enable apparmor
sudo systemctl start apparmor
# ดู profile ทั้งหมด
sudo aa-status
# ตรวจหา violation
sudo journalctl -xe | grep apparmor
สำหรับ RHEL-based ใช้ SELinux แทน
# ตรวจสอบ SELinux status
getenforce
# ถ้าเป็น Permissive ให้เปลี่ยนเป็น Enforcing
sudo setenforce 1
sudo sed -i 's/SELINUX=permissive/SELINUX=enforcing/' /etc/selinux/config
# ดู SELinux denials
sudo ausearch -m avc -ts recent
10. Audit Logging
sudo apt install auditd audispd-plugins -y
# เพิ่ม rules สำหรับ monitor การเปลี่ยนแปลงสำคัญ
sudo nano /etc/audit/rules.d/hardening.rules
-w /etc/passwd -p wa -k identity
-w /etc/shadow -p wa -k identity
-w /etc/sudoers -p wa -k sudoers
-w /etc/ssh/sshd_config -p wa -k sshd
-w /var/log/auth.log -p wa -k auth_log
-a always,exit -F arch=b64 -S execve -k exec_commands
-a always,exit -F arch=b64 -S open -F dir=/etc -F success=0 -k etc_access_failure
sudo systemctl enable auditd
sudo systemctl restart auditd
# ดู audit log
sudo ausearch -k identity
sudo aureport -au # authentication report
11. Network Hardening เพิ่มเติม
# ปิด service ที่ไม่จำเป็น
sudo systemctl list-units --type=service --state=running
# ปิด service ที่ไม่ต้องการ
sudo systemctl disable --now bluetooth avahi-daemon cups
# ตรวจสอบ listening ports
ss -tlnup
# หรือ
sudo netstat -tlnup
12. CIS Benchmark
CIS (Center for Internet Security) มี benchmark สำหรับ Linux distro ต่างๆ ที่เป็น checklist มาตรฐาน ใช้ lynis เพื่อ audit ว่า server ผ่าน benchmark แค่ไหน
sudo apt install lynis -y
# Run audit
sudo lynis audit system
# ดู score และ recommendations
# Hardening index จะอยู่ตอนท้ายของ output
Lynis จะให้ hardening index (0-100) และ list สิ่งที่ควรแก้ไข เป้าหมายควรได้คะแนนอย่างน้อย 75
Quick Hardening Script
รวม step สำคัญเป็น script เดียว
#!/bin/bash
# quick-harden.sh - รันหลังติดตั้ง server ใหม่
set -e
echo "[*] Updating system..."
apt update && apt upgrade -y
echo "[*] Installing security tools..."
apt install -y ufw fail2ban unattended-upgrades auditd lynis
echo "[*] Configuring UFW..."
ufw default deny incoming
ufw default allow outgoing
ufw allow 2222/tcp
ufw --force enable
echo "[*] Configuring fail2ban..."
systemctl enable fail2ban
systemctl start fail2ban
echo "[*] Applying kernel hardening..."
cat > /etc/sysctl.d/99-security.conf << 'EOF'
net.ipv4.tcp_syncookies = 1
net.ipv4.conf.all.accept_redirects = 0
kernel.randomize_va_space = 2
kernel.dmesg_restrict = 1
EOF
sysctl --system
echo "[*] Done! Run 'lynis audit system' to check hardening score"
สำหรับ Docker service ที่รันบน server นี้ ดูเพิ่มเติมที่ Docker Compose สำหรับ Home Lab และถ้าจะ setup Proxmox ดูที่ Proxmox Cluster Setup 2026
คำถามที่พบบ่อย (FAQ)
ต้องทำ hardening ทุกข้อไหม?
ขึ้นอยู่กับ use case ถ้าเป็น server ที่ expose internet ทำให้ครบที่สุดเท่าที่ทำได้ ถ้าเป็น server ใน LAN ที่ปิดอยู่ข้างในอย่างน้อยทำ SSH hardening, firewall และ fail2ban ก่อน ส่วน audit logging และ AppArmor อาจ skip ได้สำหรับ Home Lab
ทำ Password Authentication ปิดไป แล้วลืม copy SSH key จะทำยังไง?
ต้องเข้าผ่าน console โดยตรง (KVM, IPMI, หน้าจอ) หรือถ้าเป็น VPS ใช้ VNC/Serial console ของผู้ให้บริการ แก้ไข sshd_config ชั่วคราวให้ allow password แล้ว copy key ใหม่ นั่นคือเหตุผลที่ต้องทดสอบ key login ก่อนปิด password
Fail2ban block IP ของตัวเองทำยังไง?
ใช้คำสั่ง sudo fail2ban-client set sshd unbanip IP_ADDRESS และเพิ่ม IP ของตัวเองใน ignoreip ที่ jail.local เช่น ignoreip = 127.0.0.1/8 ::1 192.168.1.0/24
ต้องรัน lynis บ่อยแค่ไหน?
รัน lynis ทุกครั้งที่ติดตั้ง service ใหม่หรือแก้ config หลัก และอย่างน้อยเดือนละครั้งสำหรับ server ที่ expose internet ผล scan จะบอก regression ถ้ามีอะไรเปลี่ยน
SELinux vs AppArmor ต่างกันยังไง?
ทั้งคู่เป็น Mandatory Access Control (MAC) system SELinux พัฒนาโดย NSA ใช้ label-based policy ซับซ้อนกว่าแต่ flexible กว่า ใช้บน RHEL/CentOS AppArmor ใช้ path-based policy เรียนรู้ง่ายกว่า ใช้บน Ubuntu/Debian ถ้าเลือกได้ ใช้สิ่งที่มากับ distro นั้นจะดีกว่า
Automatic reboot หลัง kernel update ควร enable ไหม?
สำหรับ production server ไม่ควร enable เพราะ reboot ที่ไม่ได้วางแผน service จะ down ควรตั้ง maintenance window และ reboot manual หรือใช้ kexec สำหรับบาง distro สำหรับ Home Lab อาจ enable ได้ถ้าไม่กังวล downtime
