Lab 4 of 8 · Services and boot with systemd
Run your own service with systemd
Turn a small status page into a service that starts at boot, restarts after a crash and is sandboxed, then read its logs.
- Time
- 45–60 minutes
- Cost
- Free.
What you learn
systemd starts everything on a modern Linux server, restarts it when it crashes and collects its logs. Writing and debugging service units is a core sysadmin skill.
- Units and services, and enabling them at boot
- Restart policies that bring a crashed service back
- Reading service logs with journalctl
- Sandboxing a service with systemd security options
In the AI era: AI tools write unit files quickly, and sometimes add options that do not exist or break the service. systemd can check a unit and score its security, so you test before you trust.
Get set up
Commands use Homebrew in the Terminal app. Already have a tool? Skip it.
- A terminalOfficial install page ↗
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"Use the Terminal app. The command installs Homebrew, which the other macOS commands use.
Before you start
Finish lab 1.
Start your server and open your repository: run
multipass start sysadmin-labandmultipass shell sysadmin-labon your computer, thencd linux-admin-labon the server.
Step by step
1 Create the status page
This is the page your service will serve.
sudo mkdir -p /srv/status echo "<h1>sysadmin-lab is up</h1>" | sudo tee /srv/status/index.htmlYou should see:The
<h1>line is printed back.2 Write the service unit
Create it with
sudo nano /etc/systemd/system/status-page.service.Restart=on-failurebrings it back after a crash, andDynamicUser=yesruns it as a throwaway user instead of root.[Unit] Description=Status page for sysadmin-lab After=network-online.target Wants=network-online.target [Service] ExecStart=/usr/bin/python3 -m http.server 8080 --directory /srv/status Restart=on-failure RestartSec=2 DynamicUser=yes [Install] WantedBy=multi-user.target3 Start it and enable it at boot
daemon-reloadmakes systemd read your new file;enable --nowstarts it and starts it again at every boot.sudo systemctl daemon-reload sudo systemctl enable --now status-page systemctl status status-page --no-pager curl -s http://localhost:8080You should see:active (running), then your<h1>line.4 Watch it come back after a crash
Killing the process simulates a crash.
sudo systemctl kill --signal=SIGKILL status-page sleep 3 systemctl status status-page --no-pager | head -n 4You should see:Still
active (running), with a new process ID and a start time a few seconds ago.5 Read its logs
Everything the service prints, and everything systemd did to it, is in the journal.
journalctl -u status-page --since "10 minutes ago" --no-pager | tail -n 15You should see:The start, the kill and the automatic restart, each with a time.
6 Ask AI to harden it, then let systemd score it
Note the exposure score first. Then ask your AI assistant: "Add systemd sandboxing options to this unit that do not break a Python web server serving /srv/status on port 8080." Add its options to the
[Service]section withsudo nano /etc/systemd/system/status-page.service. Keep only options that passverifyand leave the page working.systemd-analyze security status-page --no-pager | tail -n 1 # after editing the unit: sudo systemd-analyze verify /etc/systemd/system/status-page.service sudo systemctl daemon-reload sudo systemctl restart status-page curl -s http://localhost:8080 systemd-analyze security status-page --no-pager | tail -n 1You should see:verifyprints nothing, the page still loads, and the exposure score is lower than before.7 Keep a copy in your repository
Servers get rebuilt; the copy in Git is how you rebuild them the same way.
mkdir -p config cp /etc/systemd/system/status-page.service config/ git add config git commit -m "Add hardened status-page systemd service" git push
You are done when
status-pageis enabled and came back by itself after being killed.systemd-analyze verifypasses and the exposure score went down.config/status-page.serviceis on GitHub.
We check your public GitHub repository and give you a page to share with recruiters, and can add the project to your resume.