Short answer

Write a unit file in /etc/systemd/system/myapp.service with User=, ExecStart= (the full path to the program) and Restart=on-failure, plus WantedBy=multi-user.target. Then run sudo systemctl daemon-reload and sudo systemctl enable --now myapp. It starts now and at every boot; read its output with journalctl -u myapp.

Tested on Ubuntu 26.04 and 24.04, two separate runs. On 24.04, every step is the same. Everything that differed

Before you start

  • A sudo user on Ubuntu 26.04 or 24.04.
  • The program you want to run. This guide uses a small Python web app so every step can be shown with real output; your own program (a Node or Go app, a script, a jar) goes in ExecStart= the same way.

Step 1: Put the program in place

Server
$ sudo mkdir -p /opt/hello
Server
$ sudo tee /opt/hello/app.py > /dev/null <<'EOF'import http.server, os PORT = int(os.environ.get('PORT', '8000')) class Hello(http.server.BaseHTTPRequestHandler):    def do_GET(self):        self.send_response(200)        self.end_headers()        self.wfile.write(b'hello from a systemd service\n') print(f'listening on 127.0.0.1:{PORT}', flush=True)http.server.HTTPServer(('127.0.0.1', PORT), Hello).serve_forever()EOF

It prints one line when it starts and answers web requests on port 8000 (or the port in PORT). systemd needs the full path to the program, so look it up:

Server
$ command -v python3 python/usr/bin/python3
Output hidden (1 line)

Only python3 exists. Ubuntu has no python command, and a unit that says /usr/bin/python fails with 203/EXEC (see below).

Step 2: Give it its own user

Without User=, a service runs as root. A user of its own, with no home and no login shell, means a bug in the program can only touch what that user may:

Server
$ sudo useradd --system --no-create-home --shell /usr/sbin/nologin hello
Server
$ id hellouid=999(hello) gid=986(hello) groups=986(hello)
Output hidden (1 line)

A system user (number below 1000) with its own group.

Step 3: Write the unit file

Server
$ sudo tee /etc/systemd/system/hello.service <<'EOF'[Unit]Description=Hello web appAfter=network.target [Service]User=helloGroup=helloWorkingDirectory=/opt/helloEnvironment=PORT=8000ExecStart=/usr/bin/python3 /opt/hello/app.pyRestart=on-failureRestartSec=2 [Install]WantedBy=multi-user.targetEOF
  • After=network.target: start after the network is set up.
  • User= and Group=: who it runs as.
  • WorkingDirectory=: its current folder, so relative paths in your program work.
  • Environment=: settings your program reads. For many settings or secrets, use EnvironmentFile=/etc/hello.env instead.
  • ExecStart=: the full path to the program and its arguments. No shell runs it, so cd, && and > do not work here.
  • Restart=on-failure and RestartSec=2: if it crashes, start it again after 2 seconds.
  • WantedBy=multi-user.target: what enable hooks it to, so it starts at boot.

Check the file before you use it:

Server
$ sudo systemd-analyze verify /etc/systemd/system/hello.service/usr/lib/systemd/system/xfs_scrub_all.service:26: Support for option CPUAccounting= has been removed and it is ignored/usr/lib/systemd/system/system-xfs_scrub.slice:15: Support for option CPUAccounting= has been removed and it is ignored
Output hidden (2 lines)

Nothing about hello.service means the file is fine. On Ubuntu 26.04 it also printed two warnings about xfs_scrub units that Ubuntu itself ships (an option newer systemd ignores); they are not about your service. On 24.04 it printed nothing.

Step 4: Start it now and at every boot

Server
$ sudo systemctl daemon-reload
Server
$ sudo systemctl enable --now helloCreated symlink '/etc/systemd/system/multi-user.target.wants/hello.service' → '/etc/systemd/system/hello.service'.
Output hidden (1 line)

daemon-reload makes systemd read the new file. enable creates that link so it starts at boot; --now also starts it now. start alone would not survive a reboot.

Server
$ systemctl status hello --no-pager● hello.service - Hello web app     Loaded: loaded (/etc/systemd/system/hello.service; enabled; preset: enabled)     Active: active (running) since Thu 2026-11-19 22:48:47 UTC; 2s ago Invocation: 4ce30f47b79144998de20fd45bddd385   Main PID: 2863 (python3)      Tasks: 1 (limit: 1486)     Memory: 9.1M (peak: 9.1M)        CPU: 46ms     CGroup: /system.slice/hello.service             └─2863 /usr/bin/python3 /opt/hello/app.py
Output hidden (10 lines)

enabled (starts at boot) and active (running), with the program’s process under it. It answers, and it runs as hello, not root:

Server
$ curl -s http://127.0.0.1:8000/hello from a systemd service
Output hidden (1 line)
Server
$ ps -o user=,cmd= -C python3hello    /usr/bin/python3 /opt/hello/app.py
Output hidden (1 line)

Step 5: Read its output

Everything the program prints goes to the systemd journal, with no log file to set up:

Server
$ sudo journalctl -u hello --no-pager -n 5Nov 19 22:48:47 web-server systemd[1]: Started hello.service - Hello web app.Nov 19 22:48:47 web-server python3[2863]: listening on 127.0.0.1:8000Nov 19 22:48:50 web-server python3[2863]: 127.0.0.1 - - [19/Nov/2026 22:48:50] "GET / HTTP/1.1" 200 -
Output hidden (3 lines)

systemd’s own line, the program’s print, and the request from the curl above. Add -f to follow new lines as they come.

Step 6: See it restart after a crash

Kill the program the hard way, as a crash would:

Server
$ sudo systemctl kill -s KILL hello
Server
$ sudo journalctl -u hello --no-pager -n 6Nov 19 22:48:52 web-server systemd[1]: hello.service: Killed unit cgroup '/system.slice/hello.service' with SIGKILL on client request.Nov 19 22:48:52 web-server systemd[1]: hello.service: Main process exited, code=killed, status=9/KILLNov 19 22:48:52 web-server systemd[1]: hello.service: Failed with result 'signal'.Nov 19 22:48:54 web-server systemd[1]: hello.service: Scheduled restart job, restart counter is at 1.Nov 19 22:48:54 web-server systemd[1]: Started hello.service - Hello web app.Nov 19 22:48:54 web-server python3[3373]: listening on 127.0.0.1:8000
Output hidden (6 lines)

The process died (status=9/KILL), and 2 seconds later systemd started a new one: Scheduled restart job, restart counter is at 1. A clean exit (code 0) is not a failure, so on-failure leaves it stopped; use Restart=always for a program that should run even after it exits cleanly.

Server
$ systemctl is-active helloactive
Output hidden (1 line)

Change the unit file: “changed on disk”

Edit the file, here to move the app to port 8001, and ask for the status before anything else:

Server
$ sudo sed -i 's/PORT=8000/PORT=8001/' /etc/systemd/system/hello.service
Server
$ systemctl status hello --no-pager | grep -i warningWarning: The unit file, source configuration file or drop-ins of hello.service changed on disk. Run 'systemctl daemon-reload' to reload units.
Output hidden (1 line)

systemd still runs the old version: a change takes effect only after daemon-reload, and the running program keeps its old settings until a restart. Do both:

Server
$ sudo systemctl daemon-reload && sudo systemctl restart hello
Server
$ curl -s http://127.0.0.1:8001/hello from a systemd service
Output hidden (1 line)

If it fails with “status=203/EXEC”

A second unit with the most common mistake: python instead of python3 in ExecStart=.

Server
$ sudo sed -n '/ExecStart/p' /etc/systemd/system/broken.serviceExecStart=/usr/bin/python /opt/hello/app.py
Output hidden (1 line)
Server
$ sudo systemctl start broken

systemctl start printed nothing, as if it worked. The status tells the truth:

Server
$ systemctl status broken --no-pager× broken.service     Loaded: loaded (/etc/systemd/system/broken.service; static)     Active: failed (Result: exit-code) since Thu 2026-11-19 22:49:03 UTC; 2s ago   Duration: 12ms Invocation: f3db49ccf2634c21887bfed451ba980e    Process: 4401 ExecStart=/usr/bin/python /opt/hello/app.py (code=exited, status=203/EXEC)   Main PID: 4401 (code=exited, status=203/EXEC)   Mem peak: 2M        CPU: 5ms
Output hidden (9 lines)

203/EXEC is systemd’s code for “could not run the program in ExecStart=”: the path is wrong, the file is not executable, or a script’s #! line names a missing interpreter. Check the path itself:

Server
$ ls -l /usr/bin/pythonls: cannot access '/usr/bin/python': No such file or directory
Output hidden (1 line)

Fix the path, then daemon-reload and start again. The journal sometimes adds a line naming the missing file (on 26.04 it did here), but not always, so ls -l on the path is the sure test:

Server
$ sudo journalctl -u broken --no-pager -n 4Nov 19 22:49:03 web-server (python)[4401]: broken.service: Unable to locate executable '/usr/bin/python': No such file or directoryNov 19 22:49:03 web-server (python)[4401]: broken.service: Failed at step EXEC spawning /usr/bin/python: No such file or directoryNov 19 22:49:03 web-server systemd[1]: broken.service: Main process exited, code=exited, status=203/EXECNov 19 22:49:03 web-server systemd[1]: broken.service: Failed with result 'exit-code'.
Output hidden (4 lines)

If it fails with “status=217/USER”

The same unit, now with the user name mistyped:

Server
$ sudo sed -n '/User=/p' /etc/systemd/system/broken.serviceUser=helo
Output hidden (1 line)
Server
$ sudo systemctl start broken
Server
$ systemctl status broken --no-pager× broken.service     Loaded: loaded (/etc/systemd/system/broken.service; static)     Active: failed (Result: exit-code) since Thu 2026-11-19 22:49:08 UTC; 2s ago   Duration: 6ms Invocation: 225c2efdd5cb4faea14fc6f404fef177    Process: 4963 ExecStart=/usr/bin/python3 /opt/hello/app.py (code=exited, status=217/USER)   Main PID: 4963 (code=exited, status=217/USER)   Mem peak: 1.7M        CPU: 4ms
Output hidden (9 lines)
Server
$ id heloid: 'helo': no such user
Output hidden (1 line)

217/USER: the user in User= does not exist, so systemd could not switch to it. Create the user (step 2) or fix the name.

Step 7: Lock it down

systemd can rate how much of the system a service can reach:

Server
$ systemd-analyze security hello --no-pager | tail -n 1→ Overall exposure level for hello.service: 9.2 UNSAFE 😨
Output hidden (1 line)

Add settings in a drop-in file instead of editing the unit: a drop-in survives if the main file is replaced, and shows clearly what you added. sudo systemctl edit hello opens an editor on the same file; here it is written directly:

Server
$ sudo mkdir -p /etc/systemd/system/hello.service.d
Server
$ sudo tee /etc/systemd/system/hello.service.d/hardening.conf <<'EOF'[Service]NoNewPrivileges=yesProtectSystem=strictProtectHome=yesPrivateTmp=yesPrivateDevices=yesProtectKernelTunables=yesProtectKernelModules=yesProtectControlGroups=yesRestrictSUIDSGID=yesCapabilityBoundingSet=EOF

The whole system is read-only to it (ProtectSystem=strict), home folders are hidden, it gets its own /tmp and no devices, cannot gain privileges, and has no special capabilities. A program that must write somewhere needs that folder in ReadWritePaths=.

Server
$ sudo systemctl daemon-reload && sudo systemctl restart hello
Server
$ systemd-analyze security hello --no-pager | tail -n 1→ Overall exposure level for hello.service: 4.8 OK 🙂
Output hidden (1 line)
Server
$ curl -s http://127.0.0.1:8001/hello from a systemd service
Output hidden (1 line)

From 9.2 UNSAFE to 4.8 OK, and the app still answers. systemctl cat shows every file systemd reads for the service:

Server
$ systemctl cat hello --no-pager | grep -E "^# /"# /etc/systemd/system/hello.service# /etc/systemd/system/hello.service.d/hardening.conf
Output hidden (2 lines)

Step 8: Check it comes back after a reboot

Server
$ sudo reboot
Server
$ systemctl is-active hello && systemctl is-enabled helloactiveenabled
Output hidden (2 lines)
Server
$ curl -s http://127.0.0.1:8001/hello from a systemd service
Output hidden (1 line)

Remove the service

Server
$ sudo systemctl disable --now helloRemoved '/etc/systemd/system/multi-user.target.wants/hello.service'.
Output hidden (1 line)
Server
$ sudo rm -r /etc/systemd/system/hello.service /etc/systemd/system/hello.service.d && sudo systemctl daemon-reload
Server
$ systemctl status hello --no-pagerUnit hello.service could not be found.
Output hidden (1 line)
Server
$ sudo userdel hello && sudo rm -r /opt/hello

The configuration files, ready to copy

These are the two files as they were on the test server after the reboot, with the service running (port 8001 from the change above). Change the user, paths and program for your own service.

The unit file
/etc/systemd/system/hello.service
[Unit]Description=Hello web appAfter=network.target [Service]User=helloGroup=helloWorkingDirectory=/opt/helloEnvironment=PORT=8001ExecStart=/usr/bin/python3 /opt/hello/app.pyRestart=on-failureRestartSec=2 [Install]WantedBy=multi-user.target

Read back from the test server (Ubuntu 26.04) on 2026-11-19, after the steps had worked. Placeholders such as [password]: type your own value in the box above the file to fill them in, or replace them yourself.

The hardening drop-in
/etc/systemd/system/hello.service.d/hardening.conf
[Service]NoNewPrivileges=yesProtectSystem=strictProtectHome=yesPrivateTmp=yesPrivateDevices=yesProtectKernelTunables=yesProtectKernelModules=yesProtectControlGroups=yesRestrictSUIDSGID=yesCapabilityBoundingSet=

Read back from the test server (Ubuntu 26.04) on 2026-11-19, after the steps had worked. Placeholders such as [password]: type your own value in the box above the file to fill them in, or replace them yourself.

Troubleshooting

  • status=203/EXEC: the program in ExecStart= cannot be run (wrong path, python instead of python3, not executable). Details.
  • status=217/USER: the user in User= does not exist. Details.
  • “changed on disk. Run ‘systemctl daemon-reload’”: you edited the file; reload, then restart. Details.
  • It does not start after a reboot: it was started but never enabled. Run sudo systemctl enable hello. Details.
  • It stays stopped after it exits: Restart=on-failure does not restart a clean exit; use Restart=always. Details.

What these steps changed on the server

Both runs recorded the server before the first step and after the reboot (see the Tested on box below): the unit file and its drop-in, the boot link, the hello user, the app in /opt/hello, and a program listening on 127.0.0.1:8001. No package was added.

Ubuntu 26.04 vs 24.04

The same check ran on a fresh 26.04 server and on a fresh 24.04 server, each with its own run log. Every step worked the same; these outputs differed:

WhatUbuntu 26.04Ubuntu 24.04
SystemUbuntu 26.04.1 LTS7.0.0-43-genericUbuntu 24.04.5 LTS6.8.0-147-generic
systemd259.9-0ubuntu0.1255.4-1ubuntu8.18
python33.14.3-0ubuntu23.12.3-0ubuntu2.1
systemd-analyze verify on a good unittwo warnings about Ubuntu’s own xfs_scrub units (CPUAccounting= removed)nothing
The journal line for a missing user (217/USER)Failed to determine credentials for user 'helo': Unknown userFailed to determine user credentials: No such process
systemctl statusan extra Invocation: line (an id for each start)no Invocation: line
ProofRun log, 36 checks passedRun log, 36 checks passed