An Open Access Peon

09 September 2008

Automounting a CIFS share in Ubuntu

autofs is a tool that mounts and unmounts devices on demand. To use autofs to mount CIFS shares (Windows/Samba) in Ubuntu do the following:

$ sudo apt-get install autofs smbfs smbclient


Then edit /etc/auto.master and uncomment the smb line:

#
# $Id: auto.master,v 1.4 2005/01/04 14:36:54 raven Exp $
#
# Sample auto.master file
# This is an automounter map and it has the following format
# key [ -mount-options-separated-by-comma ] location
# For details of the format look at autofs(5).
#/misc /etc/auto.misc --timeout=60
/smb /etc/auto.smb
#/misc /etc/auto.misc
#/net /etc/auto.net


Restart autofs:

$ sudo /etc/init.d/autofs restart


You can now view and mount CIFS shares by changing to the directory /smb/HOSTNAME/SHARENAME e.g. to mount and change directory to "photos" on "bart":

$ cd /smb/bart/photos


You can even list the shares on "bart" by doing:

$ ls /smb/bart/


What the default scripts don't provide is the ability to have per-connection mount options. In particular I wanted to mount some hosts read only and using UTF-8. I modified the /etc/auto.smb script as follows:

...

# This file must be executable to work! chmod 755!

key="$1"
mountopts="-fstype=cifs"
smbopts=""
credfile="/etc/auto.smb.$key"
optsfile="/etc/auto.smb.$key.opts"

if [ -e $optsfile ]; then
. $optsfile
fi

...


I then created a connection specific file /etc/auto.smb.bart.opts containing:

mountopts="$mountopts,ro,iocharset=utf8"


Now any mounts on "bart" will be set read only and use the UTF-8 character set.

1 Comments:

  • Thanks, very helpful post.

    Super trivial tweak: replace "/etc/init.d/autofs restart" with "sudo /etc/init.d/autofs restart". Won't work if it's not run as root (I suspect), plus running this as root will create the /smb directory, if it doesn't already exist.

    I also found this blog post to be helpful (just the bit about setting up a credfile) - http://www.howtoforge.com/accessing_windows_or_samba_shares_using_autofs

    With your guide + creating a credfile to supply username & password credentials, running on Ubuntu 8.04.1, and it seems to be working well.

    By Anonymous Anonymous, at 7:18 am  

Post a Comment

<< Home