Home » How-to » How to Configure Service to Start Automatically in Linux

How to Configure Service to Start Automatically in Linux

Service in the Linux operating system is just a program which is executing in the background and does not have any windows or other communication channels with a user interface. There are a lot of services which are working in the background. And most of them must be run on the system startup.

The Systemd initialization system handles all of this. It can help you to check which services are running at the moment, view their logs, configure autostart, and other things. In this article, I will explain how to configure the service to start automatically in Linux. I will use Ubuntu for all examples, but this tutorial will work in all distributions that use Systemd.


Table of Contents

Configuring Services Autostart in Linux

As I said before, services are regular programs which are running in the background and are not connected to stdin and stdout streams. Each service in Systemd has its configuration file in the filesystem. You can read about this in more detail in previous article. In this article, I will cover only autostart of services.

Linux supports run levels since SysVInit initialization system. Each Run level determines which services must be started. Systemd supports run levels too. But here they exist as targets. Here is the list of supported run levels and targets that correspond to them:

  • Run Level 0 (poweroff.target, runlevel0.target) - shut down the system;
  • Run Level 1 (rescue.target runlevel1.target) - recovery mode;
  • Run Level 3 (multi-user.target, runlevel3.target) - text-only mode;
  • Run level 5 (graphical.target) - graphical environment;
  • Run level 6 (reboot.target, runlevel6.target) - reboots the system.

When you add service to system startup, Systemd just creates a symbolic link to the service unit in the run level folder. The run level is specified in the service configuration file.

On the system startup, Systemd runs all services which are configured to start with multi-user.target and then graphical.target when it is a desktop system. On servers, Systemd runs only services for multi-user.target. Now, let's have a look at how to deal with all of this in practice.

1. List Services which are Started Automatically

If you want to find which services are added into Systemd autostart, use the list-unit-files command with filtering by state:

systemctl list-unit-files --type=service --state=enabled

2. Check Service Autostart State

You can use the is-enabled command to check whether the service is already added to autostart. For example:

sudo systemctl is-enabled nginx

You can use the status command instead of is-enabled. In addition, it displays whether the service is running at the moment and its latest logs:

sudo systemctl status nginx

You can find autostart state in the Loaded line after the path to the service unit file. The state can be enabled, disabled, or static. Services marked as static are added to autostart and can't be removed.

3. Enable Autostart for Service

Use the enable command to make the service start at the system startup:

sudo systemctl enable <name_of_service>

For example, run the following command to configure autostart for Nginx web-server:

sudo systemctl enable nginx

When the service is not running now and you want to start it now, you can combine the enable command with the --now option. For example:

sudo systemctl enable --now nginx

4. Disable Autostart for Service

If you don't want the service to start whenever the system boots use the disable command to remove it from autostart:

sudo systemctl disable <name_of_service>

For example:

sudo systemctl disable nginx

Configure Autostart for User's Services

Systemd supports not only system services but also services which can be started and managed by regular users. You don't required to have superuser privileges to manage them. Just use the --user option with the systemctl command.

Usually, these services are started when the first user session starts and are stopped when the last user session ends. If you want to get a list of the current user's services that are started automatically, use the following command:

systemctl list-unit-files --user --type=service --state=enabled

Use the enable command with the --user option to make the service start automatically. For example, if you want pipewire to start automIX-03B-OPatically use this command:

systemctl enable --user pipewire

Disabling autostart for user's services works similarly:

systemctl disable --user pipewire

If you want autostart services for a specific user not on the first session start but at the system startup you should enable lingering for this user. Run the following command to do this:

sudo loginctl enable-linger <username>

For example, run this command to enable lingering for the user with the name sergiy:

sudo loginctl enable-linger sergiy

After this you can ensure that this feature works by authorizing from another user and checking the service status for the previous user. For example:

sudo systemctl --user --machine=sergiy@ status pipewire --no-pager

Wrapping Up

In this article, I have explained how to configure the service to start automatically in Linux. As you can see it is pretty straightforward with the Systemd initialization system.

Rate the Article

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...
Creative Commons License
The article is distributed under Creative Commons ShareAlike 4.0 license. Link to the source is required .

Leave a Comment