Sub topic 10
Mounting an NFS export
This chapter covers the client side of NFS. It assumes you can run commands as root, that a server already exports a directory (see the previous chapter on /etc/exports and exportfs), and that you know basic TCP/IP addressing and systemctl. Identity mapping and /etc/fstab are covered in later chapters, so they appear here only where they affect a manual mount.
What a client needs
A Linux client needs two things before it can mount an export: the NFS client utilities and network access to the server.
- On Debian and Ubuntu, install the
nfs-commonpackage. - On Red Hat-family systems, install
nfs-utils.
The mount command hands the work to a helper program, mount.nfs, which ships in these packages. Without the package you get an error such as bad option; for several filesystems (e.g. nfs, cifs) you might need a /sbin/mount.<type> helper program.
The kernel does the actual file traffic. The helper only parses options, contacts the server, and passes the result to the kernel NFS client.
Discovering what the server offers
The showmount command asks the server which directories it exports.
showmount -e fileserverTypical output looks like this:
Export list for fileserver:
/srv/nfs/projects 192.168.1.0/24The right-hand column shows who may mount the export. If your client's address is not covered, the mount fails with access denied by server.
showmount relies on the older mountd service, which is the RPC (remote procedure call) program that answers mount requests. A server that runs only NFSv4 and blocks the RPC ports may not answer, even though NFSv4 mounts work. A failed showmount therefore does not prove that mounting is impossible.
Mounting by hand
Create a mount point, which is an ordinary empty directory, and mount the export.
showmount -e fileserver
sudo mkdir -p /mnt/projects
sudo mount -t nfs4 fileserver:/srv/nfs/projects /mnt/projectsThe source has the form server:/path. The path is the one the server exports. Check the result:
findmnt /mnt/projects
df -h /mnt/projectsfindmnt shows the filesystem type and the options in effect. df shows the size of the remote filesystem, which confirms you are looking at the server's disk and not the empty local directory.
Unmount with umount:
sudo umount /mnt/projectsIf the command reports target is busy, a process still has a file open or uses the directory as its working directory. Find it with lsof /mnt/projects or fuser -vm /mnt/projects.
A mount hides whatever was in the local directory before. The files return after you unmount, so use an empty directory.
Choosing the version: `-t nfs` and `-t nfs4`
The NFS versions differ in the ports and services they need (see the architecture chapter).
| Command | Behavior |
|---|---|
| `mount -t nfs4 ...` | Uses NFSv4 only. Fails if the server lacks it. |
| `mount -t nfs ...` | Negotiates. Tries the highest version both sides support, usually 4.2 on current systems. |
| `mount -t nfs -o vers=3 ...` | Forces NFSv3. Needs `rpcbind` and `mountd` on the server. |
| `mount -t nfs -o vers=4.1 ...` | Forces minor version 4.1. |
When you ask for a specific version, the client does not fall back to another one. A failure then points to a real mismatch, which makes testing easier.
With NFSv4 the server presents exports as a single tree. Some servers define a pseudo-root with fsid=0, and then clients mount paths relative to that root, such as fileserver:/projects. In the setup used in this tutorial, the full path /srv/nfs/projects works. If your server uses a pseudo-root and the full path fails with No such file or directory, run showmount -e or check the server's exportfs -v output to see the real layout.
Mount options that matter
Pass options with -o, separated by commas.
sudo mount -t nfs4 -o rw,hard,timeo=600,retrans=2,noatime fileserver:/srv/nfs/projects /mnt/projects| Option | Meaning |
|---|---|
| `ro` / `rw` | Mount read-only or read-write. The server's export must also allow `rw`. |
| `hard` | Retry forever if the server stops answering. This is the default. |
| `soft` | Give up after the retries and return an I/O error to the application. |
| `timeo=N` | Wait N tenths of a second before the first retry. |
| `retrans=N` | Number of retries before a further recovery action. |
| `noatime` | Do not update access times, which saves write traffic. |
| `nosuid`, `noexec`, `nodev` | Block set-user-ID files, program execution, and device nodes on the mount. |
| `vers=N` | Choose the protocol version. |
Hard versus soft
The choice between hard and soft is the most important decision on this list.
With hard, an application that touches a file on an unreachable server waits, and the process may sit in uninterruptible sleep until the server comes back. Nothing is lost or corrupted. With soft, the application receives an error after the timeout. A write that fails midway can leave a truncated file, and the application may not notice.
Use hard for anything that writes data. Consider soft only for read-only data where an error is better than a hang, and accept the risk of silent short reads.
Ownership on the client
After mounting, run ls -ln /mnt/projects. The -n flag shows numeric user and group IDs. NFS transfers numbers, not names, in the common sec=sys setup. If your user is ID 1000 on the client but the files belong to ID 1001 on the server, you will see permission errors even though both machines have a user called alice. The identity mapping chapter explains this in detail. For now, remember to compare IDs with ls -ln on both machines when access is unexpectedly denied.
Checking and diagnosing a mount
Several commands show what the client negotiated.
nfsstat -m
mount | grep nfs
cat /proc/self/mountinfo | grep nfsnfsstat -m lists each NFS mount with its full option set, including the version actually negotiated (vers=4.2), the protocol (proto=tcp), and the sizes of read and write requests (rsize, wsize). Look there first when performance or behavior differs from what you expected.
Common errors and their usual causes:
| Message | Likely cause |
|---|---|
| `mount.nfs: access denied by server while mounting` | Client address not in the export's client list, or the export was not applied with `exportfs -ra`. |
| `mount.nfs: mount point does not exist` | You did not create the local directory. |
| `mount.nfs: Connection timed out` | Firewall blocks TCP 2049, or the server is down. |
| `mount.nfs: No route to host` | Network path or firewall rejects the connection. |
| `mount.nfs: requested NFS version or transport protocol is not supported` | Server lacks the version you forced with `vers=`. |
| `mount.nfs: mounting ... failed, reason given by server: No such file or directory` | The path is wrong, or the server uses an NFSv4 pseudo-root. |
| `Permission denied` on files after a successful mount | UID/GID mismatch or `root_squash` (see the next chapter). |
You can test reachability of the NFSv4 port before mounting:
nc -zv fileserver 2049Mounting on demand with systemd
Instead of a permanent mount, you can let systemd mount the export on first access. The x-systemd.automount option creates an automount unit, and x-systemd.idle-timeout unmounts it after a period of inactivity.
sudo mount -t nfs4 -o x-systemd.automount,x-systemd.idle-timeout=300 fileserver:/srv/nfs/projects /mnt/projectsThe directory is available at once, but the real mount happens when a process first reads it. This keeps a boot from stalling when the server is down. The next chapter shows how to make this setting persistent.
Summary of the working model
- The client installs the NFS utilities and finds an export with
showmount -e. mount.nfscontacts the server, which accepts or refuses the client based on/etc/exports.- The kernel then carries all file operations, using the options you chose.
- Behavior on failure depends mostly on
hardorsoft. Access depends on numeric IDs.
Exercise
A client runs mount -t nfs4 fileserver:/srv/nfs/projects /mnt/projects and gets Connection timed out. ping fileserver works. Name two things to check and one command for each.
Show answer
First, check whether TCP port 2049 is reachable with nc -zv fileserver 2049. If it is blocked, open the NFS service in the server's firewall (firewall-cmd --list-services on the server shows what is allowed). Second, check that the NFS server is running, for example with systemctl status nfs-server on the server. A successful ping only proves ICMP works, not that the NFS port is open.
Exercise
You mount a writable export with -o soft,timeo=50,retrans=2. During a network outage an application writes a large file. What can happen, and which option would you choose instead?
Show answer
After the retries run out, the write returns an I/O error. If the application ignores it, the file is left truncated or incomplete with no clear warning. Use hard (the default) for writable data, so the application waits until the server returns.