← System admin on a Raspberry Pi

Accounts, groups and sudo

Each person gets an account and a private home folder. Groups give several accounts the same rights. Only users in the sudo group may use sudo, and the system logs every time they do.

One Pi, a whole class. How does it know who is allowed to do what?

This is authentication (who are you?) and authorisation (what may you do?) from Who are you, and what may you do?, on a real kind of system: accounts say who, groups and permissions say what.

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.

Alex's folder

You create an account for Alex with sudo adduser alex. Can Alex open your home folder, /home/pi?

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

Try: make an account

Was your prediction right?

Try it yourselfYour edits are saved in this browser.

Make an account for alex with sudo adduser alex. Then look at its folder with ls -ld /home/alex.

Use the board. This ticks when you get there.

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

Worked example: Groups and sudo

Worked exampleBefore each step, say what you think comes next.
  1. sudo adduser alex\text{sudo adduser alex}make the account, its group and its private home folder.

Now try it yourself.

Which group lets a user run sudo?

Sam gives Alex sudo. Click the line where it first goes wrong.

Where does the Pi record who used sudo?

ModifyChange the problem on purpose and see what happens.

Vary: change one thing

Try it yourselfYour edits are saved in this browser.

Alex is in the group class. Add alex to sudo as well, keeping class.

Use the board. This ticks when you get there.

Read the log that records logins and sudo use. Use sudo.

Use the board. This ticks when you get there.

Become alex with su alex. Try to read /home/pi/Documents/readme.txt. What happens?

Use the board. This ticks when you get there.

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

Practise

Which command adds alex to the group class and keeps alex's other groups?

Why is it better to give people sudo through a group than to share the pi password?

Review: you ssh to the Pi, then type exit. Which account are you now?

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

A new administrator

Then: on a real Pi, run id to see your own groups.

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

Two steps: the account, then its rights.

Another hint

sudo adduser bob

Another hint

sudo usermod -aG sudo bob (or sudo adduser bob sudo)

Create an account for bob and give bob sudo.

Use the board. This ticks when you get there.

Now you can
Next lesson4. Services: start, stop and enableA 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.
For teachers

Each person gets an account and a private home folder. Groups give several accounts the same rights. Only users in the sudo group may use sudo, and the system logs every time they do. 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: accounts say who you are; groups and permissions say what you may do. This is authentication and authorisation (Security and privacy › Sign-in and permissions) on a real system.

Simplifications: the pretend Pi's pi account is not in the adm group, so reading /var/log/auth.log needs sudo (on a real Pi it does not). Passwords are always accepted. su alex starts a pretend session and exit returns.

On a real Pi: sudo adduser asks for a password and details; give each student their own account on the group's Pi so the log tells them apart. Never share the pi password.

Other options: useradd is the low-level version of adduser; visudo and /etc/sudoers.d/ give finer control over who may run what.

With internet: a school with many Pis would use one directory (for example LDAP or Microsoft Entra) rather than adding each account by hand.

Running the lesson

  1. Predict: wrong answers think 'logged in' means 'allowed everywhere'.
  2. Try: ask why a home folder is private by default.
  3. Worked example: stop at the -a question and ask what would happen to Alex's groups.
  4. Vary: after the log, ask what it would show if Alex had guessed passwords.
  5. Make: ask who should and should not get sudo.

The hard part (on purpose)

-a versus no -a on usermod. Show both on the board and run id after each.

When students get stuck

Stretch and support

Stretch: Find out what the root account is and why Raspberry Pi OS leaves it without a password.

Support: A card: adduser makes, usermod -aG adds to a group, id shows.

Where it leads

Links: Security and privacy › Sign-in and permissions; The command line › Who can read what; next: Services.

What each step is for