Sub topic 5
Defining shares and permissions
This chapter assumes you can edit smb.conf, you know Linux users, groups and chmod, and you have created at least one Samba user with smbpasswd -a. It covers how a share section is written and how Samba decides who can read or write what.
Two layers of access control
Samba checks access twice, and a request must pass both checks.
- Share-level rules in
smb.conf: who may connect (valid users), whether the share is writable (read only), and which identity is used on disk (force user,force group). - Filesystem rules: the ordinary owner, group and mode bits (and ACLs) on the directory and files, applied to the Linux user the connection maps to.
The result is the more restrictive of the two. A share marked read only = no still fails to write if the Linux directory is not writable by the mapped user. A directory with mode 777 is still blocked if valid users excludes the caller.
A complete share definition
This smb.conf defines a standalone server with one writable share for members of the staff group.
[global]
workgroup = WORKGROUP
server role = standalone server
map to guest = never
server min protocol = SMB2
[projects]
path = /srv/samba/projects
read only = no
valid users = @staff
force group = staff
create mask = 0664
directory mask = 0775The [global] section sets server-wide behavior. map to guest = never rejects failed logins instead of silently turning them into guest access. server min protocol = SMB2 refuses the old, insecure SMB1 dialect.
Each other section is a share, and the section name (projects) is the name clients see.
The share parameters
| Parameter | Meaning |
|---|---|
| `path` | Directory on the server that the share exposes. It must exist. |
| `read only` | `yes` by default. Set `no` to allow writes. The synonym `writable = yes` means the same. |
| `valid users` | Only these users may connect. `@staff` means every member of the Linux group `staff`. |
| `force group` | Files are accessed with this group as the primary group, whatever the user's own group is. |
| `create mask` | Permission bits allowed on new files. Samba removes any bit not in the mask. |
| `directory mask` | The same, for new directories. |
Other parameters you will meet often are browseable = no (hide the share from listings, which is not a security control), guest ok = yes (allow unauthenticated access) and write list (users who may write even on a read-only share).
Preparing the directory
The configuration only works if the directory and the accounts match it. The commands below create them.
sudo groupadd staff
sudo mkdir -p /srv/samba/projects
sudo chgrp staff /srv/samba/projects
sudo chmod 2775 /srv/samba/projects
sudo useradd -M -s /usr/sbin/nologin -G staff alice
sudo smbpasswd -a alice
sudo testparm -s
sudo systemctl restart smbdPoints worth understanding:
chmod 2775sets the setgid bit (the leading2). New files and subdirectories inherit the directory's group,staff, instead of the creator's primary group. Together withforce group = staff, this keeps everything group-owned bystaff.775gives the owner and group read, write and execute. Others can only read and traverse.-M -s /usr/sbin/nologincreates a user with no home directory and no shell login. The account exists only so that Samba can map to it.smbpasswd -acreates the Samba password, which is stored separately from the Linux password. The Linux account must exist first.- On Red Hat-family systems the unit is
smb, notsmbd.
How masks interact with modes
create mask = 0664 means a new file may have at most rw-rw-r--. If a Windows client creates a file, Samba derives the Unix mode from the DOS attributes, then removes any bit outside the mask. The result is 664: the owner and group can write, and others can only read.
The mask only restricts bits. It never adds them. A mask of 0664 cannot make a file executable, even if the client asks for that.
Two related parameters change the picture:
force create modeandforce directory modeadd bits after the mask is applied. Useforce create mode = 0660to guarantee that group members can always write.inherit permissions = yesmakes new files copy the permissions of the parent directory. It overrides the masks, so use one approach or the other.
Read-only access for some users
To give some users read access and others write access, combine read only = yes with write list.
[reports]
path = /srv/samba/reports
read only = yes
valid users = @staff, @auditors
write list = @staffMembers of auditors can read. Members of staff can write, as long as the Linux permissions on /srv/samba/reports also allow staff to write. The share rule grants the right to try, and the filesystem has the final say.
Common errors
| Symptom | Likely cause |
|---|---|
| `NT_STATUS_LOGON_FAILURE` | Wrong password, or the user has no Samba password (`smbpasswd -a` was never run). |
| `NT_STATUS_ACCESS_DENIED` on connect | The user is not in `valid users`, or the group membership changed and the client has not reconnected. |
| Share is visible but writes fail | `read only` is still `yes`, or the Linux directory is not writable by the mapped user. |
| Files created with the wrong group | The setgid bit or `force group` is missing. |
| Changes to `smb.conf` seem ignored | Wrong file edited, or a parameter is overridden in the share section. Check with `testparm -s`. |
Group changes take effect only on a new connection. After adding a user to staff with usermod -aG, the user must disconnect and reconnect to the share.
Exercise
A share has read only = no, valid users = @staff and force group = staff. The directory is drwxr-xr-x root root. User alice is in staff and connects successfully, but cannot save a file. Why, and what two commands fix it?
Show answer
Share-level rules allow writing, but the filesystem does not. The directory is owned by root with group root, and neither group nor others have write permission. The staff group has no write access to it. Fix it with sudo chgrp staff /srv/samba/projects and sudo chmod 2775 /srv/samba/projects. The 2 sets the setgid bit so that new files inherit the staff group.
Exercise
With create mask = 0664, a client creates a file and asks for mode 0777. What mode does the file get, and which parameter would you add to make sure group members can always write to it?
Show answer
The file gets mode 0664, because the create mask removes every bit that is not in the mask. To guarantee group write access, add force create mode = 0660, which sets those bits after the mask is applied.