Init Systems — systemd vs SysVinit บน Linux

Init System คือกระบวนการแรกที่ Linux Kernel เรียกใช้หลัง Boot โดยมี PID 1 และรับผิดชอบในการ Start Service ต่าง ๆ จนระบบพร้อมใช้งาน Linux มี Init System หลักสองระบบที่ควรรู้จักคือ SysVinit ซึ่งเป็นระบบดั้งเดิมที่ใช้ Shell Script และ systemd ที่เข้ามาแทนที่ในปัจจุบันบน Distribution หลักทุกตัว

บทความนี้อธิบายความแตกต่างระหว่าง SysVinit และ systemd ทั้งในแง่สถาปัตยกรรม ไฟล์ Config และคำสั่งที่ใช้ เพื่อให้เข้าใจที่มาของ systemd และเหตุผลที่ Distribution ต่าง ๆ เปลี่ยนมาใช้

SysVinit — ระบบดั้งเดิม

SysVinit (System V Init) เป็น Init System ที่ใช้มาตั้งแต่ยุค UNIX เดิม มีแนวคิดหลักคือ Runlevel ซึ่งเป็นสถานะของระบบที่กำหนดว่า Service ใดควรรันอยู่ Init Script ของแต่ละ Service เก็บไว้ใน /etc/init.d/ เป็น Shell Script ที่รับ Argument start, stop, restart, status

Runlevel ใน SysVinit

# Runlevel มาตรฐาน
# 0 — Halt (ปิดเครื่อง)
# 1 — Single User Mode (Rescue/Maintenance)
# 2 — Multi-user ไม่มี Network (บาง Distro)
# 3 — Multi-user พร้อม Network (Server ทั่วไป)
# 4 — ไม่ได้ใช้ (กำหนดเองได้)
# 5 — Multi-user พร้อม Network + GUI
# 6 — Reboot

# ดู Runlevel ปัจจุบัน
runlevel

# เปลี่ยน Runlevel (RHEL-based เก่า)
sudo init 3
sudo telinit 3

จัดการ Service ด้วย SysVinit

# Start/Stop/Restart service
sudo /etc/init.d/nginx start
sudo /etc/init.d/nginx stop
sudo /etc/init.d/nginx restart

# หรือใช้ service command (wrapper)
sudo service nginx start
sudo service nginx stop
sudo service nginx status

# Enable/Disable service (RHEL-based เก่า)
sudo chkconfig nginx on
sudo chkconfig nginx off
sudo chkconfig --list nginx

# Enable/Disable service (Debian-based เก่า)
sudo update-rc.d nginx enable
sudo update-rc.d nginx disable

systemd — Init System สมัยใหม่

systemd เข้ามาแทนที่ SysVinit บน Distribution หลักตั้งแต่ปี 2011 เป็นต้นมา โดย Fedora 15 เป็นรายแรก ตามด้วย RHEL 7, Ubuntu 15.04, Debian 8 และทุก Distribution สำคัญในเวลาต่อมา systemd ใช้ Unit File แทน Shell Script และสามารถ Start Service แบบ Parallel ได้ ทำให้ Boot เร็วกว่า SysVinit อย่างมีนัยสำคัญ

เปรียบเทียบ SysVinit กับ systemd

# คำสั่งเทียบกัน
# SysVinit                          systemd
service nginx start                 systemctl start nginx
service nginx stop                  systemctl stop nginx
service nginx restart               systemctl restart nginx
service nginx status                systemctl status nginx
chkconfig nginx on                  systemctl enable nginx
chkconfig nginx off                 systemctl disable nginx
chkconfig --list                    systemctl list-unit-files

# Runlevel เทียบ Target
init 0                              systemctl poweroff
init 1                              systemctl rescue
init 3                              systemctl isolate multi-user.target
init 5                              systemctl isolate graphical.target
init 6                              systemctl reboot

ข้อดีของ systemd เหนือ SysVinit

  • Parallel Startup — systemd Start Service แบบ Parallel โดยอิงจาก Dependency Graph ทำให้ Boot เร็วกว่า SysVinit ที่ Start แบบ Sequential
  • Dependency Management — Unit File ระบุ After=, Requires=, Wants= ได้ชัดเจน ระบบจัดการ Order และ Dependency อัตโนมัติ
  • Journal Logging — systemd รวม Log ทุก Service ไว้ใน Journal ค้นหาและกรองด้วย journalctl ได้สะดวกกว่าการอ่านไฟล์ Log แยกกัน
  • Socket Activation — Service Start ได้เมื่อมี Connection เข้ามา ไม่ต้อง Start ทิ้งไว้ตลอดเวลา ประหยัด Resource
  • Cgroup Integration — ติดตาม Process ทุกตัวที่ Service สร้างผ่าน Cgroup ป้องกัน Orphan Process เมื่อ Service หยุดทำงาน

โครงสร้างไฟล์ systemd

# Unit File ของ Package — อย่าแก้ไขตรง ๆ
/usr/lib/systemd/system/

# Unit File ที่ Admin สร้างหรือ Override — แก้ที่นี่
/etc/systemd/system/

# Override (drop-in) สำหรับ Service เดิม
/etc/systemd/system/nginx.service.d/override.conf

# Runtime Unit (ชั่วคราว — หายเมื่อ reboot)
/run/systemd/system/

ตรวจสอบ Init System ที่ใช้อยู่

# ดูว่า PID 1 คืออะไร
ps -p 1 -o comm=

# ดู Init System ด้วย stat
stat /proc/1/exe

# ตรวจสอบผ่าน systemd
systemctl --version

# ตรวจสอบผ่าน init version (ถ้าใช้ SysVinit)
/sbin/init --version

Compatibility Layer — ใช้คำสั่งเก่าบน systemd

systemd มี Compatibility Layer ที่ทำให้คำสั่งเก่าอย่าง service และ chkconfig ยังใช้ได้บนระบบที่ใช้ systemd โดย Wrapper เหล่านี้จะแปลงคำสั่งไปเรียก systemctl อัตโนมัติ

# คำสั่งเหล่านี้ยังใช้ได้บน systemd (แต่จะแนะนำให้ใช้ systemctl แทน)
sudo service nginx start     # → systemctl start nginx
sudo service nginx status    # → systemctl status nginx

# บน Ubuntu/Debian มี update-rc.d สำหรับ SysVinit legacy
# บน RHEL/CentOS มี chkconfig ที่ Redirect ไป systemctl

Upstart — Init System ที่เลิกใช้

Upstart เป็น Init System ที่ Canonical พัฒนาและใช้งานใน Ubuntu 6.10 ถึง Ubuntu 14.10 ก่อนเปลี่ยนมาใช้ systemd ตั้งแต่ Ubuntu 15.04 Upstart มีแนวคิด Event-based ต่างจากทั้ง SysVinit และ systemd แต่ปัจจุบันไม่มีระบบสำคัญใดใช้งานแล้ว กล่าวถึงเพื่อให้เข้าใจบริบทเท่านั้น

สรุป

SysVinit เป็น Init System ดั้งเดิมที่ใช้ Shell Script และ Runlevel แบบ Sequential ส่วน systemd เข้ามาแทนที่ด้วยการ Start Service แบบ Parallel, Unit File ที่มีโครงสร้างชัดเจน, Journal สำหรับ Log รวมศูนย์ และ Cgroup Integration Distribution หลักทุกตัวในปัจจุบันใช้ systemd แล้ว แต่คำสั่งเก่าอย่าง service ยังใช้ได้ผ่าน Compatibility Layer บน Server สมัยใหม่ควรใช้ systemctl โดยตรงเพื่อประสิทธิภาพและความเข้ากันได้ในระยะยาว

แนะนำบริการ DE

การเรียนรู้ Init System และทดลองใช้ทั้ง SysVinit และ systemd ต้องการ Server ที่ควบคุมได้เต็มที่ Cloud VPS ของ DE รองรับทั้ง Ubuntu และ Rocky Linux พร้อม Root Access เต็มรูปแบบ เหมาะสำหรับทดลอง systemd Unit File, Target และการจัดการ Service บน Init System จริง

หากต้องการโฮสต์เว็บไซต์โดยไม่ต้องยุ่งกับ Init System เอง Cloud Hosting ของ DE มีทีมดูแลด้าน Server Infrastructure ให้อัตโนมัติ