16 min read
Linux services and packages: systemd and apt
In one sentence: A service is a program that keeps running without depending on you. On Linux, systemd starts services when the machine boots, watches over them and starts them again if they crash; apt installs those programs from a trusted catalog.
What you will learn
- Explain what a daemon is and why, on a server, systemd starts it and not you.
- Start, stop, enable and check services with systemctl, and read what they write with journalctl.
- Write a minimal unit file to turn any program into a service.
- Install, update and remove programs with apt.
Before you start, read: 04-users-and-permissions.md 05-processes-and-signals.md
The problem
Section titled “The problem”Imagine you already have your first website working on your practice server. You started it with a command, tried it with curl and it answered. You close the terminal, go to sleep and, in the morning, nobody answers. The program died with your session.
And even if you leave it open: who starts it again if it crashes at three in the morning, or if the server restarts after an update? A real server can’t depend on someone keeping a terminal open.
And before that, another question: where does the program come from, if a server has no browser to download it with? This lesson solves both things: installing programs with apt and keeping them running with systemd.
The analogy
Section titled “The analogy”The analogy
Think of a restaurant. The cooks and the waiters are the programs. None of them opens the place on their own: every morning the manager arrives, opens up and calls the people on shift.
For each position, the manager has a sheet: who it is, what they do, when they start and what to do if they leave in the middle of the shift (call them back). Everything that happens goes into the logbook. The manager is systemdThe first program Linux starts when it boots, process number 1. It starts the programs that must always be running (the services), restarts them if they crash and keeps what they write in a journal.Go to definition, the sheets are the unit files and the logbook is its journal.
The equipment (the ovens, the coffee machine) comes from a trusted supplier with a catalog, who delivers each appliance with the parts it needs and leaves it installed. That supplier is apt.
Where the analogy breaks down
- The manager only checks that the employee is still there, not that they’re working well. A frozen server that doesn’t answer can still show up as “running.”
- A real manager would find someone else; systemd starts the same program again. If the fault is in the sheet, it will fail the same way every time, and after several attempts in a row it gives up.
- The supplier doesn’t tell you about its new products: you have to ask for the catalog yourself.
How it really works
Section titled “How it really works”A daemon is a program that doesn’t depend on you
Section titled “A daemon is a program that doesn’t depend on you”When you install nginx, a web server, in “Try it,” it will answer on port 80 without you starting it. It will keep answering when you log out and after you restart the machine.
A program like that is a DaemonA program that keeps running on its own, with no window or terminal, waiting to do its job. Web servers are daemons, and many have a d at the end of their name.Go to definition: a ProcessA running program. When you start a server, the operating system creates a process that stays alive waiting for requests.Go to definition that keeps running on its own, with no window or terminal, waiting to do its job. Many have a d at the end of their name: sshd is the SSH server and systemd, the one that watches over all the others.
What you start from the terminal, on the other hand, hangs from it: as you saw in lesson 5, “Processes and signals”, when you close it, its processes receive a signal (SIGHUP) and end.
systemd starts everything else
Section titled “systemd starts everything else”When you turn on a Linux machine, the first thing that starts is its kernel, the core of the system, the part that talks to the computer’s hardware. The kernel launches a single program, which starts everything else:
- You turn on the machineor the VM
starts
- The Linux kernelthe core of the system
launches the first process
- systemdprocess number 1
reads the sheets and starts
- The enabled servicesnginx, sshd, your website…
That program is systemd, and it always has PID 1. Almost every Linux used on servers runs it, Ubuntu included.
Each program that systemd keeps running is a service, with a named sheet (nginx.service) that says what to start and how to look after it.
systemctl: the remote control for each service
Section titled “systemctl: the remote control for each service”You give systemd orders with systemctl, almost always with the same pattern: systemctl + action + service:
| Command | What it does |
|---|---|
systemctl status nginx |
Says whether it’s running, its PID and its last log lines |
sudo systemctl start nginx |
Starts it now |
sudo systemctl stop nginx |
Stops it now |
sudo systemctl restart nginx |
Stops it and starts it again |
sudo systemctl enable nginx |
Signs it up to start every time the machine boots |
sudo systemctl disable nginx |
Takes it off that list |
systemctl list-units --type=service |
Lists every service systemd has loaded |
Looking needs no permissions, but changing a service affects the whole machine: that’s why those commands carry sudo, which runs them as root (lesson 4).
The difference that confuses people the most is start versus enable. start acts now and leaves nothing written down. enable only signs it up for the next boot. For both at once, sudo systemctl enable --now nginx.
journalctl: the services’ journal
Section titled “journalctl: the services’ journal”What a service prints on screen, and what systemd does with it, goes to one place: the journal, the logbook from the analogy. You read it with journalctl:
journalctl -u nginx: everything about one service (-ufor unit, the sheet).journalctl -u nginx -n 20: only the last 20 lines.journalctl -u nginx -f: shows new lines as they arrive, liketail -fin lesson 2. You quit with Ctrl + C.
If the output doesn’t fit in the window, it opens in a pager like the one man uses: Space goes down and Q quits.
The service sheet: the unit file
Section titled “The service sheet: the unit file”Each service is described in a text file, its unit file. This is the one you’ll write in “Try it” for a web server made with Python:
[Unit]Description=Course practice website
[Service]User=ubuntuWorkingDirectory=/home/ubuntu/webExecStart=/usr/bin/python3 -m http.server 8000Restart=always
[Install]WantedBy=multi-user.targetIt has three parts, and each line is a name, an = and a value:
[Unit]: what it is.Descriptionis the name you’ll see instatusand in the journal.[Service]: how it runs.ExecStartis the command that starts the program, with the full path.Usersays which user it runs as. Without that line, it would run as root, with every permission: exactly what lesson 4 advises against.WorkingDirectoryis the folder it starts from, as if you didcdfirst.Restart=always: if the program ends, for whatever reason, systemd starts it again. It only stays stopped if you stop it withsystemctl stop, or if it fails so many times in a row that systemd gives up (you’ll see it in the challenge).
[Install]: whatenabledoes.WantedBy=multi-user.targetmeans “start it on a normal boot.”
The sheets of installed programs are in /usr/lib/systemd/system/, and yours go in /etc/systemd/system/.
systemd reads the sheets once and keeps them in memory. If you change one, tell it with sudo systemctl daemon-reload, or it will keep using the old version.
apt installs programs from a trusted catalog
Section titled “apt installs programs from a trusted catalog”On Ubuntu, programs are installed as packages: the program, its configuration and, if it’s a server, its systemd sheet, all in one file. They’re in Ubuntu’s repositories, servers on the internet that act as a catalog. The one that finds, downloads and installs them is apt, a Package managerA program that installs, updates and removes other programs from a catalog, and also brings the ones each of them needs to work. On Ubuntu it’s apt, on a Mac, Homebrew, and for Node.js code, npm. Some catalogs are looked after by a team, like Ubuntu’s; in others, like npm, anyone can publish.Go to definition.
| Command | What it does |
|---|---|
sudo apt update |
Downloads the updated catalog: which packages there are and in which version. It installs nothing |
sudo apt install nginx |
Installs nginx and the packages it needs to work |
sudo apt upgrade |
Updates what you already have to the catalog’s versions |
sudo apt remove nginx |
Uninstalls it (with purge instead of remove, it also deletes its configuration) |
apt search and apt show |
Search the catalog and describe a package, without sudo |
nginx uses files that come in another package, nginx-common, and apt installs it too. Those packages are called dependencies.
And why “trusted”? Ubuntu signs its catalog with its private key, and apt checks the Digital signatureA few bytes someone computes from a message with their private key. With the public key, anyone can check that the message hasn’t changed and that the owner of that key signed it.Go to definition with the public key that already comes with the system, as you saw in lesson 8 of Phase 0. If someone swaps a package along the way, the signature doesn’t match and apt rejects it. Downloading a program from some random website gives you no such guarantee.
On your Mac: launchd and Homebrew
Section titled “On your Mac: launchd and Homebrew”macOS has its own equivalent of systemd, launchd, which is also process number 1. And Homebrew is a package manager like apt: brew install nginx installs it and brew services start nginx keeps it running with launchd. You won’t use them in this course.
On your Mac: who is process 1?
ps -p 1What you will see:
PID TTY TIME CMD 1 ?? 56:20.99 /sbin/launchdps -p 1 shows the process with PID 1. Your TIME, the processor time it has used so far, will be different.
Try it
Section titled “Try it”The goal: install a real web server, create your own service and check that it survives being killed and the machine restarting. At the end you’ll break it on purpose and look for the cause.
Everything happens on your practice server, the course VM from lesson 4: log in with multipass shell course. You’ll use sudo, because installing programs and touching services changes the whole system, but everything stays inside the VM.
First, nginx from apt
Section titled “First, nginx from apt”1. Who is process 1?
ps -p 1What you will see:
PID TTY TIME CMD 1 ? 00:00:00 systemdIn the VM, process 1 is systemd. Check that the prompt says ubuntu@course (in WSL, your user): if it says ana@laptop, you’re on your Mac and you’ll see launchd.
2. Ask for the catalog
sudo apt updateWhat you will see:
Get:1 http://ports.ubuntu.com/ubuntu-ports noble InRelease [256 kB]…Get:19 http://ports.ubuntu.com/ubuntu-ports noble-security/main arm64 Packages [1448 kB]Fetched 35.4 MB in 4s (9916 kB/s)Reading package lists... DoneBuilding dependency tree... DoneReading state information... Done2 packages can be upgraded. Run 'apt list --upgradable' to see them.apt downloads the catalog of each repository (noble is Ubuntu 24.04). The last line warns that there are new versions: sudo apt upgrade would install them. Your numbers will be different, and with an Intel or AMD processor you’ll see archive.ubuntu.com and amd64 instead of ports.ubuntu.com and arm64.
3. Install nginx
sudo apt install -y nginxWhat you will see:
Reading package lists... DoneBuilding dependency tree... DoneReading state information... DoneThe following additional packages will be installed:nginx-common…The following NEW packages will be installed:nginx nginx-common…Setting up nginx-common (1.24.0-2ubuntu7.18) ...Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.Setting up nginx (1.24.0-2ubuntu7.18) ...…-y answers “yes” to apt’s question. Notice two things:
additional packages: the dependency,nginx-common, comes in on its own.Created symlink: that’s whatenabledoes. The package has enabled the service, so nginx is already running and will start on every boot.
We’ve removed parts (…), and your version may be newer.
4. Look at the service
systemctl status nginxWhat you will see:
● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: enabled) Active: active (running) since Mon 2026-10-12 13:28:48 UTC; 4s ago Docs: man:nginx(8)… Main PID: 384 (nginx)…Look at three lines:
Loaded: where its sheet is, andenabled, meaning it will start on boot.Active: active (running): it’s running, and since when.Main PID: the number of its main process. Yours will be different.
If the command doesn’t give you the prompt back, quit with Q.
5. Ask it for the page
curl localhostWhat you will see:
<!DOCTYPE html><html><head><title>Welcome to nginx!</title>…<h1>Welcome to nginx!</h1><p>If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.</p>…Your VM is already a web server: nginx answers on port 80, the one curl uses if you don’t tell it another, with its welcome page. If you get Couldn't connect to server, the service isn’t active: check the previous step.
6. Stop it and start it again
sudo systemctl stop nginxsystemctl is-active nginxcurl localhostsudo systemctl start nginxWhat you will see:
inactivecurl: (7) Failed to connect to localhost port 80 after 0 ms: Couldn't connect to serveris-active answers with one word. With nginx stopped, nobody is listening on port 80 and curl can’t connect. start starts it again.
7. Read its journal
journalctl -u nginxWhat you will see:
Oct 12 13:28:48 course systemd[1]: Starting nginx.service - A high performance web server and a reverse proxy server...Oct 12 13:28:48 course systemd[1]: Started nginx.service - A high performance web server and a reverse proxy server.Oct 12 13:28:52 course systemd[1]: Stopping nginx.service - A high performance web server and a reverse proxy server...Oct 12 13:28:52 course systemd[1]: nginx.service: Deactivated successfully.Oct 12 13:28:52 course systemd[1]: Stopped nginx.service - A high performance web server and a reverse proxy server.Oct 12 13:28:52 course systemd[1]: Starting nginx.service - A high performance web server and a reverse proxy server...Oct 12 13:28:52 course systemd[1]: Started nginx.service - A high performance web server and a reverse proxy server.Each line carries the date, the machine and who writes it: systemd[1] is systemd, with its PID in brackets. It’s the story of steps 3 and 6: it started when it was installed, you stopped it and you started it again.
If you get No journal files were opened due to insufficient permissions, your user can’t read the system journal: repeat the command with sudo in front.
Leave nginx installed: you’ll use it in lesson 9, “SSH”, to open a tunnel.
Then, your own service
Section titled “Then, your own service”Now you’ll turn a program that doesn’t come ready for it into a service. Python comes with a minimal web server: python3 -m http.server 8000 serves, on port 8000, the files in the folder where you start it. First, create that folder with a page:
8. A page to serve
mkdir -p ~/webecho '<h1>Hello from my service</h1>' > ~/web/index.htmlThey print nothing: if there’s no error, the page is in ~/web/index.html.
Now, the sheet. Open it with nano, as in lesson 2. It needs sudo because /etc belongs to the system:
9. Write the sheet
sudo nano /etc/systemd/system/web-course.serviceCopy into it the unit file from “The service sheet”, as is. In WSL, change ubuntu to your user in User and in WorkingDirectory. Save with Ctrl + O and Enter, and quit with Ctrl + X.
10. Enable it and start it
sudo systemctl daemon-reloadsudo systemctl enable --now web-courseWhat you will see:
Created symlink /etc/systemd/system/multi-user.target.wants/web-course.service → /etc/systemd/system/web-course.service.daemon-reload makes systemd read the new sheet, and enable --now enables it and starts it at the same time. It’s the same Created symlink as with nginx.
11. Check it
systemctl status web-coursecurl localhost:8000What you will see:
● web-course.service - Course practice website Loaded: loaded (/etc/systemd/system/web-course.service; enabled; preset: enabled) Active: active (running) since Mon 2026-10-12 13:28:57 UTC; 4ms ago Main PID: 470 (python3)…<h1>Hello from my service</h1>It’s running, and the last line is your page. Write down the Main PID: you need it in the next step. If curl says Couldn't connect to server, try again in a second: Python takes a moment to start listening.
12. Kill it and see what happens
kill 470systemctl status web-courseWhat you will see:
● web-course.service - Course practice website Loaded: loaded (/etc/systemd/system/web-course.service; enabled; preset: enabled) Active: active (running) since Mon 2026-10-12 13:29:00 UTC; 662ms ago Main PID: 483 (python3)…Oct 12 13:29:00 course systemd[1]: web-course.service: Scheduled restart job, restart counter is at 1.Oct 12 13:29:00 course systemd[1]: Started web-course.service - Course practice website.Replace 470 with your Main PID. kill asks the process to end, as in lesson 5, and doesn’t need sudo because the process belongs to your user. The service is still active (running), but with a different Main PID: the last two lines say that systemd scheduled a restart (Scheduled restart job) and started another process, because of Restart=always.
One last test is left: restarting the machine. Leave the VM with exit, restart it from your terminal with multipass restart course and log back in with multipass shell course. In WSL, close Ubuntu, run wsl --shutdown in PowerShell and open it again.
13. After restarting
systemctl is-active nginx web-coursecurl localhost:8000What you will see:
activeactive<h1>Hello from my service</h1>Nobody started anything by hand: systemd started both on boot, because they’re enabled.
The challenge: break it and find the cause
Section titled “The challenge: break it and find the cause”On a real server, services break. Open the sheet again with sudo nano /etc/systemd/system/web-course.service and change http.server to http.sever (without the second r). Save, quit and apply the change:
14. Break it
sudo systemctl daemon-reloadsudo systemctl restart web-coursesystemctl is-active web-coursecurl localhost:8000What you will see:
failedcurl: (7) Failed to connect to localhost port 8000 after 0 ms: Couldn't connect to serverrestart doesn’t complain, but the service has ended up failed. If is-active says activating or active, repeat it in a couple of seconds: systemd is still trying.
curl only knows that nobody answers; the reason is in the journal. Before you read on, look for it yourself.
15. Find the cause
journalctl -u web-course -n 12What you will see:
Oct 12 13:29:21 course python3[137]: /usr/bin/python3: No module named http.severOct 12 13:29:21 course systemd[1]: web-course.service: Main process exited, code=exited, status=1/FAILUREOct 12 13:29:21 course systemd[1]: web-course.service: Failed with result 'exit-code'.Oct 12 13:29:22 course systemd[1]: web-course.service: Scheduled restart job, restart counter is at 3.Oct 12 13:29:22 course systemd[1]: Started web-course.service - Course practice website.Oct 12 13:29:22 course python3[139]: /usr/bin/python3: No module named http.severOct 12 13:29:22 course systemd[1]: web-course.service: Main process exited, code=exited, status=1/FAILUREOct 12 13:29:22 course systemd[1]: web-course.service: Failed with result 'exit-code'.Oct 12 13:29:22 course systemd[1]: web-course.service: Scheduled restart job, restart counter is at 4.Oct 12 13:29:22 course systemd[1]: web-course.service: Start request repeated too quickly.Oct 12 13:29:22 course systemd[1]: web-course.service: Failed with result 'exit-code'.Oct 12 13:29:22 course systemd[1]: Failed to start web-course.service - Course practice website.There are two voices. python3[139] is the program, and it states the problem: No module named http.sever, nothing with that name exists. systemd[1] says that the program ended with an error (status=1/FAILURE) and that it tried again until it gave up: Start request repeated too quickly.
Fix it: put http.server back in the sheet and repeat daemon-reload and restart. Check with curl localhost:8000 that your page is back. If restart replies Job for web-course.service failed, you’ve been faster than systemd: after giving up, it waits ten seconds before letting you start it again.
If you want to remove the service when you’re done, sudo systemctl disable --now web-course stops it and takes it off the boot list.
You have already seen it
Section titled “You have already seen it”- The windowless programs on your computer. The one that syncs your photos or the one that checks for updates are there, with no window: they’re daemons.
- Your phone’s app store. It’s a package manager: a trusted catalog, signed programs and updates that come from the same place.
- The Phase 0 server. The
nc -l 8080from lesson 2 stopped with Ctrl + C or when you closed the terminal. It was a server, but not a service.
And if you code:
- npm is a package manager:
npm installalso brings in your dependencies’ dependencies, like apt withnginx-common. npm run devdies when you close the terminal, like the Python server. In production, your Node app is kept alive by systemd, by a tool like pm2 or, in Phase 7, by Docker. And with systemd, everyconsole.logends up injournalctl.
Common mistakes
Section titled “Common mistakes”- Using
startand expecting it to survive a restart.startonly starts it this time. For every boot,enable, orenable --nowfor both. - Changing the sheet and forgetting
daemon-reload. systemd keeps the old version, and warns you:Warning: The unit file, source configuration file or drop-ins of web-course.service changed on disk. Run 'systemctl daemon-reload' to reload units. - Installing without
apt update. On a freshly created server, the catalog may be empty, andsudo apt install nginxrepliesE: Unable to locate package nginx; or old, and the download fails with404 Not Found. Runsudo apt updatefirst. - Stopping at
systemctl status. It only shows a few lines of the journal, sometimes cut off. The cause is usually further up: look for it withjournalctl -u. - Forgetting
User=in the sheet. The service will run as root. If someone finds a bug in your program, they’ll be able to do whatever they want with the machine.
Summary
Section titled “Summary”- A daemon is a program that runs on its own, with no terminal. On Linux, systemd, process number 1, starts them and watches over them.
systemctlmanages services:statusto look,start,stopandrestartfor now, andenablefor every boot.- What a service writes goes to the journal:
journalctl -u. That’s where the cause is when something fails. - A unit file of a few lines turns any program into a service. After changing it,
sudo systemctl daemon-reload. aptinstalls signed programs, with their dependencies. Firstapt update, thenapt install.
Did you get it?
Section titled “Did you get it?”To review:
Further reading
Section titled “Further reading”- systemd on the ArchWiki: systemctl, unit files and the journal, in more detail. It’s for another Linux, Arch, but systemd works the same.
- systemd.service: everything the
[Service]part of a unit file can contain. - Package management on Ubuntu Server: apt, the repositories and automatic updates.
- Use systemd in WSL, from Microsoft.