fstab generator for NAS shares

Enter the NAS, the share and a folder; get the fstab line, the credentials file and the commands to test it safely.

Last updated

In short: A NAS share in /etc/fstab needs nofail and _netdev so boot never stops when the NAS is off, and x-systemd.automount so it mounts on first access instead of during boot. For SMB, keep the password in a root-only credentials file (mode 600), never in fstab. After editing, run sudo findmnt --verify, sudo systemctl daemon-reload and sudo mount -a before you reboot.

Your input never leaves your browser. Everything is calculated on your device; nothing you type is sent to a server.

How does the NAS share files?
Login and file ownership (SMB)

The password is never asked for here: you type it into the credentials file on your own machine.

Safety and boot
This machine runs

Step 1: install the SMB client and create the mount point

sudo zypper install cifs-utils
sudo mkdir -p /mnt/nas/media
cifs-utils
Provides mount.cifs, the helper needed to mount SMB shares.
mkdir -p /mnt/nas/media
Creates the empty folder the share will appear in.

Step 2: store the NAS login in a root-only file

The first command creates the file empty and readable only by root (mode 600), before any password goes in.

sudo install -m 600 /dev/null /root/.nas-credentials
sudo nano /root/.nas-credentials
sudo ls -l /root/.nas-credentials
install -m 600 /dev/null /root/.nas-credentials
Creates an empty file with permissions 600: only root can read or write it.
nano /root/.nas-credentials
Paste the two lines below and fill in your real password. Save with Ctrl+O and Enter, exit with Ctrl+X.
ls -l /root/.nas-credentials
Must start with -rw------- and show root root.

Contents of the credentials file

Save as /root/.nas-credentials

username=nasuser
password=your-NAS-password
username=nasuser
The account on the NAS, not your Linux user.
password=
The NAS password as it is, without quotes. This tool never asks for it.

Step 3: add this line to /etc/fstab

Add it as a new line at the end of the file. The six fields are separated by spaces: what to mount, where, the filesystem type, the options, and two zeros.

Save as /etc/fstab

//192.168.1.20/media /mnt/nas/media cifs rw,vers=3.0,credentials=/root/.nas-credentials,uid=1000,gid=1000,file_mode=0644,dir_mode=0755,_netdev,nofail,x-systemd.automount,x-systemd.mount-timeout=30 0 0
//192.168.1.20/media
The NAS and the share, written as //server/share.
/mnt/nas/media
The folder where the share appears on this machine.
cifs
The filesystem type: cifs, the Linux client for SMB, the protocol behind Windows file sharing.
rw
Read-write: this machine can create, change and delete files, as far as the NAS allows it.
vers=3.0
Uses SMB 3.0. Version 3.0 and newer can encrypt traffic and work with every current NAS and Windows.
credentials=/root/.nas-credentials
Reads the NAS user name and password from this root-only file. Never put password= in /etc/fstab: every user on the machine can read that file.
uid=1000,gid=1000
Makes every file look owned by user 1000 and group 1000 on this machine (check yours with id). SMB cannot show Linux owners, so this decides who may write.
file_mode=0644,dir_mode=0755
Files 644 and folders 755: the owner may write, everyone else may only read.
_netdev
Marks it as a network filesystem, so it is mounted only after the network is up.
nofail
If the NAS is off or unreachable, boot carries on instead of stopping in emergency mode.
x-systemd.automount
Mounts the share on first access instead of during boot, so boot never waits for the NAS. A program that opens the folder waits a moment while it mounts.
x-systemd.mount-timeout=30
Gives up after 30 seconds when the NAS does not answer, instead of the default 90 seconds.
0 0
No dump backups and no filesystem check at boot: a network share must never be checked by fsck.

Step 4: edit, check and mount

Keep a copy of the working fstab first. In nano, add the line at the end, save with Ctrl+O and Enter, and exit with Ctrl+X.

sudo cp /etc/fstab /etc/fstab.bak
sudo nano /etc/fstab
sudo findmnt --verify
sudo systemctl daemon-reload
sudo mount -a
findmnt /mnt/nas/media
ls /mnt/nas/media
cp /etc/fstab /etc/fstab.bak
A backup to go back to with sudo cp /etc/fstab.bak /etc/fstab.
findmnt --verify
Checks every fstab line for mistakes, such as a missing mount point or an unknown filesystem type, without mounting anything. Fix every error before you reboot.
systemctl daemon-reload
Makes systemd read the changed fstab; it turns every line into a mount unit.
mount -a
Mounts everything in fstab that is not mounted yet, right now, so a wrong password or share name shows up immediately instead of at the next boot.
findmnt /mnt/nas/media
Shows the mounted share with its real options. No output means it is not mounted.
ls /mnt/nas/media
Lists the files on the NAS.

If it does not mount

sudo journalctl -b -u mnt-nas-media.mount
sudo dmesg | tail -n 20
-u mnt-nas-media.mount
The log of this mount since boot. systemd names the unit after the mount point path.
dmesg
Kernel messages. Error -13 means a wrong user name or password, -2 a wrong share name, and -112 or -115 that the NAS cannot be reached.

Review generated files before you use them. Provided as-is — see Terms.

How does the fstab generator for NAS shares work?

/etc/fstab lists the filesystems Linux mounts at boot, one per line, with six fields separated by spaces: what to mount, where, the filesystem type, the options, and two numbers for dump and fsck (fstab(5)). systemd reads the file and turns each line into a mount unit. A NAS share is mounted over the network with NFS, the native Unix protocol, or SMB, the Windows file sharing protocol that Linux mounts with the cifs type.

The classic mistake is a plain NAS line without safety options: when the NAS is off at boot, the mount fails, systemd treats it as essential and stops in emergency mode, so a headless server never comes up. nofail lets boot continue, _netdev waits for the network, x-systemd.automount postpones the mount until something opens the folder, and x-systemd.mount-timeout limits how long an attempt may hang (systemd.mount(5)).

SMB needs a login. The password belongs in a credentials file that only root can read (mode 600), referenced with credentials=, because /etc/fstab is readable by every user. SMB cannot show Linux owners, so uid=, gid=, file_mode= and dir_mode= decide who owns the files and who may write (mount.cifs(8)). NFS keeps the real owner numbers from the NAS instead and controls access by IP address (nfs(5)).

Test before you reboot: findmnt --verify checks the file, systemctl daemon-reload makes systemd read it, and mount -a mounts it at once, so a typo shows up while you can still fix it. The line is built in your browser; nothing you type, and never your password, is sent anywhere.

Worked examples

An SMB share from a Synology NAS

Input SMB, 192.168.1.20, share media, /mnt/nas/media, SMB 3.0, user 1000, automount gives //192.168.1.20/media /mnt/nas/media cifs rw,vers=3.0,credentials=/root/.nas-credentials,uid=1000,gid=1000,file_mode=0644,dir_mode=0755,_netdev,nofail,x-systemd.automount,x-systemd.mount-timeout=30 0 0

The password lives in /root/.nas-credentials, created with sudo install -m 600 /dev/null so only root can read it. uid=1000 makes every file belong to your user, because SMB cannot show Linux owners. nofail and x-systemd.automount keep the machine booting normally when the NAS is off.

A read-only NFS export for a media server

Input NFS 4.2, truenas.local, export /mnt/tank/media, mount on /srv/media, read-only gives truenas.local:/mnt/tank/media /srv/media nfs ro,nfsvers=4.2,_netdev,nofail,x-systemd.automount,x-systemd.mount-timeout=30 0 0

ro means the media server can play files but never delete them, a cheap safety net. NFS has no uid= option: it shows the real owner numbers from the NAS, so the export must allow this machine's IP address and the files must be readable by the media server's user.

A Windows share with spaces in its name

Input SMB 3.1.1, desktop.local, share Shared Photos, /mnt/desktop/Shared Photos, group writes, no automount gives //desktop.local/Shared\040Photos /mnt/desktop/Shared\040Photos cifs

fstab separates its fields with spaces, so a space inside a name is written as \040. Without automount the share is mounted at boot; x-systemd.mount-timeout=10 limits how long boot waits when the PC is off, and nofail lets boot continue after that.

Frequently asked questions

Why did my server not boot after adding a NAS share to fstab?

Without nofail, a mount that fails at boot is treated as essential, so systemd stops in emergency mode when the NAS is off, slow or the password is wrong. Log in at the emergency prompt, run nano /etc/fstab, add nofail to the options of the NAS line (or put a # in front of it), and reboot. With nofail, x-systemd.automount and x-systemd.mount-timeout, a missing NAS only makes that one folder unavailable.

What do nofail, _netdev and x-systemd.automount do in fstab?

nofail tells systemd that boot may continue if the mount fails. _netdev marks it as a network filesystem, so it waits for the network. x-systemd.automount does not mount at boot at all: systemd watches the folder and mounts the share the first time something opens it, so boot never waits for the NAS. x-systemd.mount-timeout=30 stops a mount attempt after 30 seconds instead of the default 90 (systemd.mount(5)).

Where do I put the SMB password for a CIFS mount?

In a separate credentials file with the lines username=... and password=..., owned by root with permissions 600, and point to it with credentials=/root/.nas-credentials in the options. /etc/fstab itself must stay readable for every user, so a password=... option there is visible to anyone on the machine. Create the file with sudo install -m 600 /dev/null /root/.nas-credentials before you type the password into it.

Why can I read but not write files on my CIFS mount?

The kernel mounts the share as root, so without options every file looks owned by root. Add uid= and gid= with your own numbers (run id to see them, often 1000) and file_mode and dir_mode that allow writing, then unmount and mount again. The NAS account in the credentials file must also have write permission on the share. For NFS the owner numbers come from the NAS, so the files must be owned there by the same UID you have here.

How do I test an fstab change without rebooting?

Run sudo findmnt --verify to check the file for mistakes, sudo systemctl daemon-reload so systemd reads the new fstab, then sudo mount -a to mount everything that is not mounted yet; errors show up straight away. findmnt /mnt/nas/media then shows the mounted share. Keep a copy of the old file (sudo cp /etc/fstab /etc/fstab.bak) so you can go back.

Should I use NFS or SMB for a Linux NAS mount?

NFS is the native Linux choice: simple, fast, and it keeps Linux owners and permissions, but access is controlled by IP address and matching user numbers, not by a password. SMB (CIFS) works with every NAS and with Windows, logs in with a user name and password, but shows all files with one owner that you choose with uid=. For a Linux server with Linux clients on a trusted LAN, NFS 4.1 or newer; for mixed machines or when you want a login, SMB 3.