Introduction
As a Linux administrator or user, you may need to ensure that certain services automatically start up every time your system boots. This could be a web server, a database service, or any custom application that you need to keep running all the time. Ubuntu, like other Linux distributions, employs a couple of different systems for managing services, including the older SysVinit system and the newer systemd system. This guide will explain how you can configure a service to auto-start on system boot using systemd, which has become the default on most modern Linux distributions including Ubuntu.
Understanding Systemd
systemd
is an init system and system manager that became the new standard for Ubuntu since version 15.04. It is responsible for initializing the system in the very early boot stage to bring the Linux host into a usable state. After the booting process completes, systemd will continue to manage services throughout the system’s operation.
One of the benefits of systemd is that it allows you to easily enable or disable services to auto-start on boot. It uses .service
files located in /etc/systemd/system/
to manage system and session services.
Identifying the Service to Auto-start
Before configuring a service to start automatically, you must first know the exact name of the service. To check the status of a service or to check whether it is enabled, use the following command:
systemctl status service_name
Replace service_name
with the actual name of the service. To list all available services, run:
systemctl list-unit-files --type=service
Once you have identified the correct name of the service, you can proceed to configure it.
Enabling a Service Auto-start With systemd
To enable a service to start on boot, use the enable command with systemctl
:
sudo systemctl enable service_name
For example, to enable the Apache web server to start automatically, you would run:
sudo systemctl enable apache2
The enable command tells systemd to link the service file (for your service) from its definition directory into the multi-user.target.wants
directory to tell the system to start it during the boot sequence for multi-user runlevels.
You can check to see if the service is indeed enabled by typing:
sudo systemctl is-enabled service_name
When the service has been enabled, you will see enabled
displayed. Disabled services will show disabled
, and if a service has a condition that prevents it from being started on boot, it will display indirect
.
Disabling Auto-start for a Service
Should you wish to prevent a service from starting automatically on boot, you can use:
sudo systemctl disable service_name
This will remove the symbolic link for the service’s .service
file from the multi-user.target.wants
directory, thereby preventing it from starting automatically on the next boot.
Starting and Stopping Services Manually
Regardless of whether a service is enabled to start automatically on boot, you can manage it manually using forthcoming systemd commands.
To start a service immediately:
sudo systemctl start service_name
To stop a running service:
sudo systemctl stop service_name
To restart a running service or to start it if it’s not already running:
sudo systemctl restart service_name
And to reload a service without interrupting its operation:
sudo systemctl reload service_name
Note that not all services support the reload operation as it requires the process to be able to reload its configuration in memory without having to restart fully.
Configuring Services to Start After Network is Ready
Sometimes you have services that depend on the network being up and running. This is common for services that need to bind to network interfaces or communicate with external resources at startup. For this scenario, you should also ensure that the service is set appropriately to start after network services are brought up.
You can configure a service to start after the network is ready by editing its unit file. Find the .service
file located in /etc/systemd/system/
or the /lib/systemd/system/
directory and add a dependency on the network-online target:
[Unit]
After=network-online.target
Wants=network-online.target
Remember that you have to reload the systemd
daemon each time you change a service file:
sudo systemctl daemon-reload
Then restart your service:
sudo systemctl restart servicename
Advanced Configurations
For more advanced configuration, systemd allows the creation and management of custom service files. The process involves copying an example service file to /etc/systemd/system/
, then editing it to suit your service, and enabling it as described above. You can also control the order of service startups using the Before
and After
directives in the service’s unit file.
Moreover, for troubleshooting, you can use the journalctl
command to view the logs for your services:
journalctl -u service_name
This can be vital to understand what your service is doing at startup and to diagnose any problems right after boot.
Conclusion
Automating the startup of services on Ubuntu can greatly simplify server management and is a fundamental skill for system administrators. With the tools provided by systemd
, Ubuntu users have a robust and straightforward way to manage service behavior across reboots. Understanding and using systemctl
commands is key to effectively controlling your system’s services, allowing you to focus on more important tasks.