systemctl เป็นคำสั่งหลักสำหรับจัดการ Service บน Linux ที่ใช้ systemd เป็น Init System ซึ่งครอบคลุม Distribution หลักอย่าง Ubuntu, Debian, RHEL, Rocky Linux, Fedora และอื่น ๆ systemctl ช่วยให้ Admin สั่ง Start, Stop, Restart, Enable, Disable Service รวมถึงดูสถานะและ Log ได้จากที่เดียว
บทความนี้อธิบายคำสั่ง systemctl ที่ใช้บ่อยในงาน System Administration จริง ตั้งแต่การจัดการ Service พื้นฐาน ไปจนถึงการดู Unit Files, Target และการ Troubleshoot Service ที่มีปัญหา
คำสั่งพื้นฐาน — Start, Stop, Restart
# Start service
sudo systemctl start nginx
# Stop service
sudo systemctl stop nginx
# Restart service (Stop แล้ว Start ใหม่)
sudo systemctl restart nginx
# Reload config โดยไม่ Stop service (ถ้า service รองรับ)
sudo systemctl reload nginx
# Reload หรือ Restart (ลอง reload ก่อน ถ้าไม่ได้ค่อย restart)
sudo systemctl reload-or-restart nginx
Enable และ Disable — Auto-start เมื่อ Boot
Enable และ Disable กำหนดว่า Service จะ Start อัตโนมัติตอน Boot หรือไม่ ต่างจาก Start/Stop ที่มีผลเฉพาะ Session ปัจจุบัน
# Enable service ให้ start อัตโนมัติตอน boot
sudo systemctl enable nginx
# Enable และ Start พร้อมกันในคำสั่งเดียว
sudo systemctl enable --now nginx
# Disable service ไม่ให้ start ตอน boot
sudo systemctl disable nginx
# Disable และ Stop พร้อมกันในคำสั่งเดียว
sudo systemctl disable --now nginx
# ตรวจสอบว่า service enable อยู่หรือไม่
systemctl is-enabled nginx
ดูสถานะ Service
# ดูสถานะ service พร้อม Log ล่าสุด
systemctl status nginx
# ตรวจว่า service กำลังรันอยู่หรือไม่
systemctl is-active nginx
# ดูสถานะแบบย่อ (active / inactive / failed)
systemctl is-active nginx && echo "running" || echo "not running"
Output ของ systemctl status บอก State ของ Service เช่น active (running), inactive (dead), failed พร้อม PID, ระยะเวลาที่รัน และ Log บรรทัดล่าสุด
ดูรายการ Service ทั้งหมด
# ดู service/unit ทั้งหมดที่โหลดอยู่
systemctl list-units --type=service
# ดูเฉพาะ service ที่กำลัง active
systemctl list-units --type=service --state=active
# ดูเฉพาะ service ที่ failed
systemctl list-units --type=service --state=failed
# ดู service ที่ enable ทั้งหมด (จะ start ตอน boot)
systemctl list-unit-files --type=service --state=enabled
Mask และ Unmask Service
Mask แข็งแกร่งกว่า Disable โดย Symlink Unit File ไปที่ /dev/null ทำให้ Service นั้นไม่สามารถ Start ได้เลย แม้แต่โดยอัตโนมัติหรือจาก Dependency ของ Service อื่น
# Mask service — ป้องกันไม่ให้ start ไม่ว่ากรณีใด
sudo systemctl mask nginx
# Unmask service — เอา mask ออก
sudo systemctl unmask nginx
ดูและแก้ไข Unit File
Unit File คือ Config ของ Service ที่บอก systemd ว่าจะ Start อย่างไร รอ Dependency อะไร และ Restart เมื่อไหร่
# ดู Unit File ของ service
systemctl cat nginx
# ดูว่า Unit File อยู่ที่ไหน
systemctl show nginx --property=FragmentPath
# แก้ไข Unit File แบบ Override (สร้าง drop-in file ใน /etc/systemd/system/nginx.service.d/)
sudo systemctl edit nginx
# แก้ไข Unit File ตรง (ระวัง — จะถูกทับเมื่ออัพเดต package)
sudo systemctl edit --full nginx
# หลังแก้ไข Unit File ต้อง reload daemon
sudo systemctl daemon-reload
Target — เทียบเท่า Runlevel
Target ใน systemd เทียบได้กับ Runlevel ใน SysVinit โดย Target ที่ใช้บ่อยใน Production Server คือ multi-user.target (เทียบ Runlevel 3 — เปิด Network แต่ไม่มี GUI) และ graphical.target (เทียบ Runlevel 5 — มี GUI)
# ดู default target ปัจจุบัน
systemctl get-default
# เปลี่ยน default target (เช่น ให้ boot เป็น multi-user ไม่มี GUI)
sudo systemctl set-default multi-user.target
# สลับ target ทันที (ไม่ต้อง reboot)
sudo systemctl isolate multi-user.target
# Reboot, Poweroff ผ่าน systemctl
sudo systemctl reboot
sudo systemctl poweroff
ดู Log ด้วย journalctl
systemd รวม Log ทั้งหมดไว้ใน Journal ซึ่งดูผ่าน journalctl ทำให้ Troubleshoot Service ที่ Start ไม่ขึ้นได้ง่ายกว่าการอ่าน Log file โดยตรง
# ดู Log ของ service เฉพาะตัว
journalctl -u nginx
# ดู Log แบบ Follow (real-time)
journalctl -u nginx -f
# ดู Log 50 บรรทัดล่าสุด
journalctl -u nginx -n 50
# ดู Log ตั้งแต่ boot ล่าสุด
journalctl -u nginx -b
# ดู Log ช่วงเวลาที่กำหนด
journalctl -u nginx --since "2026-04-16 00:00" --until "2026-04-16 23:59"
Troubleshoot Service ที่ Failed
เมื่อ Service Start ไม่ขึ้นหรือ Status เป็น failed ขั้นตอนที่แนะนำคือ
# 1. ดูสถานะและ error message
systemctl status nginx
# 2. ดู Log ที่เกี่ยวข้องอย่างละเอียด
journalctl -u nginx -n 100 --no-pager
# 3. ตรวจสอบ config syntax ของ service นั้น
nginx -t # nginx
apache2ctl configtest # apache2
# 4. Reset failed state หลังแก้ไข
sudo systemctl reset-failed nginx
# 5. Start ใหม่
sudo systemctl start nginx
สร้าง Custom Service Unit
สามารถสร้าง Unit File เองเพื่อให้ systemd จัดการ Process หรือ Script ที่ต้องการ รูปแบบ Unit File พื้นฐานสำหรับ Application หรือ Script มีดังนี้
# สร้างไฟล์ /etc/systemd/system/myapp.service
[Unit]
Description=My Application
After=network.target
[Service]
Type=simple
User=deploy
WorkingDirectory=/opt/myapp
ExecStart=/opt/myapp/bin/start.sh
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
# หลังสร้าง Unit File ต้อง reload daemon และ enable
sudo systemctl daemon-reload
sudo systemctl enable --now myapp
สรุป
systemctl เป็นเครื่องมือหลักในการจัดการ Service บน Linux ที่ใช้ systemd คำสั่งที่ใช้บ่อยที่สุดคือ start, stop, restart, enable --now, status และ disable --now การใช้ journalctl -u servicename ควบคู่กันช่วย Troubleshoot ปัญหาได้อย่างรวดเร็ว ส่วน Mask ใช้เมื่อต้องการป้องกันไม่ให้ Service Start ได้เลย และ systemctl edit ช่วยปรับแต่ง Unit File โดยไม่กระทบกับ Package Update
แนะนำบริการ DE
การฝึกใช้ systemctl และจัดการ Service บน Linux ต้องการ Server ที่มีสิทธิ์ root เต็มรูปแบบ Cloud VPS ของ DE ให้สิทธิ์ root สมบูรณ์พร้อม OS หลากหลายให้เลือกทั้ง Ubuntu และ Rocky Linux เหมาะสำหรับทดลองจัดการ Service, สร้าง Custom Unit File และฝึก System Administration บน Server จริง
หากต้องการโฮสต์เว็บไซต์โดยไม่ต้องจัดการ Service หรือ systemd เอง Cloud Hosting ของ DE มีระบบที่ดูแล Web Server และ Service ต่าง ๆ ให้โดยอัตโนมัติ

