← System admin on a Raspberry Pi

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.

Samba is installed, but nobody can reach the share. Is it broken?

Programs on a server have to be running before anyone can use them. A service is a program started by the operating system and kept running. The operating system's job is to start, stop and watch services, which is why one command, systemctl, controls them all.

How this lesson works: Predict, Try, Investigate, Modify, Practise, Make
PredictRead the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.

Enable

smbd is stopped. You type sudo systemctl enable smbd. Is smbd running now?

TryTry it on the board and compare the result with your prediction.

Try: start it and keep it

Was your prediction right?

Try it yourselfYour edits are saved in this browser.

smbd is stopped and will not start at boot. Make it run now and at every boot.

Use the board. This ticks when you get there.

InvestigateWork out why it happens, one step at a time.

Worked example: Two different switches

Worked exampleBefore each step, say what you think comes next.
  1. systemctl status smbd\text{systemctl status smbd}is it running now, and will it start at boot?

Now try it yourself.

What is the difference between start and enable?

Sam thinks the share is up. Click the line where it first goes wrong.

Where can you read why a service stopped or failed?

ModifyChange the problem on purpose and see what happens.

Vary: change one thing

Try it yourselfYour edits are saved in this browser.

smbd is running and enabled. Stop it now and stop it starting at boot.

Use the board. This ticks when you get there.

smbd is running but not enabled. Reboot with sudo reboot. Is smbd running afterwards?

Use the board. This ticks when you get there.

Read smbd's log with journalctl -u smbd.

Use the board. This ticks when you get there.

PractiseA few on your own, to check you have it.

Practise

status shows: active (running), disabled. You reboot. What happens to the service?

Which single command starts a service now and sets it to start at every boot?

Review: Sam adds alex to a group with usermod -G. What went wrong?

MakeMake your own problem using what you have learned. Expect a few tries; that is normal.

Survive a reboot

Then: on a real Pi, run systemctl status ssh and systemctl list-units --type=service.

Try it yourselfYour edits are saved in this browser.
Need a hint?

Before a hint: what kind of stuck is this? Try a strategy first

It runs now but would not after a reboot.

Another hint

Enable it first, then reboot.

Another hint

sudo systemctl enable smbd, then sudo reboot, then systemctl status smbd.

smbd is running but would vanish at a reboot. Fix that, then reboot and check that it is running.

Use the board. This ticks when you get there.

Now you can
Next lesson5. Share files with SambaSamba shares a folder so Windows, Mac and Linux computers can use it. The share is described in a config file the service reads when it starts, so you restart it after a change. The folder's group and permissions decide who may write.
For teachers

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. About 40 minutes.

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

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.

The hard part (on purpose)

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

When students get stuck

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).

Where it leads

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

What each step is for