Pages

Thursday, January 22, 2015

*Utility: Using autofs on Linux to automatically mount remote filesystems


NOTE: This page has moved to https://datamakes.com/2015/01/22/using-autofs-to-automatically-mount-linux-filesystems/

Mounting a remote file system automatically
In today's connected world, it is hard to imagine a computer that does not connect to another one remotely.  Just consider the simple example of having a file server in your home.  This is a pretty common situation where you want to have 1 copy of your files but access them from multiple computers without having to make copies.

It would become rather annoying to have to manually reconnect to your file server everytime you need a file.  There are also times where you connection times out - even though you haven't rebooted - and you have to re-establish your network connections.

Never fear - autofs is here!  This will make your life easier.

Let's start by making sure that you have the autofs package installed.  NOTE:  This is the package name on Ubuntu - and it may vary by distribution
sudo apt-get install autofs
That was easy, now let's configure autofs to automatically mount filesystems for us.  In this example, I will be connecting to a Windows server using the cifs protocol.  The same example can be modified to other networked file systems as well.

First, autofs does not ship a script to automatically mount cifs file systems.  So, let's create /etc/auto.cifs and add the following contents.  NOTE:  There are 2 places highlighted in red that you need to change to be your username - and remove the < and > signs.
#!/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.cifs.$key"
# Note: Use cifs instead of smbfs:
mountopts="-fstype=cifs,file_mode=0644,dir_mode=0755,uid=<your username>,gid=<your username>"
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 sure that you make this script executable by typing
sudo chmod 755 /etc/auto.cifs
Now, you need to store credentials for each host you wish to connect to.  These credentials are stored in /etc/auto.cifs.<ip address> - for example, /etc/auto.cifs.192.168.1.1  The format of the file is 1 line for username and 1 line for password, such as
username=<my username>
password=<my password>
Next, we need to tell autofs about our new script - and how to execute it.  This is done by simply adding 1 line to the bottom of /etc/auto.master which reads
/cifs  /etc/auto.cifs --timeout 60

This tells autofs that when you access /cifs/<ip address> that it should mount the remote filesystem for you automatically.  The timeout option says that if I have not accessed the filesystem in 60 seconds, then it should be automatically dismounted.

Since we are connecting to a remote Windows server, you need to append the share name to the end of the directory.  For example, if you
cd /cifs/192.168.1.1/c$
autofs will automatically log into 192.168.1.1 with the credential stored in /etc/auto.cifs.192.168.1.1 and attempt to mount the share named c$.  This assumes that you have the appropriate permissions on the Windows server to access this share.

Finally, we need to make sure that the base directory (/cifs in my example) exists.  You don't have to worry about any directory underneath the base, autofs takes care of that automatically.  Let's type
sudo mkdir /cifs
sudo chown <username>.<username> /cifs

Now that everything is properly connected, you need to restart autofs to recognize the changes.  You can do this by either rebooting or entering
sudo /etc/init.d/autofs restart

Happy file sharing - and no more remembering to mount remote file systems manually.