Skip to main content

Managing Services with systemd

1. Starting Services

Command Description Notes
systemctl start service-name Starts a service immediately Service must be enabled to start at boot
systemctl restart service-name Stops and then starts a service Useful for applying configuration changes
systemctl try-restart service-name Restarts service only if it is running Safer alternative to restart

Example: Starting a service

sudo systemctl start nginx
Note: Use systemctl status <em>service-name</em> to verify the service started successfully.

2. Stopping Services

Command Description Notes
systemctl stop <service> Stops the specified service immediately Does not disable the service; it will start again on boot
systemctl kill <service> Sends a signal to terminate the service process Useful when stop does not work
systemctl disable <service> Disables the service from starting at boot Does not stop a running service; use stop first

Example: Stopping a service

systemctl stop nginx

Example: Disabling a service at boot

systemctl disable nginx

3. Restarting Services

Command Description Notes
systemctl restart <service> Restarts the specified service Stops and then starts the service
systemctl try-restart <service> Restarts the service only if it is running Safely attempts restart without error if not active
systemctl reload-or-restart <service> Reloads configuration, falls back to restart if reload fails Useful for services that support hot reloading

Example: Restarting a service

sudo systemctl restart nginx

Example: Conditionally restarting a service

sudo systemctl try-restart apache2

4. Reloading Configuration

Reloading configuration in systemd allows you to apply changes to service configurations without restarting the service. This is useful for updating settings like environment variables, resource limits, or other runtime options.

Command Description Notes
systemctl reload service-name Reloads the configuration of a running service. Does not restart the service process. Only reloads configuration files.
systemctl kill service-name --signal HUP Sends a hangup signal to the service process, which often triggers a reload. Useful when systemctl reload does not work or is not supported.

Example: Reloading a Web Server Configuration

systemctl reload nginx
Note: Not all services support configuration reloading. Services must explicitly handle the reload signal (e.g., SIGHUP) to apply new configurations.
Warning: If a service does not properly handle reloads, it may continue using old configurations or behave unexpectedly.

5. Checking Service Status

Command Description Example
systemctl status service-name Displays the current status of a service, including its state, PID, and recent log entries. systemctl status nginx
systemctl is-active service-name Returns active if the service is running, otherwise inactive. systemctl is-active nginx
systemctl is-enabled service-name Checks if a service is enabled to start at boot time. systemctl is-enabled nginx
systemctl list-units --type=service Lists all loaded service units and their status. systemctl list-units --type=service
Note: The systemctl status command provides detailed runtime information, including logs and configuration paths.

6. Enabling Services at Boot

Command Description Example
systemctl enable Enables a service to start at boot sudo systemctl enable nginx.service
systemctl disable Disables a service from starting at boot sudo systemctl disable nginx.service
systemctl is-enabled Checks if a service is enabled systemctl is-enabled nginx.service

Example: Enable a service at boot

sudo systemctl enable nginx.service

Example: Check if a service is enabled

systemctl is-enabled nginx.service
Note: The .service extension is optional when using systemctl commands.

7. Disabling Services

Service Name Status Command Notes
example.service enabled systemctl disable example.service Disables the service from starting at boot
another.service enabled systemctl disable another.service Disables the service from starting at boot

Example: Disabling a Service

systemctl disable nginx.service
Note: Disabling a service does not stop it if it is currently running.
Warning: Use systemctl mask to prevent a service from being started or enabled.

8. Listing All Services

Command Description Notes
systemctl list-units --type=service Lists all loaded service units Shows only services that are currently loaded and active
systemctl list-unit-files --type=service Lists all service unit files Shows all installed services, whether enabled or disabled
systemctl list-units --state=running Lists only running services Filters output to show only active services
systemctl list-units --state=failed Lists only failed services Useful for troubleshooting service issues

Example: Listing all running services

systemctl list-units --type=service --state=running

Example: Listing all service unit files

systemctl list-unit-files --type=service

9. Viewing Service Dependencies

Command Description Example
systemctl list-dependencies Lists all dependencies of a service systemctl list-dependencies nginx.service
systemctl list-dependencies --reverse Shows reverse dependencies (what depends on this service) systemctl list-dependencies --reverse nginx.service
systemctl list-units --all --state=loaded Lists all loaded units and their dependencies systemctl list-units --all --state=loaded

Example: View service dependencies

systemctl list-dependencies nginx.service

Example: View reverse dependencies

systemctl list-dependencies --reverse nginx.service

10. Masking Services

Masking a service in systemd prevents it from being started or enabled, effectively disabling it permanently.

Command Description Notes
systemctl mask <service> Disables a service by creating a symlink to /dev/null Service cannot be started or enabled
systemctl unmask <service> Re-enables a masked service Removes the mask and restores normal operation

Example: Masking a service

sudo systemctl mask ssh.service

Example: Unmasking a service

sudo systemctl unmask ssh.service
Note: Masking a service does not remove its configuration files or unit definition. It only prevents systemd from starting or enabling it.

11. Creating Custom Service Units

Property Description Example
Unit Base unit type (required) [Unit]
Service Service-specific configuration (required for service units) [Service]
Install Installation configuration (required for enabling units) [Install]

Example: Basic Service Unit

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

[Service]
Type=simple
User=myuser
ExecStart=/usr/bin/myapp
Restart=always

[Install]
WantedBy=multi-user.target
Note: Service units must be placed in /etc/systemd/system/ or /lib/systemd/system/ to be recognized by systemd.

Example: Service with Environment Variables

[Unit]
Description=Web Server with Env
After=network.target

[Service]
Type=simple
User=www-data
Environment=NODE_ENV=production
Environment=PORT=3000
ExecStart=/usr/bin/node /app/server.js
Restart=on-failure

[Install]
WantedBy=multi-user.target
Warning: Ensure proper file permissions (e.g., chmod 644) and ownership for custom service files to prevent security issues.

12. Reloading systemd Configuration

Reloading systemd configuration allows you to update the daemon's configuration without restarting the entire systemd process. This is useful for applying changes to service files or other configuration elements.

Command Description Notes
systemctl daemon-reload Reloads the systemd manager configuration Does not restart services; applies changes to unit files
systemctl reload <service> Reloads a specific service (if supported) Triggers the service's reload command; may not be available for all services

Example: Reloading systemd configuration

sudo systemctl daemon-reload
Note: Changes to unit files require daemon-reload to take effect. Some services support reload which sends a signal to the service process to re-read its configuration.
Warning: Using daemon-reload does not restart services, but it may cause issues if a service's configuration has changed in a way that requires a restart.

Was this article helpful?