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:
Then edit /etc/auto.master and uncomment the smb line:
Restart autofs:
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":
You can even list the shares on "bart" by doing:
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:
I then created a connection specific file /etc/auto.smb.bart.opts containing:
Now any mounts on "bart" will be set read only and use the UTF-8 character set.
$ 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.