Auto Mount a Network File System in Ubuntu based on a Specific Network

Published by Tomas  Vitvar on October 20, 2007 in Linux

When you use different network file systems on different networks (e.g., you often move your laptop from work to home) you may want to mount them automatically depending on the network you are connected. You can do this with a script I introduce in this post.

I often take my laptop from work to home and thus change the networks. At work there are network file systems which I use to backup my data and share files with others. These are only accessible from my work’s private network. When I am at home, I need to mount my home network file system — the source for my video and music. The other situation might be that you want to mount file systems at certain network where you have a fast access to those file systems (e.g., file system in your linux server at work over ssh). To do this automatically depending on the network you are connected, just follow the steps below. This idea and the script are based on the idea and the script for auto mounting SSH file systems.

HOWTO
This howto works for Ubuntu Fiesty. Feel free to adjust it to your distribution and environment.

1: you need to specify valid networks for your fstab entries. For this the script uses the comment attribute of fstab entry options where in single quotas you list the network addresses that the file system should be mounted at. The individual network addresses are separated with commas as shown in the example below.

//srvname /mnt/somedir cifs comment='192.168.16.0,10.2.16.0', username=usr,password=pswd,domain=IE,noauto 0 0

You also should specify noauto attribute to prevent the file system to be mounted automatically by your system. The script will take care of it instead. Also, since network file systems are usually only accessible with proper username/password you should also specify those.

2: download the scripts mount-networks and umount-networks to the folder /etc/network/if-up.d and /etc/network/if-down.d respectively. You can do it using following commands:

sudo wget http://www.vitvar.com/misc/mount-networks --directory-prefix=/etc/network/if-up.d/

sudo wget http://www.vitvar.com/misc/umount-networks --directory-prefix=/etc/network/if-down.d/

3: set the rights for those scripts so that they can be executed:

sudo chmod 601 /etc/network/if-up.d/mount-networks sudo chmod 601 /etc/network/if-down.d/umount-networks

That’s it. You can now switch your networks and your file systems will be mounted as you define in your fstab.

How it works
The script mount-networks is executed whenever the network is on. It searches for all fstab entries with attribute comment. For each entry, it parses the comment attribute and gets all the networks. Then, it gets IP addresses and masks for all interfaces using the ifconfig shell command. For each IP address and using the mask, the script tests if the IP belongs to at least one of the neworks from the fstab entry. If yes, the script mounts the file system.

The script umount-networks is executed whenever the network gets down. The script just unmounts all file systems from the fstab which have the comment attribute defined with list of networks.

5 Responses to “Auto Mount a Network File System in Ubuntu based on a Specific Network”

  1. fessler ralf says:

    hi thomas,

    thanx for your scripts! do you although have a solution to automount the shares if the are behind an openvpn tunnel. the line in my fstab is

    //10.8.0.1/MUSIK /media/MUSIK-gondel cifs username=werner,password=salsa,auto,user,rw 0 0

    and if the vpn connection is established i can manually mount with sudo mount -a but it does not automount.
    would really appreciate your help!!
    thanx and best regards ralf

  2. [...] Context-aware Computing for Mac Published by Tomas Vitvar on July 9, 2009 in Uncategorized [...]

  3. Steve says:

    I realize that you have moved onto the mac, and boy would I love to do that (I use one for my personal use, but work keeps me on other systems). Anyhow, you script sounds perfect but when I install it per the directions above it does not work. I have validated that I can mount the device with “mount connection” and it works great. I have included the comment=’ipaddress’,… in the fstab entry and when I disconnect, reconnect to the network I get the following in my syslog:

    nm-dispatcher.action: Script ‘/etc/NetworkManager/dispatcher.d/01ifupdown’ exited with error status 1.

    I don’t know if it has anything to do with this script or not. But I know that in the end I don’t end up with the storage device on my desktop after connecting to the network.

    Do you have any thoughts or ideas? Thanks very much.
    Steve

  4. Oscar says:

    I had to change one line in the mount-networks script. After that it works like a charm. Excellent post! Thanks!

    Maybe my change will help others too. I changed line 85 in mount-networks from

    for num in `echo “$2.” | egrep -o “^[0-9]*\.” | sed “s/\.//”`; do
    to

    for num in `echo “$2″ | sed “s/\./ /g”`; do

  5. Tomas says:

    Hi Oscar, does this work for you on the latest version of Ubuntu? I haven’t been using this for a while as I have switched to Mac. Some people had difficulties running this on new versions of Ubuntu…

Leave a Reply

-->

Blog

Archive