1. What is systemd? #
systemd is a system and service manager for Linux that replaces the traditional SysVinit and Upstart. It’s designed for faster boot times, better resource management, and centralized configuration.
Key components of systemd:
systemd
: The main daemon.journald
: Centralized logging system.timedated
,logind
,networkd
: Manage time, user sessions, and networking.- Units: systemd uses “unit files” to define and control system components like services, sockets, devices, and more.
2. Basic Commands #
Service Management #
- Start a service:
systemctl start <service>
- Stop a service:
systemctl stop <service>
- Restart a service:
systemctl restart <service>
- Enable a service (start at boot):
systemctl enable <service>
- Disable a service (prevent from starting at boot):
systemctl disable <service>
- Check the status of a service:
systemctl status <service>
System State #
- Reboot the system:
systemctl reboot
- Power off the system:
systemctl poweroff
- View system logs:
journalctl
3. Unit Files #
What are Unit Files? #
Unit files describe system resources and are located in /etc/systemd/system/
(custom), /lib/systemd/system/
(default), or /usr/lib/systemd/system/
.
Types of unit files:
.service
: Services (e.g., web servers, databases)..socket
: Sockets for communication..timer
: Timers (like cron jobs)..target
: Group of units (like runlevels).
Viewing and Editing Unit Files #
- View a unit file:
systemctl cat <unit>
- Edit a unit file:This opens an override file for safe customization.
sudo systemctl edit <unit>
Reload systemd after editing: #
systemctl daemon-reload
4. Boot Targets #
Targets are similar to runlevels in SysVinit.
Common targets:
graphical.target
: Multi-user with a graphical interface.multi-user.target
: Multi-user without GUI (default for servers).rescue.target
: Single-user mode.emergency.target
: Minimal system, no services.
Commands:
- Check current target:
systemctl get-default
- Change default target:
systemctl set-default <target>
- Switch to a target:
systemctl isolate <target>
5. Logging with journalctl #
journalctl
manages logs. It’s a central logging system for systemd.
- View logs:
journalctl
- View logs for a service:
journalctl -u <service>
- Follow logs in real-time:
journalctl -f
- View logs since a specific time:
journalctl --since "2025-01-01 00:00:00"
6. Creating a Custom Service #
Create the unit file:
sudo nano /etc/systemd/system/myapp.service
Example:
[Unit] Description=My Custom Application After=network.target [Service] ExecStart=/usr/bin/myapp Restart=always User=myuser [Install] WantedBy=multi-user.target
Reload systemd:
systemctl daemon-reload
Enable and start the service:
systemctl enable myapp.service systemctl start myapp.service
Check status:
systemctl status myapp.service
7. Useful Tools #
systemctl
: Main tool for managing services and units.journalctl
: Log management.systemd-analyze
: Debug and analyze boot performance.systemd-analyze blame
8. Advanced Features #
Timers: systemd alternative to
cron
. Example timer unit:[Unit] Description=Run MyApp Daily [Timer] OnCalendar=daily Persistent=true [Install] WantedBy=timers.target
Socket Activation: Services start only when needed.
Resource Management: Use
systemd-cgls
andsystemd-cgtop
to manage cgroups.
Mastering systemd makes you more efficient at managing Linux systems. It’s a powerful tool with many features to explore. Dive deeper into its documentation and practice to become a pro! 🚀