init

2024-03-30

Understanding Systemd Units

Before diving into commands, let’s grasp the fundamental concept of Systemd units. These are configuration files that describe a service, target, or device. They reside in /etc/systemd/system/ (and other locations). Each unit file has a specific extension: .service for services, .target for groups of units, and so on.

Common systemctl Commands

The systemctl command is your primary tool for interacting with Systemd. Here are some essential commands with examples:

1. Listing Services:

To see all active services, use:

systemctl list-units

This provides a list of all loaded and active units, their status (active, inactive, failed), and load state. Filtering is possible:

systemctl list-units --type=service

This shows only services.

2. Starting, Stopping, and Restarting Services:

Let’s say we want to manage the SSH service (usually ssh).

sudo systemctl start ssh

This starts the SSH service. The sudo is needed because managing services often requires root privileges.

sudo systemctl stop ssh

This stops the SSH service.

sudo systemctl restart ssh

Restarts the SSH service gracefully.

sudo systemctl reload ssh

This reloads the configuration of the running service without restarting. Useful if you’ve changed the configuration file.

3. Checking Service Status:

To check the status of a service:

sudo systemctl status ssh

This provides detailed information about the service, including its status, active state, and logs.

4. Enabling and Disabling Services:

sudo systemctl enable ssh

This ensures the service starts automatically on boot.

sudo systemctl disable ssh

This prevents the service from starting automatically on boot.

5. Working with Service Files:

Understanding the structure of a service file is important for customization. A basic service file might look like this:

[Unit]
Description=My Custom Service
After=network.target

[Service]
Type=simple
User=myuser
Group=mygroup
ExecStart=/path/to/my/service/script

[Install]
WantedBy=multi-user.target

Remember to replace placeholders like /path/to/my/service/script, myuser, and mygroup with your actual values. After creating this file (e.g., /etc/systemd/system/my-custom-service.service), you need to reload the daemon and enable/start the service:

sudo systemctl daemon-reload
sudo systemctl enable my-custom-service
sudo systemctl start my-custom-service

6. Viewing Logs:

Systemd provides a convenient way to view service logs:

sudo journalctl -u ssh

This shows the logs specifically for the SSH service. journalctl -xe displays recent system logs across all units.

These examples provide a solid foundation for managing Linux services with Systemd. Further exploration into Systemd’s capabilities, including timers, sockets, and more, will improve your Linux administration skills.