We previously wrote about how to create CIFS/SMB shares from Windows to Ubuntu, and in that guide we made the mount permanent by adding the entries to fstab. So far, all good – but there was one thing that we didn’t like and that we were missing from when we used Windows – every time we rebooted our laptop we had to manually add those shares by typing ‘mount -a’ in the terminal. In Windows it just worked as it was Windows shares, and it works on Ubuntu as well, but we hate doing things manually, and especially if it’s a task that we have to repeat over and over again.

So we tried a few solutions, like adding a script to rc.local with the command ‘mount -a’. we also tried adding the script as different users if it was a permissions issue – but no luck. we tried adding just ‘mount -a’ to an existing startup script, but no success. So we gave up on that for a while, until today. It finally hit one of the guys on our team that the reason it didn’t work was because there was no internet connection when the script was run. So what we had to do was trying to run the script after our network was enabled. It turns out you can do that in the interfaces file, here is how:

  1. create the script
    $~: sudo -i && nano /etc/network/if-up.d/mountsmb
  2. Add the command to that script
    mount -a
    exit 0

    Press CTRL+X to save and exit.

  3. make the script executable
    $~: chmod +x /etc/network/if-up.d/mountsmb
  4. Execute the script in interfaces
    $~: nano /etc/network/interfaces

    We have it set to run when our Wifi is detected, in order to do so you have to change YourSSID to the name of your network. Add this in the bottom of your interfaces file:

    if [ `iwlist eth0 scanning | grep YourSSID` ]
    bash /etc/network/if-up.d/mountsmb
  5. Save and exit, and your all done.

    Test your new config by rebooting your computer. If your shares are mounted automatically, everything is working.