← System admin on a Raspberry Pi

Share files with Samba

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

Everyone in the class needs to hand in work to one place. How do you set that up?

File sharing is the most common job for a small server. Two things have to be right: the service has to know about the share (a config file), and the files on the Pi have to allow the right people (owners, groups and permissions from lesson 3).

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.

No restart

You add a [classshare] section to smb.conf but do not restart smbd. Does smbclient -L localhost list classshare?

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

Try: add a share

Was your prediction right?

Try it yourselfYour edits are saved in this browser.

Add a share called classshare to /etc/samba/smb.conf, restart smbd, then run smbclient -L localhost.

Use the board. This ticks when you get there.

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

Worked example: Writing to a file you do not own

Worked exampleBefore each step, say what you think comes next.
  1. echo -e "[classshare]npath = /srv/class" | sudo tee -a /etc/samba/smb.conf\text{echo -e "[classshare]\\npath = /srv/class" | sudo tee -a /etc/samba/smb.conf}add the share, as root.

Now try it yourself.

Why does sudo echo "[x]" >> /etc/samba/smb.conf fail?

Sam fixes the share. Click the line where it first goes wrong.

/srv/class is drwxrwxr-x, owner root, group class. Who can add files?

ModifyChange the problem on purpose and see what happens.

Vary: change one thing

Try it yourselfYour edits are saved in this browser.

Make /srv/class belong to the group class.

Use the board. This ticks when you get there.

/srv/class belongs to class, but Alex still cannot save a file there. Fix the permissions.

Use the board. This ticks when you get there.

Someone set /srv/class to 777. Bob is not in class and should only be able to read. Fix it.

Use the board. This ticks when you get there.

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

Practise

A folder is rwxrwxr-x with group class. Alex is not in class. What can Alex do?

You change smb.conf. What must you do for Samba to use it?

Review: you ran sudo systemctl enable smbd. Is smbd running now?

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

A class hand-in share

Then: on a real Pi, run testparm to check smb.conf and smbpasswd -a alex to give Alex a Samba password (Samba keeps its own).

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

Three jobs: the share, the restart, the folder's group and mode.

Another hint

tee -a to add [class] and path = /srv/class to smb.conf, then restart smbd.

Another hint

sudo chgrp class /srv/class and sudo chmod 775 /srv/class.

Share /srv/class as [class] so that Alex (in group class) can save files there.

Use the board. This ticks when you get there.

Now you can
Next lesson6. Host Jellyfin, a media serverJellyfin serves your films and music to any device on the network, through a web page on port 8096. It runs as its own user, so it can only read files that user is allowed to read. A firewall decides which ports can be reached.
For teachers

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

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

Students will be able to

Before class

Try the boards yourselves.

The one idea: a service reads its settings at start-up, and the files it serves have owners, groups and permissions that decide who may use them.

Simplifications: the pretend smb.conf only needs [name] and path. The real one also wants read only = no, valid users = @class and a Samba password for each user (sudo smbpasswd -a alex), because Samba's passwords are separate from the Pi's. Say this before students try it for real.

Taught: Samba (works with Windows, Mac and Linux). Other options to name: NFS (file sharing between Linux computers, simpler but with no passwords), Nextcloud (a web page for files, like your own Drive, needs a web server and database), Syncthing (copies folders between computers).

With internet: sharing outside the school needs a VPN (Tailscale or WireGuard) or the files can be read by anyone who finds the port. Do not open router ports for Samba.

Running the lesson

  1. Predict: wrong answers think the service watches its file.
  2. Try: show the failing sudo echo version first.
  3. Worked example: stop at 777 and ask who else it lets in.
  4. Vary: after taking rights away, ask whether Bob is stopped from reading.
  5. Make: ask what a real hand-in folder would also need.

The hard part (on purpose)

The sudo-and-redirect gotcha. Run the failing command so students read the error.

When students get stuck

Stretch and support

Stretch: Find out why Samba needs its own password for each user.

Support: A card with the three jobs: share, restart, group.

Where it leads

Links: Who can read what (permissions); Accounts, groups and sudo; next: Host Jellyfin.

What each step is for