/ linux-administration

Mount network share after boot and/or depending on network location

To address the problem of auto-mounting your shares depending on your location in terms of networks, SSID or vpn connected.

I used to solve this with scripts be it with systemd services, cron, fstab in some way (_netdev) etc. But presently I use NetworkManager[1] and it's dispatcher service.

If you need more details on NetworkManager you can find it, as lots of other useful guides, on Arch linux wiki page[2]. I will not explain how to install NetworkManager cause you can find everything on Arch wiki page, but the most important thing is to keep in mind that NetworkManager manages only interfaces not declared in /etc/network/interfaces so before you start make sure those you need are left out in that file.

I will explain my situation. When I'm at work I wanna auto-mount my "work" shares using local networks and my "home" shares using vpn connection. When I get home it's the other way around. So to make this work you first create your networks using NetworkManager. To make things easier you can use nm-applet which is GUI that will help you create networks and vpn networks. So at work I have 2 ethernet networks and 1 vpn network that connects to home vpn server and it is automatically established when one of those 2 ethernet networks is up. As mentioned you can setup this using nm-applet easily.

Next it's good to define your shares in /etc/fstab to make it easier to use in scripts and to be able to remount it manually if needed.

---
//192.188.31.30/share /mnt/share		cifs	noauto,user,username=user1,password=password,uid=user1,gid=group1 0 2
---

It's good to put noauto option so the system won't try to mount it during boot time.

After that comes the mounting part when connected to specific network. You need to create script in /etc/NetworkManager/dispatcher.d/ that can look something like this:

#!/bin/bash

status=$2

if [ $status = "up" -o $status = "vpn-up" ]; then
    if [ "$CONNECTION_UUID" = "a74446a2-a257-4d13-89bb-990502d8d3a0" -o "$CONNECTION_UUID" = "2498f520-f133-4894-955b-752d44e1edfb"]; then
                mount /mnt/share
	elif [ "$CONNECTION_UUID" = "f30dc93e-e158-4d60-a356-b4e779de74fc" -o "$CONNECTION_UUID" = "9e7f1aa1-c989-4787-b68a-dfee8e5bc9c6" ]; then
                mount /mnt/home
	fi
fi

In short I mount /mnt/share define in fstab when my local network at work or vpn to home network is up and same thing goes for /mnt/home while at home or vpn to work is up.

$CONNECTION_UUID and $VPN_NAME you can easily get from cli:


nmcli connection show
NAME            UUID                                  TYPE             DEVICE  
home_SSID       f30dc93e-e158-4d60-a356-b4e779de74fc  802-11-wireless  wlp58s0 
vpn_work        2498f520-f133-4894-955b-752d44e1edfb  vpn              wlp58s0 
tun0            d1a01b6d-ed8b-4b1a-af42-c79c8c5280f5  tun              tun0    
work1_user      a74446a2-a257-4d13-89bb-990502d8d3a0  802-3-ethernet   --      
vpn_home        9e7f1aa1-c989-4787-b68a-dfee8e5bc9c6  vpn              --

And after disconnecting you need to umount all cifs shares. I mean you don't but It would be good to avoid problems later like delay in shutdown or reboot etc. Create file in /etc/NetworkManager/dispatcher.d/pre-down.d/:

#!/bin/bash
umount -a -l -t cifs

Scripts in dispatcher.d folder need to be root owned and in mode 755 to work. Don't forget if you need more info to read Arch wiki page[2:1]. This is example for mounting network shares but you can use it for any kind of work needed when certain network comes up so feel free to experiment.


  1. https://wiki.gnome.org/Projects/NetworkManager ↩︎

  2. https://wiki.archlinux.org/index.php/NetworkManager ↩︎ ↩︎