1. Overview

Most Linux distributions use systemd as a contemporary service manager. Usually, we need to become root to control the services. However, we can also allow ordinary users to handle services on their own. This method is often called rootless.

In this tutorial, we’ll learn to install, manage and control services on a per-user basis.

2. Creating and Adding a Sample Service

First, let’s write a simple Bash script user_service for the service:


while true
    echo "User $me at $now"
    sleep 10

So, the script prints the date and user name. Then, we need to copy the script to the /usr/local/bin directory and make sure that the user has executable permission on it.

Next, let’s prepare the service’s unit file user_service.service:

Description=Script Daemon For Test User Services



Since the entries User and Group are meaningless for user service, we’ve commented them out. Next, we redirect the script’s output to log_file with the StandardOutput entry. It’s worth noting that the %h modifier stands for the user’s home directory.

Now, with the sudo privilege let’s copy the unit file to the /etc/systemd/user directory. In this way, systemd regards the service as the user’s one. Moreover, the service is available for all users.

3. The user Option

We can manage services as regular users with the help of the user option of systemctl. Thus, services can be enabled/disabled, started/stopped, and so on without the sudo privilege.

So, let’s install the service:

$ systemctl --user daemon-reload

Afterwards, let’s start it:

$ systemctl --user start user_service.service

Next, let’s check its status:

$ systemctl --user status user_service.service
● user_service.service - Script Daemon For Test User Services
     Loaded: loaded (/etc/xdg/systemd/user/user_service.service; disabled; vendor preset: enable>
     Active: active (running) since Thu 2023-01-12 19:23:14 CET; 28s ago
   Main PID: 4935 (user_service)
      Tasks: 2 (limit: 18982)
     Memory: 580.0K
        CPU: 16ms
     CGroup: /user.slice/user-1000.slice/[email protected]/app.slice/user_service.service
             ├─4935 /bin/bash /usr/local/bin/user_service
             └─4972 sleep 10

sty 12 19:23:14 ubuntu systemd[1511]: Started Script Daemon For Test User Services.

Again, we should note that we’ve done that all without the sudo command.

4. Enabling, Disabling, and the Service’s Lifetime

Now let’s enable or disable the service with systemctl:

$ systemctl --user enable user_service.service
Created symlink /home/joe/.config/systemd/user/default.target.wants/user_service.service → /etc/xdg/systemd/user/user_service.service.

Thus, once enabled, the service starts automatically after our login. Then, it’ll be running as long as we have some open sessions. In other words, the service instance is bound to the user, not to the session.

Finally, let’s disable it:

$ systemctl --user disable user_service.service

5. Toggling Services of All Users

With the root privilege, we can enable or disable the service for all users with the global option of systemctl:

$ sudo systemctl --global enable user_service.service
Created symlink /etc/systemd/user/default.target.wants/user_service.service → /etc/systemd/user/user_service.service.

Consequently, all users obtain their own running instance of the service immediately after login.

6. Extending Service’s Life

Let’s assume that we intend our service to perform long-running tasks, e.g., calculation. So, we should extend its life beyond the end of the last session. In that case, we use loginctl with the enable-linger command:

$ loginctl enable-linger

Now, our services start right after the system boot and run till the shutdown. We should keep in mind that this applies to all our systemd services. Finally, we can turn it off with disable-linger.

7. Inspecting Service’s Log

Now let’s check the service’s log with the journalctl command. Once again, we’ll use the user option:

$ journalctl --user -u user_service
# ...
Jan 12 19:50:20 ubuntu systemd[1511]: Stopped Script Daemon For Test User Services.
Jan 12 19:50:24 ubuntu systemd[1511]: Started Script Daemon For Test User Services.

8. Conclusion

In this article, we looked at user services managed by systemd.

First, we created a simple service and added it to the systemd using the administrator privilege. Then, we managed the service as a non-sudoer thanks to the user option of systemctl.

Subsequently, we took a look at the lifetime of service and learned how to start the service without login. Finally, we examined the service’s log.

Comments are closed on this article!