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




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.


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


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

# $Id$
# This file must be executable to work! chmod 755!
# 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.
# Note: Use cifs instead of smbfs:
for P in /bin /sbin /usr/bin /usr/sbin
        if [ -x $P/smbclient ]
[ -x $SMBCLIENT ] || exit 1
if [ -e "$credfile" ]
        smbclientopts="-A "$credfile
$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


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:


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


/etc/init.d/autofs restart

That's all.


Use the command


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:

20 Comment(s)

Add comment



I found that I had to make two changes to the script.  First, silly as it sounds, I have some shares with more than two spaces.  So I had to change the awk command "sub" to gsub.  Also, NT/XP insists on sharing some default shares with a "$" in them.  Unless the $ is escaped, autofs doesn't like the output of the script.  Here are my changes:

$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 }

Thank you so much for the script!  I wouldn't have figured this out without the example


From: Peter Funk

Hello Davek,
you were right. I've updated and fixed the script using your snippet. Thanks!


I think this would be a great tutorial if you could explain what you mean by "You need to modify the line defining the mountopts above".  Is this where I put the path to my share?  I am a bit mystified by this.

From: Anonymous

If you know how function Autofs, you should know the PATH. Let me tell you, in the auto.master you specify the "root" mount point in /cifs and if you winsvr is(for example) windata and you winfolder shared is SHARE the mountpoint would be /cifs/windata/SHARE = /cifs/FILESERVERNAME/FOLDERSHARED

From: Jason

I am using this updated version of the script but still do not seem to have access to any admin $ shares.  My messages output:


Aug 26 12:45:58 FMI2081 kernel: CIFS VFS: cifs_mount failed w/return code = -6


After looking that up it seems to think the share name is invalid...


From: Anonymous

Use this:

$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
dir = $2
loc = $2
# Enclose mount dir and location in quotes
# Double quote "$" in location as it is special
gsub(/\$$/, "\\$", loc);
print " \\\n\t \"/" dir "\"", "\"://" key "/" loc "\""
END { if (!first) print "\n"; else exit 2 }'

From: Anonymous

Thanks Anon!

This solved my problem with trying to access folders with the $ sign in the share or folder name.

From: Anonymous

I'm still not able to get this working with admin shares (shares that end in a $).

 When I try to create the mount point manually, I find that autofs doesn't seem to like a $ in the mount point.  So I can mount the target with the escaped $, but I can't mount it to a folder with a $ in it.  I am trying to rewrite the script to filter the $ out of the share name, listing as //SERVER/SHARE$ as /cifs/SERVER/SHARE and then to escape the $ on the actual target, unfortuneately I'm not the best at awk.  I might try to rewrite with sed...



From: Wim De Rammelaere

Another bug is still present.

Sometimes you don't have access to all shares.

You should change

$SMBCLIENT $smbclientopts -gL $key


$SMBCLIENT $smbclientopts -gNL $key



From: Anonymous

Thanks for the tip, but on my setup it didn't work.

Browsing the shares didn't work - the server root appeared in each share root.

This implies that browsing the server root worked well - all shares were shown correctly there.

Instead of trying to solve the problem in the script, I just added one line for each share in the auto.SERVER like so:

SHARE1 -fstype=smbfs,username=USERNAME,password=PASSWORD,uid=1000,gid=1000 ://SERVER/SHARE1

SHARE2 -fstype=smbfs,username=USERNAME,password=PASSWORD,uid=1000,gid=1000 ://SERVER/SHARE2



And so on.

That worked!

From: Anonymous

Thank you for your great howto!!

Could you please add --ghost to the line going into auto.master, as it will create "stubs" for server and shares eventhough they are not (yet) mounted. In this way the user will see what is out there, instead of having to know or having to search for it.

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

From: Anonymous

I have followed the instructions exactly on a RHEL 5 server and it does not work for me.  No messages in the logs.  Any help?

From: Anonymous

Great tutorial and comments, thanks all.

One of my remote shares had "browsable = no" in the server's /etc/samba/smb.conf and this didn't show up in autofs. Changing that to "browsable = yes" and restarting both client and server fixed it.

There might be a better way, but it was the easiest fix for me.

From: KF Sol

Hello Peter,

Thanks for your great tutorial.

However, on centos ( Linux version 2.6.18-194.el5  CentOS release 5.5 (Final)) the uid=[my_user] stanza in the auto.cifs did not work for me.   

If I left uid=[my_user] in that line then in /var/log/messages I found :  >> bad user name "my_user"  at the time I tried to access the share. I could see the share but not access it.

I had the leave it out and the validation was done with the $credfile and it works fine now.

 So it looks like:


From: Douglas Borg

I had a lot of trouble getting this method to work for me on CentOS5.2. I finally broke down and RTFM. There is a much easier way to get this all set up if you have CentOS 5.x and it might work better on some other RedHat-like systems (or any system with automount 5 probably):

 I am using a slightly modified version of the "Yet Another Even-better method" with great success. I only wish I had found this way first. Hope someone else finds this useful.

From: James Watt

The author really needs to put a big warning at the top that says, "This is a completely ridiculous way of doing this and you can very simply do this with a couple lines of configuration. This script is only for people who want to take 10x as long to complete the task as they need."

From: Boaz Harrosh

Thanks for the above instructions works like a charm... except

for international users the cifs protocol is unicode the Kernel's defaults on Ubuntu and Fedora is not usually utf8. So in above /etc/auto.cifs script change:

- mountopts="-fstype=cifs,file_mode=0644,dir_mode=0755,uid=user,gid=users"
+ mountopts="-fstype=cifs,file_mode=0644,dir_mode=0755,uid=user,gid=users,iocharset=utf8"
.e.g add iocharset=utf8 to mountopts=
utf8 in above line should match your console and GUI locale. In most modern distros I know it is utf8 since a long time. Else change it to match your environment

From: James Watt

I'm sorry to post such a negative comment here, but I want to forewarn anyone else who wastes 4 hours on this like I did.

This guide is simply over the top and ridiculous. You don't need a script like this to mount cifs points with autofs. I could not get this guide to work no matter what I did. It's also quite assinine that I'd be required to mount every share instead of just the one mountpoint that I need like this script supposedly does, nor did this script allow for me to use multiple usernames and passwords for my mounts.A much better and simpler explanation can be found here. I literally had this guy's post working within minutes.


The short version of the above link is this:

1. mkdir /cifs (or ensure it's a TOTALLY EMPY DIRECTORY if it already exists.)

2. The only line you should have in /etc/auto.master will look like this

/cifs /etc/auto.cifs --timeout=300 --ghost --soft

3. The only line(s) in /etc/auto.cifs should be your specific mount points. Here's mine, which is two separate mounts. The first column is the local mount point (i.e. /cifs/termserver/ for the first line.) The second column is the options. The last column is the UNC share path as you'd type it to get to the specific share.

termserver -fstype=cifs,rw,iocharset=utf8,sec=ntlm,uid=james,gid=james,credentials=/etc/auto.credentials.termserver ://termserver/frontendsbackup -fstype=cifs,rw,iocharset=utf8,sec=ntlm,uid=james,gid=james,credentials=/etc/auto.credentials.backupserver ://backupserver/sql4. You don't need separate credentials files, but I did because the accounts are different on both. Your credential file should just have the following two lines in it with no extra line breaks:




Don't forget to chmod 600 the crenetials files.

From: Tim

I agree with James; the script at the top of this page wasn't necessary for my setup, and the instructions on the page he linked to were simple and straightforward.  Maybe the script here would help for someone with a more complicated setup, but it was confusing overkill for what I needed.

From: skyrail

Applied the same method as James, it worked fine.But I require a way to auto unmount samba shares, usb mount too, when the share is no longer available. Why? because it's ressource consuming and the filesystem hang and severely slow down when browsing empty folder mount point mounted by automount, or its parent fodler.