Ubuntu: Automatic mounting of Samba share when connected to a wireless network

I wanted to make my laptop running Ubuntu automatically mount a Samba share whenever it connected to my wireless home network. I quickly discovered the /etc/network/if-up.d and /etc/network/if-down.d folders which contain scripts that are run whenever a network interface is started or stopped.

I devised the following simple scripts to mount and dismount the share when needed:

Mount:

#!/bin/bash
if [ "$IFACE" = "<INTERFACE>" ]
then
    if iwconfig|grep -c <SSID>
    then
        sleep 10s
        mount -t smbfs <SHARE> <MOUNT-FOLDER> -o username=<USERNAME>,password=<PASSWORD>
    fi
fi

Dismount:

#!/bin/bash
if [ "$IFACE" = "<INTERFACE>" ]
then
    if cat /etc/mtab|grep -c <MOUNT-FOLDER>
    then
        umount -t smbfs <MOUNT-FOLDER>
    fi
fi

In order for it to work with your local network settings, you have to replace the following values:

  • <INTERFACE>: Your wireless network interface. Normally eth0, eth1, eth2 or the like. Can be found by running the iwconfigcommand.
  • <SSID>: The SSID of your wireless network.
  • <SHARE>: The Samba share that you want to mount, e.g. \\192.168.1.5\myshare.
  • <MOUNT-FOLDER>: The path to an existing empty folder in which to mount the share.
  • <USERNAME> and <PASSWORD>: The username and password to use when connecting to the share.

Note the 10 second sleep command (sleep 10s) run when we want to mount. This is due to the fact that mount cannot mount a network share immediately after connecting to the network. This bugged me for quite some time, but is easily solved by waiting 10 seconds before trying to mount the share.

About the author

Jesper Borgstrup Jesper is a Masters student of computer science at the University of Copenhagen.

6 thoughts on “Ubuntu: Automatic mounting of Samba share when connected to a wireless network”

  1. Simple, elegant, and effective. Thank you so much for this, and thank you for posting on Ubuntu Forums!

  2. Hi. Thanks for the post. This is exactly what I'm looking for. However, I seem to be unable to get things working appropriately.

    For what it's worth, I'm using NFS versus Samba, but I don't believe that should be an issue.

    Below is what I've cobbled together as a script from your post. It's in the /etc/network/if-up.d folder. Script is titled "mount-wireless-shares" (no extension).


    #!/bin/bash
    if [ "$IFACE" = "wlan1" ]
    then
    if iwconfig|grep -c NetworkNameHere
    then
    sleep 10s
    mount -t nfs 192.168.X.XX:/Directory1 /mnt/mnt_Directory1 -o username=username,password=password
    mount -t nfs 192.168.X.XX:/Directory2 /mnt/Directory2 -o username=username,password=password
    fi
    fi

    Can you tell me what I'm doing wrong?

    As info, I realize that having Uname & PW hardcoded is bad-form. Once I get this to work I'll plug in a credentials file.

    Variations of these NFS commands work fine from the terminal by preceding with "sudo" and elimination of the -o user / password components.

    Any help is appreciated.

    1. Have you tried replacing the value of '192.168.X.XX:/Directory1 ' with '\\192.168.X.XX\Directory1'?

  3. This is AWESOME, I am setting up my wife's laptop with ubuntu and needed to get shares connected wirelessly automagically. Thank you SOOOO much! Yea YOU!

Leave a Reply

Your email address will not be published. Required fields are marked *

* Copy This Password *

* Type Or Paste Password Here *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>