Skip to content

Instantly share code, notes, and snippets.

@danielcarr
Last active July 17, 2018 11:37
Show Gist options
  • Save danielcarr/928296f5463a9ddc5abee6c3513c7334 to your computer and use it in GitHub Desktop.
Save danielcarr/928296f5463a9ddc5abee6c3513c7334 to your computer and use it in GitHub Desktop.
Automatically mount and unmount network shares with connection to company network
#! /usr/bin/env bash
# Put this in /etc/NetworkManager/dispatcher.d/
# to mount and unmount smb/cifs shares with the connection to the network
if test "${IP4_DOMAINS}" = "${COMPANY_DOMAIN}"; then
if "${2}" = "up" -o "${2}" = "vpn-up"; then
# mount the common directory as read only to avoid mistakes
mount /mnt/sharedserver
mount /mnt/home
fi
elif test "${2}" = "down" -o "${2}" = "vpn-down"; then
umount -l -a -t cifs # lazily unmount all cifs filesystems
fi
# This is the /etc/fstab file
# ...
# lots other entries
# ...
//SHAREADDRESS/common/directory/path /mnt/sharedserver cifs noauto,ro,uid=1000,gid=100,credentials=/home/username/.sharedserver.creds 0 0
//SHAREADDRESS/users/my-directory /mnt/home cifs noauto,credentials=/home/username/.sharedserver.creds 0 0
domain=DOMAIN
username=USERNAME
password=PASSWORD
@danielcarr
Copy link
Author

Rather than passing the username and password directly in the script, it's better to make a file (in a location that can only be edited or read by the user who should have access to the server), in the format of sharedserver.creds, and pass that as the credentials option to the mount command.

@danielcarr
Copy link
Author

The even better way is to add the mount points to /etc/fstab and then you can just mount the mount point without specifying anything further. And it makes it easier to remount (for example to add write permissions) later on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment