← Teacher guide

Services: start, stop and enable

A service is a program that runs in the background so others can use it. systemctl starts and stops one now, and enable decides whether it starts at every boot. These are two separate things.

System admin on a Raspberry Pi · Serve · about 40 minutes · Open the student lesson

Classroom resources

Open these in Waypoints Prepare with one click. To share a file another way, copy its address and paste it into Load URL, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Try 8 min · Worked example 10 min · Vary 10 min · Practise and Make 12 min

Before class

Try the boards yourself.

The one idea: the operating system starts and watches background programs. A service's state and its boot setting are separate.

Simplifications: the pretend Pi has five services (ssh, cron, smbd, jellyfin, unattended-upgrades). reboot is instant. On a real Pi systemctl status pages its output: press q to leave.

Other options: service smbd start is the older form of the same thing; Docker or containers are another way to run a service (parked).

With internet: services that others reach over the internet are a bigger risk than ones on the school network, because anyone can try them (see the last lesson).

Running the lesson

  1. Predict: wrong answers think enable starts it.
  2. Try: ask which command did which job.
  3. Worked example: show status before and after on the board.
  4. Vary: after the reboot task, ask which services a server must have enabled.
  5. Make: ask why it is proved by rebooting.

In class

On the slides at these points. Students can do the whole lesson on their own; these are for the room. In the deck, the Run, Modify and Make slides have the live playground in them (the tutorial link is the backup), and the notes above are in each slide's speaker notes.

The hard part (on purpose)

Enable versus start. Make students say both out loud: 'now' and 'at boot'.

When students get stuck

What you will seeA question to askThe fix
A student enabled the service but cannot connect.Is it running? What does status say?sudo systemctl start name, or enable --now.
systemctl says 'Interactive authentication required'.Did you use sudo?sudo systemctl ...

Answers to the checks

  1. enable: smbd is stopped. You type sudo systemctl enable smbd. Is smbd running now?
    No. enable decides what happens at boot. To run it now you need start (or enable --now).
  2. Try: start it and keep it: smbd is stopped and will not start at boot. Make it run now and at every boot.

    sudo systemctl start smbd, and sudo systemctl enable smbd. Two commands, because now and at boot are different.
  3. Two different switches: What is the difference between start and enable?
    start runs it now; enable runs it at every boot
    A service has a state (running or not now) and a boot setting (will it start next time). systemctl status shows both.
  4. Find the mistake: Sam thinks the share is up. Click the line where it first goes wrong.
    line 3
    Line 3 is where Sam's belief starts: no error doesn't mean running. enable only changes the boot setting. Check with systemctl status smbd.
  5. Read the log: Where can you read why a service stopped or failed?
    journalctl -u name
    journalctl -u smbd prints that service's log. It is the first place an administrator looks.
  6. Switch it off: smbd is running and enabled. Stop it now and stop it starting at boot.

    sudo systemctl stop smbd, and sudo systemctl disable smbd.
  7. After a reboot: smbd is running but not enabled. Reboot with sudo reboot. Is smbd running afterwards?

    Not running: it was never enabled, so it did not start at boot. This is why enable matters on a server.
  8. Read the log: Read smbd's log with journalctl -u smbd.

    journalctl -u smbd shows when it started and stopped.
  9. Reading status: status shows: active (running), disabled. You reboot. What happens to the service?
    It stays off after the reboot
    Running now and starting at boot are separate. This one was running but disabled.
  10. Both at once: Which single command starts a service now and sets it to start at every boot?
    sudo systemctl enable --now name
    enable --now does both in one go.
  11. Review: -aG: Review: Sam adds alex to a group with usermod -G. What went wrong?
    -G without -a replaces all Alex's groups
    -aG adds and keeps the rest.
  12. survive a reboot: smbd is running but would vanish at a reboot. Fix that, then reboot and check that it is running.

    enable first, then reboot to prove it. Never trust a service you have not rebooted with.

Solutions

One possible solution for each task. Other correct programs are fine.

Vary: change one thing › Switch it off

Open task: any program that does what the step asks.

Vary: change one thing › After a reboot

Open task: any program that does what the step asks.

Vary: change one thing › Read the log

Open task: any program that does what the step asks.

survive a reboot

Open task: any program that does what the step asks.

Stretch and support

Stretch: Find out what a unit file is and where the Pi keeps them.

Support: Two sticky notes: NOW (start, stop) and BOOT (enable, disable).

Exit ticket

smbd is running but disabled. What happens after a reboot?

It stays off: disabled means it does not start at boot.

What each step is for

Where it leads

Links: Networks (ports and servers); next: Share files with Samba.