Sub topic 17
Choosing between Samba and NFS
This chapter assumes you can configure both services as covered earlier: a Samba share with smb.conf and an NFS export with /etc/exports. You also need to know how each protocol identifies users. The goal here is a decision method you can apply to a real requirement.
The working model: two different trust models
The two protocols answer one question differently: who is allowed to touch a file, and who vouches for that person?
- SMB (Samba) authenticates the user at connection time. The client sends a username and password (or a Kerberos ticket). The server checks them, maps the result to a Linux account, and performs every file operation as that account.
- NFS (with the default
sec=sys) trusts the client. The client sends numeric user and group IDs with each request. The server believes them, subject to the export's host restrictions androot_squash. NFSv4 with Kerberos (sec=krb5) changes this, but it takes extra setup.
Most other differences follow from this one. Samba suits environments where users log in as themselves from many kinds of machines. NFS suits environments where the client machines are managed by you and share a consistent identity scheme.
Comparison
| Aspect | Samba (SMB) | NFS |
|---|---|---|
| Native clients | Windows, macOS, Linux (cifs-utils) | Linux, Unix, macOS (Windows needs an optional client) |
| Authentication | Per-user password or Kerberos | Host and UID trust by default; Kerberos optional in NFSv4 |
| Identity on the wire | Account name | Numeric UID/GID (NFSv4 uses name@domain strings, mapped by idmapd) |
| Per-user access control | Samba accounts, `valid users`, share ACLs | Linux file permissions and export host rules |
| Typical ports | 445/TCP (139 legacy) | 2049/TCP for NFSv4 |
| Windows features | ACLs, offline files, shadow copies, printing | Not provided |
| POSIX behavior | Approximated (needs mount options and Samba extensions) | Close to native (permissions, symlinks, hard links, special files) |
| Setup effort | More: users, passwords, `smb.conf` | Less: one export line, if IDs line up |
Decision criteria
1. Client operating systems
If any Windows machine must reach the data, choose Samba. Windows ships an SMB client and no NFS client by default. If the clients are all Linux or Unix, NFS is usually the simpler and faster choice.
2. Who needs to be identified
Ask whether the server must know which person made a change. Samba requires a login, so access is tied to a named account, and smbstatus shows who has what open. With default NFS, the server only sees a UID sent by a client. Anyone with root on a permitted client can claim any UID except 0 (which root_squash remaps). That is acceptable on a trusted server-room network and a poor fit for laptops or shared lab machines.
3. Permission semantics
Linux software often expects real POSIX behavior: ownership changes, symlinks, hard links, sockets, and file locks that behave consistently. NFS provides these naturally. Over CIFS, some work only with mount options such as uid=, gid=, file_mode=, and dir_mode=, or with the Unix extensions (SMB 3.1.1 POSIX extensions on recent kernels and Samba versions). Databases, build trees, home directories, and container volumes generally behave better on NFS.
4. Identity management
NFS works smoothly only if user alice has the same UID on every machine. With a handful of hosts you can keep /etc/passwd in sync by hand, but that stops scaling quickly. A central directory (LDAP with SSSD, or a domain) solves it. Samba avoids this on the client side, because the client sends a name and the server decides the mapping. That is the main reason Samba works well in a mixed environment with no central directory.
5. Performance and overhead
Both protocols are fast enough for ordinary office files on a modern network. NFS has less per-operation overhead for many small Unix-style operations, such as compiling or listing large trees. SMB 3 has strong features of its own, including multichannel and encryption. Measure with your own workload before deciding on performance alone. A benchmark of large sequential copies says little about a workload of thousands of small files.
6. Firewall and operational footprint
Samba needs 445/TCP. NFSv4 needs only 2049/TCP, which is simple to filter. NFSv3 adds rpcbind, mountd, and lockd ports, which is why the NFSv4-only configuration is easier to secure. Samba carries more moving parts (accounts, a password database, possibly winbind), so it has more to maintain.
Worked scenarios
- A design office with Windows and macOS workstations sharing project folders. Choose Samba. Users need named logins, Windows ACL behavior, and no client-side software. Use a group-based share like the
[projects]example from earlier in this tutorial. - Ten Linux application servers sharing a read-mostly content directory. Choose NFS. Export read-only to the server subnet and mount at boot with
_netdev. Identity matters little because the data is read-only. - Linux developers' home directories mounted on several build hosts. Choose NFS, and make sure UIDs are consistent through LDAP or a managed user-creation process. POSIX behavior and speed matter here.
- Mixed: Linux servers plus a few Windows analysts reading the same reports. Serve the reports through Samba to the analysts. Mount the same directory by NFS to the servers only if file ownership and locking are managed carefully (see the warning below).
Running both services on the same directory
You can export one directory through both services. It works, but check these points first:
- Samba's
create mask,force group, andvalid userssettings are not applied to files created over NFS. The two paths can produce files with different ownership and modes. - Samba-side ACLs and Windows locking do not coordinate with NFS locks. Two clients writing the same file through different protocols can corrupt it.
- Use
setgiddirectories (chmod 2775) so that both paths create files with the same group.
A quick check on your own environment
Before choosing, gather the facts with a few commands. These show the client types, whether UIDs line up, and which services are already running.
# On the server: which sharing services are listening?
sudo ss -tlnp | grep -E ':(445|139|2049)\b'
# On two Linux machines: do UIDs match for the same person?
id alice
# On the server: who is connected right now (Samba)?
sudo smbstatus -bIf id alice returns different UIDs on two hosts, NFS with sec=sys will show the wrong file owners until you fix the IDs. That is a strong sign to either centralize identity or use Samba for that group of machines.
Common mistakes
- Choosing NFS because it is faster, then discovering Windows clients. Collect the client list first.
- Choosing Samba for Linux-only workloads that need symlinks, sockets, or exact permissions. The mount works, but programs fail in subtle ways.
- Using NFS on an untrusted network with
sec=sys. Any host in the export range can impersonate users. Restrict the export to specific hosts or move to Kerberos. - Mixing protocols on one directory without a plan. Pick a primary writer protocol.
Exercise
A company has 40 Windows laptops and 6 Linux servers. The laptops need to edit documents in a shared folder under their own logins. The servers only need to read those documents to generate reports. Which protocol or protocols would you use, and how would you configure the servers' access?
Show answer
Use Samba as the primary protocol, because the Windows laptops need named logins and Windows client support. For the Linux servers, either mount the same Samba share with mount.cifs using a read-only service account, or export the directory over NFS read-only (ro) to the servers' addresses. The NFS option gives the servers native POSIX behavior and avoids a password in a credentials file, but only do it if no server writes through NFS. Because the servers never write, the two paths cannot conflict on the same files.
Exercise
Two Linux hosts mount the same NFS export with sec=sys. User alice has UID 1001 on host A and UID 1005 on host B. What happens when she creates a file from host A and then lists it from host B, and what are two ways to fix it?
Show answer
The file is stored with owner UID 1001. On host B, UID 1001 maps to a different user (or to no user), so alice appears not to own her file and may be unable to modify it. Fix it by giving alice the same UID on both hosts (for example through LDAP with SSSD, or by recreating the account with a fixed UID), or by switching that group of machines to Samba, where the server maps the login name to one account.