PleinIQ
Menu
Language

Choose your language

This page is only in English for now. Choosing a language opens its home page.

Other languages

Your browser can translate any page into almost any language.

Other browsers: Open the browser menu and look for “Translate”.

Your resume, your downloads and lab commands are never translated.

Roadmaps / Linux / Lab 4

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.

  • Install
    /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-lab and multipass shell sysadmin-lab on your computer, then cd linux-admin-lab on the server.

Step by step

  1. 1 Create the status page

    This is the page your service will serve.

    Run this on your lab server
    sudo mkdir -p /srv/status
    echo "<h1>sysadmin-lab is up</h1>" | sudo tee /srv/status/index.html
    You should see:

    The <h1> line is printed back.

  2. 2 Write the service unit

    Create it with sudo nano /etc/systemd/system/status-page.service. Restart=on-failure brings it back after a crash, and DynamicUser=yes runs it as a throwaway user instead of root.

    File: /etc/systemd/system/status-page.service
    [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.target
    
  3. 3 Start it and enable it at boot

    daemon-reload makes systemd read your new file; enable --now starts it and starts it again at every boot.

    Run this on your lab server
    sudo systemctl daemon-reload
    sudo systemctl enable --now status-page
    systemctl status status-page --no-pager
    curl -s http://localhost:8080
    You should see:

    active (running), then your <h1> line.

  4. 4 Watch it come back after a crash

    Killing the process simulates a crash.

    Run this on your lab server
    sudo systemctl kill --signal=SIGKILL status-page
    sleep 3
    systemctl status status-page --no-pager | head -n 4
    You should see:

    Still active (running), with a new process ID and a start time a few seconds ago.

  5. 5 Read its logs

    Everything the service prints, and everything systemd did to it, is in the journal.

    Run this on your lab server
    journalctl -u status-page --since "10 minutes ago" --no-pager | tail -n 15
    You should see:

    The start, the kill and the automatic restart, each with a time.

  6. 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 with sudo nano /etc/systemd/system/status-page.service. Keep only options that pass verify and leave the page working.

    Run this on your lab server
    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 1
    You should see:

    verify prints nothing, the page still loads, and the exposure score is lower than before.

  7. 7 Keep a copy in your repository

    Servers get rebuilt; the copy in Git is how you rebuild them the same way.

    Run this on your lab server
    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-page is enabled and came back by itself after being killed.

  • systemd-analyze verify passes and the exposure score went down.

  • config/status-page.service is on GitHub.

Finished? Get your proof page

We check your public GitHub repository and give you a page to share with recruiters, and can add the project to your resume.

Join PleinIQ Updates

What are you interested in?

Read how we handle your information in our privacy notice.