Mounting SMB drives under linux the old school way

January 14, 2023 - Reading time: 4 minutes

Setting up /etc/fstab entry for SMB (Windows) file share can be tricky. Here I document options that I use. Today many file managers will offer built-in support for mounting SMB shares using FUSE userland file system support instead.

Setup

We will put /etc/fstab entry like below:

//<IP or host>/<share> /mnt/<host>/<share> cifs user=<login>,seal,vers=3.0,soft,user,noauto,nosuid,nodev,noexec 0 0

Where:

  • <IP or host> is your SMB server (Linux running samba or Windows share)
  • <share> is the name of the share
  • user=<login> sets the user name that owns the share
  • seal enables encryption
  • vers=3.0 uses recent version of SMB protocol
  • soft make programs fail on I/O instead of hang if your network or server goes away
  • noauto tells mount to not automatically mount this share at boot (when mount -a is used)
  • nosuid,nodev,noexec tell OS to not respect suid flag, do not provide device nodes and not to execute anything from mounted volume - I use this trio on external storage by default as a security precaution

You will need to create /mnt/<host>/<share> directory with mkdir as root.

Extra options:

  • nostrictsync,cache=loose will improve write performance at a higher risk of data corruption

Mounting

Now you can run mount /mnt/<host>/<share> as a regular user. You will be prompted for the share user password.

Note on un-mounting

If your system uses symlink approach for mtab (/etc/mtab -> /proc/self/mounts) then you won't be able to umount your share without using sudo. See Option "user" work for mount, not for umount.

If your system hangs on shut down or reboot after you have mounted the share it may be because your wi-fi connection goes down before the file systems are un-mounted.

Extra: Automating password entry with pass

If you use pass password manager you can use this script to automate the mounting process:

#!/bin/sh
expect \
-c "spawn mount /mnt/<host>/<share>" \
-c 'set spawn_id_mount $spawn_id' \
-c 'expect "Password for" {log_user 0; spawn -noecho pass <password path>; expect -re "(.*?)\n"; set pass "$expect_out(1,string)"; wait; set spawn_id $spawn_id_mount; send -- "$pass"; log_user 1; send_user -- "*****"} eof {exit}' \
-c 'interact'

Remember to replace <host>, <share> and <password path> with path to the share password in you manager. You may need to tweak the script for different system language locales.

Mastodon