Sub topic 12
Mounting at boot with /etc/fstab
This chapter assumes you can already mount an NFS export and a Samba share by hand, as covered in the earlier chapters. It also assumes you can read the six fields of an /etc/fstab line. Here you will make those mounts persistent, so they come back after a reboot, and learn which options keep a boot from hanging when the file server is down.
Why a manual mount is not enough
A mount command changes the running kernel's mount table. The change is lost at reboot. The file /etc/fstab lists the filesystems that the system mounts at boot. The init system (systemd) reads it and generates a mount unit for each line.
Network filesystems add a problem that local disks do not have. They need the network to be up, and they need the remote server to answer. A wrong fstab line for a local disk is a small mistake. A wrong line for a network share can stall boot for minutes, or drop the machine into emergency mode.
The six fields
Each line of /etc/fstab has six whitespace-separated fields.
| Field | Meaning | NFS example | CIFS example |
|---|---|---|---|
| 1. Device | What to mount | `fileserver:/srv/nfs/projects` | `//fileserver/projects` |
| 2. Mount point | Local directory, which must exist | `/mnt/nfs-projects` | `/mnt/smb-projects` |
| 3. Type | Filesystem type | `nfs4` | `cifs` |
| 4. Options | Comma-separated mount options | `defaults,_netdev` | `credentials=...,vers=3.0,...` |
| 5. Dump | Used by the old `dump` backup tool | `0` | `0` |
| 6. Pass | `fsck` order at boot | `0` | `0` |
Always use 0 for the last two fields on network filesystems. The client cannot run fsck on a remote filesystem, and nobody uses dump for it.
A complete example
This is the client file from the earlier chapters, with one NFS line and one CIFS line.
# /etc/fstab on the client
fileserver:/srv/nfs/projects /mnt/nfs-projects nfs4 defaults,_netdev 0 0
//fileserver/projects /mnt/smb-projects cifs credentials=/root/.smbcreds,vers=3.0,uid=1000,gid=1000,_netdev 0 0Create the mount points before the first boot test:
sudo mkdir -p /mnt/nfs-projects /mnt/smb-projectsThe `_netdev` option
_netdev tells the system that the device needs the network. systemd then orders the mount after network-online.target instead of trying it early in boot. Without it, the mount may be attempted before the network interface has an address, and it fails.
Some modern systems detect network filesystems by their type and add this ordering for you. Keeping _netdev is harmless and makes your intent clear, so keep it.
The CIFS credentials file
The fstab file is readable by every user. Never put password= in it. Put the secrets in a separate file that only root can read.
sudo tee /root/.smbcreds >/dev/null <<'EOF'
username=alice
password=your-samba-password
domain=WORKGROUP
EOF
sudo chown root:root /root/.smbcreds
sudo chmod 600 /root/.smbcredsThe credentials= option points mount.cifs at this file. The uid= and gid= options set which local user and group appear to own the files, because CIFS does not carry Linux IDs by default.
Options that decide what happens when the server is gone
The default behaviour is to try the mount at boot and to treat failure as a problem. For a server that may be offline, this default is often wrong. These options change it.
| Option | Effect |
|---|---|
| `nofail` | A failed mount does not stop boot or send it to emergency mode. |
| `x-systemd.automount` | Mount on first access, not at boot. |
| `x-systemd.mount-timeout=30` | Give up on a mount attempt after 30 seconds. |
| `x-systemd.idle-timeout=600` | Unmount again after 600 seconds without use. Needs `x-systemd.automount`. |
| `soft` or `hard` (NFS) | Whether I/O errors are returned to programs (`soft`) or retried forever (`hard`). |
| `_netdev` | Wait for the network before mounting. |
A common, resilient line looks like this:
fileserver:/srv/nfs/projects /mnt/nfs-projects nfs4 defaults,_netdev,nofail,x-systemd.automount,x-systemd.mount-timeout=30 0 0With x-systemd.automount, systemd places an automount point at /mnt/nfs-projects. The first program that reads the directory triggers the real mount. If the server is down at boot, nothing waits.
soft versus hard on NFS
NFS defaults to hard. If the server stops answering, programs block in uninterruptible sleep until it returns. This protects data, because writes are not silently dropped. With soft, a request fails after the retry limit, and an application may then believe a write succeeded when it did not. Use hard for read-write data. Use soft only for read-only data where an error is acceptable.
How systemd handles fstab
systemd converts each fstab line into a unit when it reads the file. A mount at /mnt/nfs-projects becomes mnt-nfs\x2dprojects.mount, and an automount becomes the matching .automount unit.
placeholderThe sequence below shows what happens at boot for an automounted share.
To see the generated units, run:
systemctl list-units --type=mount,automount
systemctl status mnt-nfs\\x2dprojects.mountTesting without rebooting
Do not test an fstab change by rebooting. A typo can leave a remote machine unreachable. Test in this order.
- Unmount any manual mount of the same share:
sudo umount /mnt/nfs-projects. - Tell systemd to re-read fstab:
sudo systemctl daemon-reload. - Check syntax and options:
sudo findmnt --verify. - Mount everything listed in fstab:
sudo mount -a. - Confirm the result:
findmnt /mnt/nfs-projects.
If mount -a prints an error, fix the line before you reboot. Only then reboot and confirm the mounts return.
sudo umount /mnt/nfs-projects /mnt/smb-projects
sudo systemctl daemon-reload
sudo findmnt --verify
sudo mount -a
findmnt -t nfs4,cifsCommon errors
- Boot drops to emergency mode. A required mount failed. Add
nofail, or usex-systemd.automount. Fix the line from the emergency shell by editing/etc/fstab. - Mount works by hand but fails at boot. The network was not ready. Add
_netdev, and check that the network manager has a wait-online service enabled, for exampleNetworkManager-wait-online.service. mount error(13): Permission deniedon CIFS. The credentials file has the wrong password or the wrongdomain=. Test withsmbclientfirst.mount point does not exist. Create the directory in field 2 withmkdir -p.- Files show the wrong owner on CIFS. Set
uid=andgid=to the intended local IDs. Useid aliceto find them. - Spaces in a path. Write a space as
\040in fstab, for example/mnt/my\040share. - Changes to fstab seem ignored. Run
sudo systemctl daemon-reload, because systemd caches the generated units.
Exercise
A server mounts //fileserver/projects through fstab. After a power outage, the client boots slowly and then stops in emergency mode because the file server is still down. Name two options you would add to the fstab line, and say what each does.
Show answer
Add nofail so that a failed mount does not stop boot or trigger emergency mode. Add x-systemd.automount so the share is mounted on first access instead of at boot. Optionally add x-systemd.mount-timeout=30 to cap how long a mount attempt waits.
Exercise
You added a CIFS line with password=secret directly in fstab. What is wrong with it, and how do you fix it?
Show answer
/etc/fstab is readable by every user, so the password is exposed. Move username= and password= into a file such as /root/.smbcreds, set it to chown root:root and chmod 600, and replace the password option in fstab with credentials=/root/.smbcreds.