Having issues automounting samba share

I just remembered this post. Here’s the script if anyone wants it. Requires autofs, cifs-utils and smbclient installed. Works on Ubuntu 16.04+, Fedora 31+, CentOS 7+, Debian 9+. It also worked on Void Linux when I tested it long ago, just modify “systemctl” with “sv” are it’s good to go. It should also work on other distros and it’s simple enough that you can hack the heck out of it. Tested with bash, dash and oksh. Should work with other shells.

Originally this was used in a business environment, whenever I gave Linux users access to a new samba share, instead of me manually remoting and editing /etc/auto.customsmb file to add the new mounts, or worse, let them edit and break it, I would make them run this script whenever they got rights on a new share, or for the most Linux newbies, I’d ssh and run it myself. The script was slightly edited to allow user input of the domain name or IP of the samba server, it had it hard-coded (since we only had 1 server and I wanted to make it simple to run for most people). Remember to make the file executable by runing chmod u+x filename.sh on it.

#!/bin/sh
[ -z $1 ] && echo "please add samba's domain name or IP like so: \"./mkautofssmbconf.sh nas-name.local\"" && exit 1
VARREMOTESMB=$1
VARUSER=$(whoami)
VARUID=$(grep $VARUSER /etc/passwd | cut -d ':' -f3)
VARGID=$(grep $VARUSER /etc/passwd | cut -d ':' -f4)
VARTMPCFG=$(mktemp /tmp/autofstmpconf.XXXXX)

chmod 644 ${VARTMPCFG}

for VARFLD in $(smbclient -A /home/${VARUSER}/.smb.credentials -gL ${VARREMOTESMB} | grep Disk | cut -d '|' -f2) ; do
    echo /mnt/remote/${VARFLD} -fstype=cifs,uid=${VARUID},gid=${VARGID},credentials=/home/${VARUSER}/.smb.credentials,rw ://${VARREMOTESMB}/${VARFLD} >> ${VARTMPCFG}
    [ ! -d /mnt/remote/${VARFLD} ] && sudo mkdir -p /mnt/remote/${VARFLD}
done;
sudo /bin/sh -c '[ -f /etc/auto.smbmedia ] && rm -f /etc/auto.smbmedia & mv ${VARTMPCFG} /etc/auto.smbmedia && chown root:root /etc/auto.smbmedia && systemctl restart autofs'

As mentioned above, add this line in /etc/auto.master before the +auto.master line:
/- auto.smbmedia

And the /home/$(whoami)/.smb.credentials file should look like so:
username=ursmbuser
password=ursmbpasswd

Run the script and it should auto configure autofs for your samba share. I strongly believe it should go without saying, but just to avoid any copyright questions (I don’t believe in that nonsense), the above code is licensed under

The MIT License

Copyright © 2021 Biky

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

I use Samba only on Windows nowadays. I prefer sshfs for Linux, since I can just use my ssh key to authenticate and I don’t percieve any noticable speed difference. And I don’t like auto-mountings shared folders, I just keep a few scripts that I use to mount my samba location and my phone running PrimitiveFTPd and otherwise manually mount other locations as needed. But autofs comes in really handy when you don’t want 24/7 mounted remote folders, but want the ease of use of auto-mounting locations on-demand.

1 Like