Sub topic 1
How network file sharing works
Prerequisites
This chapter assumes you can already do the following:
- Use the Linux command line and edit text files.
- Read and change users, groups, and permissions with
chmodandchown. - Recognize IP addresses, subnets, and hostnames.
- Install packages and manage services with
systemctl.
You do not need to know Samba or NFS yet. This chapter builds the working model that the later chapters configure.
What a network file share is
A network file share is a directory on one machine, the server, that other machines, the clients, can open as if it were local. Programs on the client call the usual open, read, and write functions. The client's operating system turns those calls into network requests, sends them to the server, and returns the results.
The server owns the data, the disk, and the permission checks. The client holds only a mount point, which is an ordinary empty directory where the remote files appear.
Two protocols dominate on Linux:
- SMB (Server Message Block, also called CIFS in older documents) is the native protocol of Windows. On Linux, the Samba package provides the server side.
- NFS (Network File System) is the native protocol of Unix-like systems. The Linux kernel provides the server side.
Both protocols solve the same problem. They differ in how they identify users, how they handle state, and which clients they suit best.
The request path
A read on a mounted share passes through several layers. Knowing them helps you decide where to look when something breaks.
The key layers are:
- VFS (virtual filesystem). This kernel layer gives every filesystem the same interface. The application cannot tell a local ext4 directory from a remote share.
- Client driver. The
cifsmodule speaks SMB. Thenfsmodule speaks NFS. - Transport. Both protocols normally use TCP. Port 445 carries SMB, and port 2049 carries NFSv4.
- Server daemon.
smbdhandles SMB. The kernel NFS server (nfsd) handles NFS. - Server-side permission check. The server decides whether the request is allowed. A client cannot override this decision.
Who is asking? Identity and authorization
Every request must answer two questions: who is asking, and what may they do? The two protocols answer the first question differently, and that difference shapes most of the configuration you will write.
SMB: the user proves identity with a password
With SMB, the client sends a username and proves it knows the password. Samba checks the credentials against its own account database or against a domain. If they match, smbd maps the SMB user to a Linux user and performs all file operations as that user. Linux file permissions still apply, and Samba adds its own share-level rules on top.
NFS: the client vouches for the user
With the traditional NFS security mode (sec=sys), the client sends numeric user and group IDs (UIDs and GIDs) with each request. The server trusts those numbers. It does not check a password. If the client says UID 1000, the server treats the request as UID 1000.
This design is simple and fast. It is also the reason NFS access control rests on two things: which hosts may mount an export, and whether UIDs agree between machines. A user who is root on a client can claim any UID. Later chapters cover root_squash, UID mapping, and Kerberos-based security that closes this gap.
The following table compares the two models.
| Question | SMB (Samba) | NFS with `sec=sys` |
|---|---|---|
| How does the client identify a user? | Username and password | Numeric UID and GID sent by the client |
| Where is identity checked? | On the server, against a password database | On the client; the server trusts the numbers |
| Separate password database? | Yes, Samba keeps its own passwords | No |
| Main access control | User authentication plus share rules | Allowed hosts in `/etc/exports` plus file permissions |
| Typical clients | Windows, macOS, Linux | Linux and other Unix-like systems |
Authorization happens in layers
A request must pass every layer. The most restrictive layer wins.
- Network reachability. The firewall must allow the traffic, and the client must be able to reach the server.
- Service-level rules. For Samba, these are settings such as
valid usersinsmb.conf. For NFS, these are the client list and options in/etc/exports. - Linux file permissions. Owner, group, mode bits, and ACLs on the server's files.
- SELinux. On Red Hat-family systems, a file's security label must also permit the daemon to access it.
When a user gets "Permission denied", work through these layers in order. Most sharing problems come from a layer you forgot, not from a bug.
State, locking, and protocol versions
Protocols differ in how much the server remembers about each client.
SMB is stateful. The server tracks open files, sessions, and locks for each connected client. You can ask Samba who is connected and which files are open. Windows applications rely on this behavior for locking and for opportunistic locks (oplocks), which let a client cache a file for speed.
NFS began as stateless. NFSv2 and NFSv3 servers kept no per-client open-file state, and separate helper services handled mounting and locking. NFSv4 added state, such as open files and locks, and folded those helpers into one protocol on a single port. NFSv4 is the right default on modern systems.
| Version | Notes |
|---|---|
| SMB1 | Obsolete and insecure. Disable it. |
| SMB2 and SMB3 | Current. SMB3 adds encryption and better performance. Use these. |
| NFSv3 | Stateless. Needs several helper services and ports. Still common in older environments. |
| NFSv4.x | Stateful. Uses TCP port 2049 only. Supports stronger security. |
Later chapters set a minimum protocol version for both services.
Mounting: how a client attaches a share
A client attaches a share with the mount command. A share can also be mounted on demand or at boot, which later chapters cover. The basic form names the filesystem type, the remote location, and the local mount point.
# SMB: server name, share name
sudo mount -t cifs //fileserver/projects /mnt/projects -o username=alice,vers=3.0
# NFS: server name, exported path
sudo mount -t nfs4 fileserver:/srv/nfs/projects /mnt/projectsThe two syntaxes look different on purpose. SMB addresses a share by name (//server/share), and the server maps that name to a real path. NFS addresses an exported path (server:/path), and the client sees the server's directory layout.
After a successful mount, findmnt shows the filesystem type and options:
findmnt /mnt/projectsIf a server becomes unreachable, programs that use the mount can hang while the client retries. This is normal for network filesystems. It is why the client options chosen later (such as soft, hard, and _netdev) matter.
What to expect from the rest of the tutorial
The next chapters follow the layers described here. The SMB chapters cover the Samba daemons, smb.conf, Samba accounts, share permissions, verification, and client mounts. The NFS chapters cover versions, /etc/exports, client mounts, and UID mapping. After both, the tutorial covers persistent mounts, the firewall, SELinux, hardening, and troubleshooting, and ends by helping you choose between the two.
Common misunderstandings
- "A share is just a mounted disk." A share is a remote service with its own authentication and its own rules. A disk has none of these.
- "Windows needs Samba and Linux needs NFS, always." Linux clients can mount Samba shares, and some Windows editions can mount NFS. The split is a convention, not a rule.
- "Matching usernames are enough for NFS." With
sec=sys, the numeric UID must match. Two users namedalicewith different UIDs are different users to the server. - "The firewall is the only access control." A firewall controls reachability only. Authorization still depends on service rules, file permissions, and SELinux.
Exercise
A client mounts an NFS export using the default sec=sys security. On the client, the user bob has UID 1001. On the server, UID 1001 belongs to a different user, carol. Whose files can bob read on the share, and why?
Show answer
Bob acts as carol on the server. With sec=sys, the client sends only the numeric UID, and the server applies its own permissions for UID 1001, which is carol's account. The names do not matter. This is why UIDs must agree across machines, or be mapped, when you use NFS.
Exercise
A Samba user can see the share in smbclient -L but gets "Permission denied" when writing a file. Name the authorization layers you would check, in order.
Show answer
First check the Samba share rules in smb.conf, such as read only and valid users. Next check the Linux owner, group, and mode bits on the server directory, and any ACLs. Then check SELinux labels and booleans if the server uses SELinux. The firewall is not the cause, because the user already reached the server and listed the shares.
Exercise
Why does the server, not the client, decide whether a chmod on a mounted share succeeds?
Show answer
The server owns the files and enforces permissions. The client only forwards the request. The server checks the requesting identity against the file's owner and mode and returns success or an error.