This will be a short one. I wanted a link to point to whenever someone needs help with mounting a Samba share. I have previously mentioned this how-to here:
If you just want a run once and basically forget script, go down to tl;dr.
Prerequisites:
- a Linux terminal and sudo / doas permissions
- a Samba file share and its credentials
- to have
autofs
,cifs-utils
andsmbclient
installed. They may come in different varieties / names depending on your distro, like smb-client or similar. I believe that’s how they are called on Ubuntu and Debian
What does the script do? Basically queries your samba server using your credentials, sees all the Samba shares that are available for your user and mounts all of them in /mnt/remote. Whenever you add a new samba share, you can just run ./mkautofssmbconf.sh <samba-server-or-ip>
and the script will refresh your autofs configuration for you.
Steps:
- add this line in /etc/auto.master (or it might be /etc/autofs/auto.master depending on your distro) right before the +auto.master line:
/- auto.smbmedia
- Create a file named
.smb.credentials
under your home folder (that would be something like /home/$USER/.smb.credentials), which should look like so:
username=your-samba-username
password=your-samba-password
- run
chmod 600 ~/.smb.credentials
to make the file only readable by your user (and however has root permissions, obviously) - create a file called
mkautofssmbconf.sh
and put it wherever you like, I prefer in a folder called scripts or utils in my home directory:
mkautofssmbconf.sh
#!/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=${USER}
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'
sudo mv ${VARTMPCFG} /etc/auto.smbmedia && sudo /bin/sh -c 'chown root:root /etc/auto.smbmedia && systemctl restart autofs'
- make the script executable by running
chmod u+x mkautofssmbconf.sh
Usage:
As mentioned in the script, all you have to do after you have your .smb.credentials and /etc/auto.master config modified, is to just run ./mkautofssmbconf.sh samba-server.lan.local
or if you don’t have a DNS, replace the samba-server.lan.local with the IP address of your SMB server.
tl;dr quick to run script for the lazy people out there. Run with sudo, like sudo ./deploy-autofs.sh
deploy-autofs.sh
#!/bin/sh
echo
echo "Insert Samba username: "
read VARSMBUSER
echo "Insert Samba password (text will appear blank): "
stty -echo
read VARSMBPASS
stty echo
echo
if [ "$(ls -1 /home | wc -l)" -gt "1" ]
then
echo "You have more than 1 users on this machine. Select which user is yours: "
VARLSHOMEUSRS=$(ls -1 /home)
VARNO=0
for lineuser in ${VARLSHOMEUSRS}
do
VARNO=$(($VARNO+1))
echo ${VARNO}. ${lineuser}
done
read ANSWER
[ ${ANSWER} -gt ${VARNO} ] || \
[ ${ANSWER} -lt 1 ] && \
echo "Error, you have inserted a value bigger than the number of users or lower than 1. Please insert your username from the current Linux machine manually: " && \
read VARUSER || \
VARUSER=$(printf ${VARLSHOMEUSRS} | sed -n ${ANSWER}p)
fi
VARAUTOPATH=$(find /etc | grep auto.master | head -n 1)
VARTMPAUTOMAST=$(mktemp /tmp/auto.master.XXXXX)
VARUSER=$(ls -1 /home/ | head -n 1)
if [ "$(grep -c -i fedora /etc/os-release)" -gt "0" ]
then
dnf check-update
dnf update -y
dnf install -y autofs cifs-utils samba-client
elif ([ "$(grep -c -i ubuntu /etc/os-release)" -gt "0" ] || [ "$(grep -c -i debian /etc/os-release)" -gt "0" ])
then
apt-get update
apt-get -y upgrade
apt-get -y install autofs cifs-utils smbclient
fi
sed '/^\+auto.master/i \/- auto.smbmedia' ${VARAUTOPATH} > ${VARTMPAUTOMAST}
mv ${VARTMPAUTOMAST} ${VARAUTOPATH}
cat << EOCRDF > /home/${VARUSER}/.smb.credentials
username=${VARSMBUSER}
password=${VARSMBPASS}
EOCRDF
chmod 600 /home/${VARUSER}/.smb.credentials
chown ${VARUSER}:${VARUSER} /home/${VARUSER}/.smb.credentials
cat << EOF > /home/${VARUSER}/mkautofssmbconf.sh
#!/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=\${USER}
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'
sudo mv \${VARTMPCFG} /etc/auto.smbmedia && sudo sh -c 'chown root:root /etc/auto.smbmedia && systemctl restart autofs'
EOF
chmod u+x /home/${VARUSER}/mkautofssmbconf.sh
chown ${VARUSER}:${VARUSER} /home/${VARUSER}/mkautofssmbconf.sh
Then lastly, go to your home directory and run (as your user)
./mkautofssmbconf.sh samba-server.dns.name-or-ip