Accessing Windows Or Samba Shares Using AutoFS

Want to support HowtoForge? Become a subscriber!
 
Submitted by pefu (Contact Author) (Forums) on Fri, 2006-11-24 15:00. :: Linux | Samba | Storage

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


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Boaz Harrosh (not registered) on Fri, 2010-10-29 00:06.

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

Submitted by Douglas Borg (not registered) on Wed, 2010-08-25 19:33.

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):

 http://wiki.centos.org/TipsAndTricks/WindowsShares#head-96fc35dfb9f5c3083e0569ec02e1aeaa80bb20b8

 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.

Submitted by KF Sol (not registered) on Sun, 2010-08-15 20:14.

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:

mountopts="-fstype=cifs,file_mode=0644,dir_mode=0755"

Submitted by Anonymous (not registered) on Mon, 2010-05-31 18:14.

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.

Submitted by Anonymous (not registered) on Tue, 2009-12-01 22:53.
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?
Submitted by Anonymous (not registered) on Sun, 2009-07-05 13:42.

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

Submitted by Anonymous (not registered) on Sun, 2009-03-15 11:22.

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

SHARE3 .. ://SERVER/SHARE3

...

And so on.

That worked!


Submitted by Wim De Rammelaere (not registered) on Mon, 2008-10-20 18:37.

Another bug is still present.

Sometimes you don't have access to all shares.

You should change

$SMBCLIENT $smbclientopts -gL $key

to

$SMBCLIENT $smbclientopts -gNL $key

 

 

Submitted by davek (registered user) on Tue, 2006-11-28 07:02.

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

Davek 

Submitted by Anonymous (not registered) on Wed, 2009-08-26 22:12.

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...

 

 

Submitted by Jason (not registered) on Wed, 2009-08-26 17:50.

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...

 

Submitted by Anonymous (not registered) on Fri, 2011-06-17 00:10.
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);
gsub(/\&/,"\\\\&",loc)
print " \\\n\t \"/" dir "\"", "\"://" key "/" loc "\""
}
END { if (!first) print "\n"; else exit 2 }'
Submitted by Anonymous (not registered) on Wed, 2011-08-24 01:00.

Thanks Anon!

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

Submitted by Peter Funk (not registered) on Fri, 2008-10-10 14:30.
Hello Davek,
you were right. I've updated and fixed the script using your snippet. Thanks!
PeFu.
Submitted by hobie (registered user) on Mon, 2008-12-08 18:33.

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.

Submitted by Anonymous (not registered) on Tue, 2009-07-28 20:53.
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