Skip to content

Instantly share code, notes, and snippets.

@rbb
Created October 18, 2018 23:07
Show Gist options
  • Save rbb/59c57d82c3a7d8c63a673afc86c84c33 to your computer and use it in GitHub Desktop.
Save rbb/59c57d82c3a7d8c63a673afc86c84c33 to your computer and use it in GitHub Desktop.
Setup autofs on Ubuntu 18.04

Mounting CIFS with automount on Ubuntu 18:

  1. Install the necessary applications/servers:

    sudo apt install autofs smbclient cifs-utils

  2. Configure autofs

create /etc/auto.cifs, with this variant from auto.smb, courtesy howtoforge

#!/bin/bash
# $Id$
# This file must be executable to work! chmod 755!
key="$1"
# Note: create a cred file for each windows/Samba-Server in your network
#       which requires password authentification.  The file should contain
#       exactly two lines:
#          username=user
#          password=*****
#       Please don't use blank spaces to separate the equal sign from the
#       user account name or password.
credfile="/etc/auto.smb.$key"
# Note: Use cifs instead of smbfs:
mountopts="-fstype=cifs,file_mode=0644,dir_mode=0755,uid=user,gid=users"
smbclientopts=""
for P in /bin /sbin /usr/bin /usr/sbin
do
        if [ -x $P/smbclient ]
        then
                SMBCLIENT=$P/smbclient
                break
        fi
done
[ -x $SMBCLIENT ] || exit 1
if [ -e "$credfile" ]
then
        mountopts=$mountopts",credentials=$credfile"
        smbclientopts="-A "$credfile
else
        smbclientopts="-N"
fi
$SMBCLIENT $smbclientopts -gL $key 2>/dev/null \
   | awk -v key="$key" -v opts="$mountopts" -F'|' -- '
        BEGIN   { ORS=""; first=1 }
   /Disk/  { if (first) { print opts; first=0 };
        gsub(/ /, "\\ ", $2);
        sub(/\$/, "\\$", $2);
        print " \\\n\t /" $2, "://" key "/" $2 }
        END     { if (!first) print "\n"; else exit 1 }
        '

Note: you may need to modify the mountopts, for an older ReadyNAS, I used:

mountopts="-fstype=cifs,file_mode=0644,dir_mode=0755,uid=my_user_name,gid=my_user_name,vers=1.0"

Set permissions: chmod 755 /etc/auto.cifs

Create /etc/auto.smb.FILESERVERNAME with something like:

username=user
password=secret

Set permissions: chmod 600 /etc/auto.smb.*

edit /etc/auto.master to add:

/cifs /etc/auto.cifs --timeout=60
  1. Fix for No DNS entries

My work network does not have a DNS entry for the file server I want. But I do have an entry in my local /etc/hosts file. I tried adding a name resolve order line to /etc/samba/smb.conf as recommended here, but it didn't work. Looking at the docs, I ended up using dns proxy.

[global]
dns proxy = yes
  1. Reload, Restart and enable autofs
sudo systemctl reload autofs.service
sudo systemctl restart autofs.service
sudo systemctl enable autofs.service
  1. Verify operation

list the directory with something like: ls /mnt/cifs/server/share

If things don't work, start digging into /var/log/syslog and /var/log/samba/log* (use ls -lrt to find the most recently changed logs).

running automount -f -v instead of with systemctl can be helpful

Running smbclient -L server -d 256 can be helpful. Using smbclient -L server -m SMBx was key for me solving the Mount CIFS Host is down problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment