Network filesystems: NFS and SMB

Intermediate · 32 min

Share directories with NFS exports, mount NFS and SMB shares with safe options, automount on demand, and recover from stale file handles.

Before thisMounting and fstabUsers, groups and the account files

Akamai Developers · 32 min

Network filesystems: NFS and SMB

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Akamai DevelopersReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A network filesystem makes a directory on one machine appear in the tree of others. NFS is the Unix-native protocol, SMB the Windows one, both well supported on Linux.
  • An NFS server publishes directories in /etc/exports: a path, the clients allowed, and options like rw, sync and root_squash. `exportfs -ra` applies changes.
  • Classic NFS trusts the client's UIDs. User 1000 on the client is user 1000 on the server, so IDs must match, and root_squash stops client root acting as root on the share.
  • NFS mounts are hard by default: if the server vanishes, processes wait instead of getting errors. soft risks silent data corruption.
  • SMB shares mount with mount.cifs. Put the password in a credentials file readable only by root, not in fstab.
  • autofs or systemd's x-systemd.automount mounts a share on first access, so a dead server does not hang the boot.
  • 'Stale file handle' (ESTALE) means the client holds a reference the server no longer recognises. Remount to clear it, and find what changed on the server.

Files that live somewhere else

Three web servers need the same uploaded images. A team wants one home directory that follows them to any machine. A backup job needs to write to a NAS. Copying files around does not scale, and object storage needs application changes. A network filesystem does neither: a directory on one machine is mounted into the tree of others, and programs use it with ordinary file calls.

Linux speaks two: NFS, the Unix-native protocol, and SMB (also called CIFS), the Windows file-sharing protocol, which Linux mounts as a client and serves with Samba. The rest of this lesson is how to set them up and, more importantly, how they fail.

Serving with NFS

The server side is the kernel NFS server plus a list of exports. I downloaded Debian 13’s nfs-kernel-server (2.8.3) and unpacked it on this Mac. The /etc/exports it installs is only comments, with these examples:

# Example for NFSv2 and NFSv3:
# /srv/homes       hostname1(rw,sync,no_subtree_check) hostname2(ro,sync,no_subtree_check)
#
# Example for NFSv4:
# /srv/nfs4        gss/krb5i(rw,sync,fsid=0,crossmnt,no_subtree_check)
# /srv/nfs4/homes  gss/krb5i(rw,sync,no_subtree_check)

Each line is a path and a list of clients, each followed by options in brackets. exports(5) says no whitespace is permitted between a client and its option list. A stray space there is a well-known way to export a share to every host by accident. A typical internal share:

/srv/shared   10.0.0.0/24(rw,sync,no_subtree_check)

The options worth knowing, from exports(5):

  • rw / ro: read-write or read-only.
  • sync, the default: reply only after data is on stable storage. async replies early and is faster, but the man page warns that a server crash can lose or corrupt data.
  • root_squash, the default: requests from UID 0 are mapped to the anonymous user. no_root_squash gives client root full root on the export, and the man page says it is mainly for diskless clients.
  • no_subtree_check: the default since nfs-utils 1.1.0. Subtree checking causes more problems than it is worth.

After editing, exportfs -ra re-exports everything, and exportfs -v shows what is live. Debian’s nfs-server.service runs exportfs -r before starting and on reload.

The identity problem

Classic NFS uses what nfs(5) calls AUTH_SYS: the client sends the numeric UID and GID of the user, and the server believes it. The man page is blunt: this is easy to spoof, but adequate “on a trusted physical network between trusted hosts”. Two consequences:

  1. IDs must match. If ana is UID 1001 on the client and 1004 on the server, her files appear to belong to someone else. Central accounts, via LDAP or a directory service, or careful provisioning, fix this. The accounts lesson covers UIDs.
  2. Anyone root on a client can be any user on the share. root_squash only stops them being root itself. For untrusted networks, NFSv4 with Kerberos (sec=krb5p) authenticates users and encrypts traffic.

So keep NFS on a trusted network segment, restrict exports to specific addresses, and firewall the ports.

Mounting on a client

From nfs(5) and mount(8), not run here:

mount -t nfs 10.0.0.5:/srv/shared /mnt/shared
showmount -e 10.0.0.5                      # what the server exports (NFSv3)

In /etc/fstab:

10.0.0.5:/srv/shared  /mnt/shared  nfs  defaults,_netdev,nofail,x-systemd.automount  0 0

_netdev waits for the network, nofail keeps the boot going if the server is down, and x-systemd.automount mounts on first access, all from the fstab lesson.

hard versus soft. nfs(5) says that by default, a hard mount, requests that time out are retried indefinitely. If the server goes away, every process touching the mount waits, in the uninterruptible D state from the /proc lesson, until it comes back, then continues as if nothing happened. soft returns an error instead, and the man page warns that it “can cause silent data corruption in certain cases”. Keep hard for anything you write to.

Automounting

A share mounted at boot is a share whose server must be up at boot. Automounting mounts it when someone first opens a path under it, and can unmount it after idle time. Two ways:

  • systemd: x-systemd.automount in fstab, optionally with x-systemd.idle-timeout=10min. Nothing else to install.
  • autofs: a daemon with its own maps. A master map line such as /mnt/nfs /etc/auto.nfs hands a directory to a map, and per autofs(5) each map line is key [-options] location, such as shared -rw 10.0.0.5:/srv/shared. Accessing /mnt/nfs/shared mounts it. autofs suits large or centrally managed maps, such as home directories for many users.

Mounting SMB shares

mount.cifs, from the cifs-utils package:

mount -t cifs //nas.example.net/backup /mnt/backup \
      -o credentials=/etc/samba/backup.cred,uid=backup,gid=backup

The credentials file has username= and password= lines, per mount.cifs(8). Make it mode 600 and owned by root, because /etc/fstab is readable by everyone. SMB has no Unix UIDs to send, so uid= and gid= set who owns the files locally. vers= picks the protocol. The default negotiates the highest SMB2 or later version both sides support, and 1.0, the original CIFS, should stay off. In fstab, the same _netdev,nofail,x-systemd.automount advice applies.

Stale file handles

errno(3) lists ESTALE, “Stale file handle”, noting it can occur for NFS. NFS clients refer to files by handles the server issued. If the server can no longer resolve one, because the file was deleted and replaced, the export was moved, or the exported filesystem was recreated so its identity changed, every access through the old handle fails. Recovery is to unmount and remount: umount /mnt/shared and mount again, or if the server is gone and umount hangs, umount -f, and as a last resort umount -l, which umount(8) says detaches the mount now and cleans up later, adding that a reboot should be expected soon after using it for a network filesystem. exports(5) describes fsid=, which pins an export’s identity so it survives changes to the underlying device.

About the video

The embed is all of Akamai Developers’ “How to Install and Configure an NFS Linux Server and Client”, March 2023, 32 minutes. Its chapters run from creating the shares, installing and configuring the server and examining and rewriting /etc/exports, through installing the client, testing connectivity and mounting and unmounting, to auto-mounting with autofs and verifying idle unmounts. That is the NFS half of this lesson end to end. It does not cover SMB, hard versus soft, identity mapping or stale handles, which the lesson adds. English was judged from the channel and English auto-captions, the content from the chapters.

What was run: downloading and unpacking Debian 13’s nfs-kernel-server package and reading its exports template and service unit on macOS. Behaviour is from exports(5), nfs(5), mount.cifs(8), autofs(5), umount(8) and errno(3) on man7.org, checked 2026-11-28.

Where this fits

That ends the storage group. Next, the network these filesystems travel over: interfaces, addresses and the ip command.

Check yourself

  1. 01Files written by root on an NFS client show up on the server owned by nobody. Why?

    root_squash, the default. Requests from UID 0 are mapped to the anonymous user so that root on a client is not root on the server.

  2. 02Why is `async` in an export faster, and why is it risky?

    The server replies before data reaches stable storage. If the server crashes, writes the client thinks are done can be lost or corrupted.

  3. 03An NFS server reboots and every process on the clients that touches the mount freezes. Is that a bug?

    No, it is the hard mount default. Requests retry until the server returns, then continue without errors. That protects data, which soft would not.

  4. 04How do you keep a backup server's SMB password out of /etc/fstab?

    Use the credentials=/etc/samba/backup.cred option and put username= and password= lines in that file with mode 600, owned by root.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark