انتقل إلى المحتوى الرئيسي

الفصل 8.1 - مقدمة إلى systemd

أهداف التعلم​

في نهاية هذا الفصل، ستكون قادرًا على:

  • فهم ما هو systemd
  • استخدام systemctl لإدارة الخدمات
  • فهم وحدات systemd
  • رؤية حالة الخدمات
  • بدء، إيقاف، إعادة تشغيل الخدمات

ما هو systemd؟​

systemd هو نظام التهيئة وإدارة الخدمات في معظم توزيعات لينكس الحديثة.

الوظائف:

  • إدارة الخدمات
  • بدء النظام
  • إدارة السجلات (journald)
  • الجدولة (المؤقتات)
  • تركيب أنظمة الملفات

وحدات systemd​

أنواع الوحدات​

# رؤية الأنواع
systemctl list-units --type=service
systemctl list-units --type=timer
systemctl list-units --type=mount
systemctl list-units --type=socket

أنواع شائعة:

  • .service: خدمات
  • .timer: مهام مجدولة
  • .mount: نقاط التركيب
  • .socket: مقابس الشبكة
  • .target: مجموعات الوحدات

systemctl - الأوامر الأساسية​

رؤية الحالة​

# جميع الخدمات
systemctl list-units --type=service

# الخدمات النشطة
systemctl list-units --type=service --state=running

# الخدمات غير النشطة
systemctl list-units --type=service --state=inactive

# الخدمات الفاشلة
systemctl list-units --type=service --state=failed

# حالة خدمة
systemctl status sshd
systemctl status apache2

بدء/إيقاف​

# بدء
sudo systemctl start service_name

# إيقاف
sudo systemctl stop service_name

# إعادة تشغيل
sudo systemctl restart service_name

# إعادة تحميل التكوين
sudo systemctl reload service_name

# إعادة تحميل أو إعادة تشغيل
sudo systemctl reload-or-restart service_name

تفعيل/تعطيل عند بدء التشغيل​

# تفعيل (بدء تلقائي)
sudo systemctl enable service_name

# تعطيل
sudo systemctl disable service_name

# تفعيل وبدء
sudo systemctl enable --now service_name

# التحقق إذا كان مفعلًا
systemctl is-enabled service_name

حالات الخدمات​

حالات ممكنة​

الحالات:

  • active (running): قيد التشغيل
  • active (exited): انتهى بنجاح
  • inactive: متوقف
  • failed: فشل
  • activating: قيد البدء

رؤية التفاصيل​

# حالة مفصلة
systemctl status service_name

# معلومات كاملة
systemctl show service_name

# خاصية محددة
systemctl show -p ActiveState service_name
systemctl show -p LoadState service_name

أوامر مفيدة​

سرد الخدمات​

# جميع الخدمات
systemctl list-units --type=service --all

# الخدمات المفعلة
systemctl list-unit-files --type=service --state=enabled

# الخدمات المعطلة
systemctl list-unit-files --type=service --state=disabled

# حسب الحالة
systemctl list-units --type=service --state=running
systemctl list-units --type=service --state=failed

إخفاء/إظهار​

# إخفاء خدمة
sudo systemctl mask service_name

# إظهار
sudo systemctl unmask service_name

إعادة تحميل systemd​

# إعادة تحميل التكوين (بعد تعديل الملفات)
sudo systemctl daemon-reload

أمثلة عملية​

إدارة SSH​

# رؤية الحالة
systemctl status sshd

# بدء
sudo systemctl start sshd

# تفعيل عند بدء التشغيل
sudo systemctl enable sshd

# إعادة تشغيل
sudo systemctl restart sshd

إدارة Apache/Nginx​

# Apache
sudo systemctl start apache2
sudo systemctl enable apache2
sudo systemctl status apache2

# Nginx
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl status nginx

الخدمات الفاشلة​

# رؤية الخدمات الفاشلة
systemctl list-units --state=failed

# إعادة تشغيل خدمة فاشلة
sudo systemctl restart failed_service

# رؤية السجلات
sudo journalctl -u failed_service

ملخص​

في هذا الفصل، تعلمت:

systemd: نظام التهيئة وإدارة الخدمات
الوحدات: .service, .timer, .mount, .socket, .target
systemctl: أوامر لإدارة الخدمات
الحالات: active, inactive, failed, activating
الأوامر: start, stop, restart, enable, disable, status
الممارسة: إدارة الخدمات الشائعة (SSH، Apache، إلخ.)


الخطوات التالية​

الفصل 8.2: إنشاء خدمات مخصصة
الفصل 8.3: Cron متقدم وsystemd Timers


تم إنشاء الفصل في: ديسمبر 2024