Accessing Windows Or Samba Shares Using AutoFS

You already installed Linux on your networked desktop PC and now you want to work with files stored on some other PCs in your network. This is where autofs comes into play. This tutorial shows how to configure autofs to use CIFS to access Windows or Samba shares from Linux Desktop PCs. It also includes a tailored configuration file.

If autofs Version 4.0 or newer is already installed, you should find the files

/etc/auto.master

and

/etc/auto.smb

on your system. Otherwise start the package manager of your distribution (e.g. YaST on SuSE, synaptic on Debian or Ubuntu, ...) and install it. When you are at it, also install the Samba client package (look for smbclient), because we will also need this. On some distributions (Ubuntu) we also need the package smbfs which contains the utilities mount.cifs and umount.cifs.

Configuration

If autofs is already installed, it is probably still not configured and not working. Assuming your Linux Distribution contains a Linux 2.6.x kernel I recommend to use the common internet file system (cifs) module to access files on the network. Please store the following file as

/etc/auto.cifs

on your computer. You need root (or sudo) to have the permissions to do this:

#!/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 }
        '

Make this file executable using the command:

chmod 755 /etc/auto.cifs

This file is a slightly modified version of the file auto.smb which usually comes as part of the autofs package. You need to modify the line defining the mountopts above and change user to the name of your personal account name. Now you have to give autofs the credentials needed to access shares on your network. To do this create a file called

/etc/auto.smb.FILESERVERNAME

for each computer you want to access. Of course replace FILESERVERNAME with the name of the computer. Fill these files with user account name and password needed to access the shares on the computer. The content of these files should like the following example:

username=user
password=secret

Use the command

chmod 600 /etc/auto.smb.*

to protect the password information.

Now we have to tell autofs to make use of our new configuration file. Use the commands:

echo "/cifs /etc/auto.cifs --timeout=60" >>/etc/auto.master

and

/etc/init.d/autofs restart

That's all.

Testing

Use the command

ls -als /cifs/FILESERVERNAME/SHARENAME

to check, if it works. If not, consult the system logfiles (usually /var/log/messages or /var/log/syslog) for messages.

Credits: davek for providing a patch to better escape dollar signs and blanks in share names

Share this page:

24 Comment(s)