Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have no problem mounting my shares (fstab or Gigolo). What I looking for is a way to automatically unmount the shares when I disconnect from the network and reconnect when I return. Gigolo seems close. It reconnects fine, but I still seem to have to manually disconnect from each share individually.

share|improve this question

You will have to write a script. This is a skeleton / template, call it net_mount and put it in /etc/NetworkManager/dispatcher.d

# auto mount/umount network shares


# set your desired network here

mount_shares (){
    # command(s) to mount shares here
    mount share1
    mount share2

umount_shares (){
    # command(s) to un-mount shares here
    umount share1
    umount share2

if [ -n "`/sbin/ip addr show $IF to $NETMASK`" ]; then
case "$STATUS" in

Make it executable

sudo chmod a+x /etc/NetworkManager/dispatcher.d/net_mount
share|improve this answer
Thanks. So this script will be used in conjunction with fstab or should everything be done in the script? ie /etc/fstab : // /mnt/example cifs username=user,password=1234 – jasonwert Jan 12 '12 at 2:10
You have to fill in how to mount your shares. If they are in fstab, mount /mnt/example. Personally I would add them to fstab with the noauto option. – bodhi.zazen Jan 12 '12 at 2:16
I seem to still have an issue. It mounts fine, but as an example if I pull the network cable nothing gets unmounted. My conky script hangs and if I try to go to /mnt it sits there spinning. As soon as I put the cable back in /mnt loads and conky starts working again. Am I missing something in my fstab? // /mnt/videos cifs noauto,username=user,dir_mask=0777,password=1234,rw 0 0 – jasonwert Jan 12 '12 at 3:28
The script is for connecting and disconnecting with network manager. Why are you pulling your network cable ? Disconnect and re-connect with NM, pull the cable after you disconnect and before you re-connect. – bodhi.zazen Jan 12 '12 at 4:18
Makes no difference. I tried "Disconnect" from the network manager and got the exact same result. Everything hangs – jasonwert Jan 12 '12 at 4:29
up vote 1 down vote accepted

I found exactly what I was looking for in the Ubuntu Forums.

HOWTO: Automatically mount and unmount shares (alternative version)

It automatically mounts and unmounts shares as they come and go. I've used this method on Ubuntu 11.10, 12.04, and Debian Squeeze and it works flawlessly.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.