ตรวจสอบความปลอดภัย VPS ด้วย Lynis Security Audit Tool
Security Linux Audit 23 กันยายน 2569

ตรวจสอบความปลอดภัย VPS ด้วย Lynis — Security Auditing Tool

หลายครั้งที่เราตั้งค่า VPS แล้วคิดว่าปลอดภัยพอแล้ว แต่ในความเป็นจริง ยังมีช่องโหว่เล็กๆ น้อยๆ ที่หลบซ่อนอยู่ในการตั้งค่าที่ถูกมองข้าม — permission ที่หลวมเกิน, service ที่เปิดทิ้งไว้โดยไม่จำเป็น, หรือ SSL/TLS ที่ใช้ cipher เก่า

Lynis คือ open-source security auditing tool ที่สแกนระบบ Linux/Unix ของคุณและให้คะแนน Hardening Index พร้อมรายการคำแนะนำ hardening แบบละเอียด ทีม security ระดับองค์กรใช้ Lynis เป็นส่วนหนึ่งของ compliance workflow มาตั้งแต่ปี 2007

Lynis คืออะไร และต่างจาก Vulnerability Scanner อย่างไร

Lynis เป็น security auditing tool ไม่ใช่ vulnerability scanner แบบ Nessus หรือ OpenVAS ความต่างคือ:

Lynis (Audit) Vulnerability Scanner
รันบนเครื่องเป้าหมายโดยตรง (local) สแกนจากภายนอกผ่านเครือข่าย
ตรวจ config, permission, best practice ตรวจ CVE และ exploit ที่รู้จัก
ให้คำแนะนำ hardening เฉพาะระบบ รายงาน severity ของช่องโหว่
ไม่ต้องใช้ network — รัน local ได้ ต้องการ network access

ใช้ Lynis ร่วมกับ vulnerability scanner ได้เป็นอย่างดี — Lynis ช่วย harden configuration ก่อน แล้วจึงสแกนด้วย Nessus เพื่อหา CVE

ติดตั้ง Lynis

Ubuntu / Debian

# ติดตั้งจาก official Lynis repo (แนะนำ — ได้เวอร์ชันล่าสุดเสมอ)
curl -fsSL https://packages.cisofy.com/keys/cisofy-software-public.key | sudo gpg --dearmor -o /usr/share/keyrings/cisofy-software-public.gpg
echo "deb [arch=amd64,arm64 signed-by=/usr/share/keyrings/cisofy-software-public.gpg] https://packages.cisofy.com/community/lynis/deb/ stable main" | sudo tee /etc/apt/sources.list.d/cisofy-lynis.list
sudo apt update
sudo apt install lynis -y

# ตรวจสอบเวอร์ชัน
lynis show version

CentOS / AlmaLinux / Rocky Linux

sudo rpm --import https://packages.cisofy.com/keys/cisofy-software-public.key
cat > /etc/yum.repos.d/cisofy-lynis.repo << 'EOF'
[lynis]
name=CISOfy Software - Lynis package
baseurl=https://packages.cisofy.com/community/lynis/rpm/
enabled=1
gpgcheck=1
gpgkey=https://packages.cisofy.com/keys/cisofy-software-public.key
priority=2
EOF
sudo yum install lynis -y

ติดตั้งแบบ Git (รุ่นล่าสุดเสมอ)

cd /opt
sudo git clone https://github.com/CISOfy/lynis
cd lynis
sudo ./lynis audit system

รัน Security Audit ครั้งแรก

คำสั่งพื้นฐานในการ audit ระบบ:

# รัน full system audit (ต้องใช้ root)
sudo lynis audit system

# รันแบบเงียบ — แสดงเฉพาะ warning/suggestion
sudo lynis audit system --quiet

# บันทึก report ลงไฟล์
sudo lynis audit system --report-file /tmp/lynis-report-$(date +%Y%m%d).dat

Lynis จะสแกนผ่าน category ต่างๆ เช่น Boot/Services, Filesystem, Users, Networking, Logging, Malware, Authentication, SSH, Firewall, Cryptography และอื่นๆ การสแกนใช้เวลาประมาณ 1-3 นาที

ทำความเข้าใจผล Audit

หลังสแกนเสร็จ Lynis จะแสดงสรุปที่สำคัญ:

================================================================================
  Lynis security scan details:

  Hardening index : 68 [##############      ]
  Tests performed : 242
  Plugins enabled : 2

  Components:
  - Firewall               [V]
  - Malware scanner        [X]

  Lynis Modules:
  - Compliance Status      [?]
  - Security Audit         [V]
  - Vulnerability Scan     [V]

  Files:
  - Test and debug information      : /var/log/lynis.log
  - Report data                     : /var/log/lynis-report.dat
================================================================================

Hardening Index คือคะแนนรวม 0-100 — ระบบที่เพิ่ง setup มักได้ 50-65 เป้าหมายที่ดีคือ 75+ สำหรับ production server

อ่าน Warning และ Suggestion

# ดู warning ทั้งหมดจาก report
grep "^warning\[]" /var/log/lynis-report.dat

# ดู suggestion ทั้งหมด
grep "^suggestion\[]" /var/log/lynis-report.dat

# ดูด้วย lynis show (ถ้า version รองรับ)
sudo lynis show warnings
sudo lynis show suggestions

ตัวอย่าง warning ที่พบบ่อย:

Warning ID ปัญหา วิธีแก้เบื้องต้น
AUTH-9262 password aging ไม่ได้ตั้ง chage -M 90 username
SSH-7408 SSH PermitRootLogin เปิดอยู่ ปิดใน /etc/ssh/sshd_config
FIRE-4512 ไม่มี firewall ที่ active เปิด UFW หรือ nftables
KRNL-5820 kernel ค้างเก่า ต้อง reboot reboot หลัง update
MALW-3280 ไม่มี malware scanner ติดตั้ง ClamAV หรือ rkhunter

Hardening ตาม Suggestion ของ Lynis

ปิด Root Login SSH

sudo sed -i 's/^#*PermitRootLogin.*/PermitRootLogin no/' /etc/ssh/sshd_config
sudo systemctl restart sshd

ตั้งค่า Password Aging

# ตั้งค่า default ใน /etc/login.defs
sudo sed -i 's/^PASS_MAX_DAYS.*/PASS_MAX_DAYS   90/' /etc/login.defs
sudo sed -i 's/^PASS_MIN_DAYS.*/PASS_MIN_DAYS   7/' /etc/login.defs
sudo sed -i 's/^PASS_WARN_AGE.*/PASS_WARN_AGE   14/' /etc/login.defs

เปิด Process Accounting

sudo apt install acct -y   # Debian/Ubuntu
sudo systemctl enable acct --now

เพิ่ม Legal Banner

echo "Authorized access only. Activity is monitored." | sudo tee /etc/issue.net
sudo sed -i 's/^#*Banner.*/Banner \/etc\/issue.net/' /etc/ssh/sshd_config
sudo systemctl restart sshd

สแกนเฉพาะ Category

# สแกนเฉพาะ SSH
sudo lynis audit system --tests-from-group ssh

# สแกนเฉพาะ authentication
sudo lynis audit system --tests-from-group authentication

# ดูรายการ group ทั้งหมด
sudo lynis show groups

# รันเฉพาะ test ID ที่ต้องการ
sudo lynis audit system --tests AUTH-9262,SSH-7408

ตั้ง Cron รัน Lynis อัตโนมัติทุกสัปดาห์

# สร้าง script สำหรับ automated scan
cat > /etc/cron.weekly/lynis-audit << 'EOF'
#!/bin/bash
REPORT_DIR="/var/log/lynis-reports"
mkdir -p "$REPORT_DIR"
DATE=$(date +%Y%m%d)
sudo lynis audit system --cronjob \
  --report-file "$REPORT_DIR/lynis-report-$DATE.dat" \
  > "$REPORT_DIR/lynis-log-$DATE.txt" 2>&1
# เก็บ report ล่าสุด 30 วัน
find "$REPORT_DIR" -mtime +30 -delete
EOF
chmod +x /etc/cron.weekly/lynis-audit
Tip: flag --cronjob จะปิด color output และ interactive prompt ทำให้เหมาะสำหรับรันแบบ automated — ผล report จะอยู่ในไฟล์ที่กำหนด

เปรียบเทียบ Report ระหว่าง Scan

# ดู hardening index ของ report แต่ละครั้ง
grep "hardening_index" /var/log/lynis-reports/lynis-report-*.dat

# output ตัวอย่าง:
# hardening_index=68  (2026-09-01)
# hardening_index=72  (2026-09-08)
# hardening_index=76  (2026-09-15)

การที่ Hardening Index เพิ่มขึ้นแต่ละสัปดาห์แสดงว่าการ hardening ของคุณได้ผล เป้าหมายระยะยาวคือ 80+ สำหรับ production server ที่จัดเก็บข้อมูลสำคัญ


ตรวจสอบว่า Lynis ทำงานถูกต้อง

# 1. ตรวจสอบ version และ database
lynis show version
lynis show dbdir

# 2. ตรวจสอบ update ของ Lynis
sudo lynis update info

# 3. รัน quick check ดู warning เท่านั้น
sudo lynis audit system --quick 2>&1 | grep -E "Warning|Suggestion|Hardening"

# 4. ตรวจสอบ log ว่าสแกนสมบูรณ์
sudo tail -5 /var/log/lynis.log

# 5. ดู hardening index ล่าสุด
grep "hardening_index" /var/log/lynis-report.dat
Tip: ถ้า Hardening Index ต่ำกว่า 60 ให้เริ่มจาก warning ก่อน (มักมีผลกระทบมากกว่า suggestion) และ fix ทีละอัน แล้วรัน scan ใหม่เพื่อดูความคืบหน้า

แก้ปัญหาที่พบบ่อย

Error: "Lynis requires root privileges"

Lynis ต้องรันด้วย root เพื่อตรวจ file permission และ config ที่จำกัดการเข้าถึง

# รันด้วย sudo
sudo lynis audit system

# หรือ switch เป็น root ก่อน
sudo su -
lynis audit system

Error: "No tests were performed" หรือ scan หยุดกลางคัน

มักเกิดจาก Lynis version เก่าหรือ database ไม่สมบูรณ์

# อัปเดต Lynis
sudo apt update && sudo apt upgrade lynis -y  # Debian/Ubuntu
sudo yum update lynis -y  # RHEL/CentOS

# ถ้าใช้ Git version
cd /opt/lynis && sudo git pull

# ตรวจสอบความสมบูรณ์ของ database
sudo lynis show dbdir
ls -la $(sudo lynis show dbdir)

Error: "File permissions are not set correctly for /etc/lynis/custom.prf"

Lynis ตรวจสอบ permission ของ config file เองด้วย — ต้องให้เป็น 640 หรือ 600

sudo chmod 640 /etc/lynis/custom.prf
sudo chown root:root /etc/lynis/custom.prf

Warning จำนวนมากเกี่ยวกับ kernel parameters

Lynis แนะนำให้ตั้งค่า sysctl หลายตัว — แต่บางค่าอาจ conflict กับ application ให้ทดสอบใน staging ก่อนเสมอ

# ดู kernel suggestion จาก Lynis
grep "KRNL\|sysctl" /var/log/lynis-report.dat | grep "suggestion\|warning"

# ทดสอบ sysctl แบบ temporary ก่อน
sudo sysctl -w net.ipv4.conf.all.accept_redirects=0
# ถ้าไม่มีปัญหาค่อย persist ใน /etc/sysctl.d/99-lynis.conf

Lynis รัน cron แล้วไม่มี report

ตรวจสอบว่า cron script มีสิทธิ์ execute และ path ของ lynis ถูกต้อง

# ตรวจสอบว่า lynis อยู่ใน PATH ของ cron
which lynis

# ใช้ full path ใน cron script
/usr/bin/lynis audit system --cronjob

# ตรวจสอบ cron mail สำหรับ error
sudo grep "lynis" /var/mail/root 2>/dev/null || sudo journalctl -u cron | grep lynis

คำถามที่พบบ่อย

Lynis ใช้ได้กับ distro ไหนบ้าง?

Lynis รองรับ Linux distribution ทุกตัวที่ใช้งานทั่วไป ทั้ง Ubuntu, Debian, CentOS, AlmaLinux, Rocky Linux, Fedora, Arch Linux รวมถึง macOS และ FreeBSD การ audit บางส่วนจะปรับตาม distro อัตโนมัติ — เช่น test SSH config จะตรวจ path ที่ถูกต้องตาม distro นั้นๆ

Hardening Index 70 ถือว่าผ่านหรือไม่?

ไม่มีค่า "pass/fail" ที่ตายตัว — ขึ้นอยู่กับ use case: server ทั่วไปควรมีค่า 65+ VPS ที่จัดเก็บข้อมูล sensitive ควรมี 75+ และ server ที่ต้องการ compliance (PCI-DSS, HIPAA) ควรมี 80+ ให้มองว่าเป็น baseline ที่ต้องปรับปรุงต่อเนื่อง ไม่ใช่ exam score

Lynis ต่างจาก OpenSCAP อย่างไร?

OpenSCAP เป็น framework สำหรับ compliance กับมาตรฐานเฉพาะ (เช่น CIS Benchmark, STIG) มีความซับซ้อนกว่าและต้องการ profile XML Lynis ใช้ง่ายกว่า ไม่ต้องการ profile พิเศษ และให้คำแนะนำที่ actionable ทันที ทั้งสองใช้ร่วมกันได้ดี — Lynis สำหรับ quick hardening audit, OpenSCAP สำหรับ formal compliance report