← Teacher guide

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.

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

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)

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

When students get stuck

What you will seeA question to askThe fix
Permission denied writing smb.conf.Are you using sudo echo with >> ?echo ... | sudo tee -a file.
The share does not appear.Did you restart smbd?sudo systemctl restart smbd.

Answers to the checks

  1. no restart: You add a [classshare] section to smb.conf but do not restart smbd. Does smbclient -L localhost list classshare?
    No. Services read their config when they start. After a change, restart (or reload) the service.
  2. Try: add a share: Add a share called classshare to /etc/samba/smb.conf, restart smbd, then run smbclient -L localhost.

    echo -e "[classshare]\npath = /srv/class" | sudo tee -a /etc/samba/smb.conf, then sudo systemctl restart smbd. tee, because sudo echo ... >> would still be your shell writing.
  3. Writing to a file you do not own: Why does sudo echo "[x]" >> /etc/samba/smb.conf fail?
    The >> runs in your shell, not under sudo
    Your shell sets up the redirect before sudo runs, so it has no permission to write the file. Use echo ... | sudo tee -a file, so tee (running as root) does the writing.
  4. Find the mistake: Sam fixes the share. Click the line where it first goes wrong.
    line 2
    Line 2: 777 lets everyone do everything. The cause was that Alex isn't in the folder's group. Fix that: sudo chgrp class /srv/class and chmod 775.
  5. Who can add files: /srv/class is drwxrwxr-x, owner root, group class. Who can add files?
    Root and members of class
    Owner (rwx) and group (rwx) can write; everyone else (r-x) can only read.
  6. Group the folder: Make /srv/class belong to the group class.

    sudo chgrp class /srv/class.
  7. Let the group write: /srv/class belongs to class, but Alex still cannot save a file there. Fix the permissions.

    sudo chmod 775 /srv/class: the group needs w.
  8. Take rights away: Someone set /srv/class to 777. Bob is not in class and should only be able to read. Fix it.

    sudo chmod 775 /srv/class (or o-w): others lose w.
  9. Others: A folder is rwxrwxr-x with group class. Alex is not in class. What can Alex do?
    Read it, but not add files
    Others get r-x: look in, but not change.
  10. After a change: You change smb.conf. What must you do for Samba to use it?
    Restart (or reload) smbd
    smbd reads its settings at start-up.
  11. Review: enable: Review: you ran sudo systemctl enable smbd. Is smbd running now?
    Maybe not: enable only acts at the next boot
    Check with systemctl status.
  12. a class hand-in share: Share /srv/class as [class] so that Alex (in group class) can save files there.

    Add the [class] share with its path, restart smbd, set the folder's group to class and mode 775.

Solutions

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

Vary: change one thing › Group the folder

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

Vary: change one thing › Let the group write

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

Vary: change one thing › Take rights away

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

a class hand-in share

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

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.

Exit ticket

Why does smbd need a restart after you edit smb.conf?

It reads the file when it starts, not every time it is used.

What each step is for

Where it leads

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