Sub topic 3
Anatomy of smb.conf
This chapter covers how Samba's configuration file is organized and how its parts interact. It assumes you can edit files as root, know Linux users, groups, and permissions, and can manage services with systemctl. The next chapter creates the Samba users that the shares here refer to.
Where the file lives and how it is read
Samba reads one main file, normally /etc/samba/smb.conf. Both smbd (file serving) and nmbd (name service, if used) read it. smbd rechecks the file periodically and reloads it when it changes. You can force a reload with sudo smbcontrol all reload-config. Changes to some global settings, such as the protocol version, only affect new connections. A full systemctl restart is the safest way to apply them, but it disconnects clients.
The format is simple:
- A line in square brackets, such as
[global], starts a section. - A line of the form
name = valuesets a parameter in the current section. - Lines starting with
#or;are comments. - Parameter names ignore case and ignore the difference between spaces and underscores in most cases, so
read onlyandRead_Onlymean the same thing. - A trailing backslash continues a value on the next line.
- Booleans accept
yes/no,true/false, and1/0.
The three kinds of sections
Every section name is either [global], a special service name, or the name of a share.
[global]
[global] holds settings that describe the server as a whole, such as its role, its protocol limits, and its logging. It also supplies default values for share-level parameters. If you set read only = no in [global], every share becomes writable unless it says otherwise. Put share-specific values in the share instead, so one share cannot become writable by accident.
Special sections
Two section names have built-in meaning:
[homes]creates a share for each authenticated user, pointing at that user's home directory. The share name becomes the user's name.[printers]exposes the system's print queues.
A file server that only shares directories usually needs neither. Omit them so they do not expose anything.
Ordinary shares
Any other name creates a share, and clients see that name. The name is not the directory name. It is a label, and path decides which directory it serves. Share names are case-insensitive for clients, so Projects and projects collide.
A minimal working file
This file is the one used throughout the tutorial. It has one [global] section and one share.
[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 parameters fall into two groups.
| Parameter | Section | Meaning |
|---|---|---|
| `workgroup` | global | The NetBIOS workgroup or domain name. Clients in a different workgroup can usually still connect by name. |
| `server role` | global | `standalone server` means Samba keeps its own user database and is not part of a domain. |
| `map to guest` | global | `never` rejects failed logins instead of silently treating them as guest. |
| `server min protocol` | global | The oldest SMB version the server accepts. `SMB2` refuses the obsolete SMB1. |
| `path` | share | The directory on the server that the share exposes. |
| `read only` | share | `no` allows writes. Its inverse is `writable = yes`. |
| `valid users` | share | Who may connect. `@staff` means members of the Unix group `staff`. |
| `force group` | share | Files are created with this group, whichever user creates them. |
| `create mask`, `directory mask` | share | The maximum permission bits for new files and new directories. |
Samba never grants more than the Linux filesystem allows. The effective access is the intersection of the share settings and the Unix permissions on the directory. A share with read only = no still fails for a user whose group has no write permission on path.
Variables and includes
Values can contain substitution variables that Samba fills in per connection. The most useful ones are:
%U: the session username.%S: the name of the current share.%m: the client's NetBIOS name.%h: the server's hostname.
This lets one section serve many users. The following [homes] share limits each share to its owner, and the share name is replaced by the user's name:
[homes]
comment = Home directories
browseable = no
read only = no
valid users = %SYou can also split a large file with include = /etc/samba/shares.conf. Variables work in the filename too, for example include = /etc/samba/users/%U.conf. Keep the include approach simple. Debugging is much harder when the effective configuration is spread over many files.
Checking the parsed result
testparm parses the file the way smbd does and reports errors. The -s option prints the effective configuration without waiting for a keypress.
sudo testparm -sLook at three things in its output:
- Warnings about unknown parameters. A typo such as
raed only = nois reported asUnknown parameter encountered, and Samba ignores the line. - Warnings that a parameter is in the wrong section. For example, a global-only parameter inside a share produces
Global parameter ... found in service section!, and it has no effect. - The
Loaded services file OKline. If this line is missing,smbdmay refuse to load the file or load only part of it.
testparm also shows values that you did not set. It prints the non-default values, so a short file expands into only the settings that differ from Samba's built-in defaults. Use testparm -sv to see every parameter, including the defaults.
Common mistakes
- Editing without checking. Run
testparm -safter every edit, before you reload. - Typos that are silently ignored. Samba skips unknown parameters, so a misspelled
valid usersleaves the share open to every authenticated user. - Putting access rules in
[global]. Avalid usersline in[global]applies to every share, including future ones. Keep it in the share. - Mixing up
force groupandvalid users.valid usersdecides who may connect.force groupdecides which Unix group the files get. They do not substitute for each other. - Expecting masks to add permissions.
create mask = 0664only removes bits beyond 0664. It cannot make a file more open than the client requested. - Using
;mid-line. Comment characters only work at the start of a line. Putting# noteafter a value makes it part of the value in many parameters.
Exercise
You add read only = no to [global], and later add a new share [archive] with no read only line. Is [archive] writable for clients, and how would you make it read-only?
Show answer
Yes, it inherits read only = no from [global], so it is writable as far as Samba is concerned (the Unix permissions on its path still apply). To make it read-only, set read only = yes inside [archive], because a value in a share overrides the global default. A better fix is to remove read only = no from [global] and set it only in the shares that need writing.
Exercise
testparm -s prints Unknown parameter encountered: "valid user" for your share, but still ends with Loaded services file OK. What is the effect on the share, and what do you change?
Show answer
The parameter is ignored, so the share has no user restriction and any authenticated user can connect. The correct name is valid users (with an s). Fix the spelling, run testparm -s again to confirm the warning is gone, then reload the configuration.