Skip to content

Instantly share code, notes, and snippets.

@e7d
Last active April 12, 2022 14:04
Show Gist options
  • Save e7d/47c8ed43b511e67ea577705cb654b406 to your computer and use it in GitHub Desktop.
Save e7d/47c8ed43b511e67ea577705cb654b406 to your computer and use it in GitHub Desktop.
Auto-Mount Windows shared folders (CIFS) in Debian Stretch when network is ready

Ensure necessary dependencies are installed:

sudo apt-get update
sudo apt-get install cifs-utils

Add post-up /etc/network/if-up.d/cifs-mount to your interface (here eth0) in /etc/network/interfaces file:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug eth0
iface eth0 inet dhcp
post-up /etc/network/if-up.d/cifs-mount

Copy this post-up script in /etc/network/if-up.d/cifs-mount file:

#! /bin/sh
# Remount network dependant CIFS mounts when the eth0 interface comes up.

set -e

cat /etc/fstab | grep cifs | sed 's/\,\?noauto\,\?//' >/tmp/cifs-fstab
cat /tmp/cifs-fstab
mount -a --fstab /tmp/cifs-fstab
rm /tmp/cifs-fstab

exit 0

Open /etc/fstab file to ensure your CIFS mount has noauto option, so that it will not be mounted right upon startup, but later when network interface comes up, thanks to the post-up script written before:

# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
/dev/sda1               /               ext4            errors=remount-ro       0       1
/dev/sda5               none            swap            sw                      0       0
/dev/sr0                /media/cdrom0   udf,iso9660     user,noauto             0       0
//192.168.0.1/share     /mnt/share      cifs            noauto,credentials=/home/user/.smbcredentials,iocharset=utf8,sec=ntlm,file_mode=0777,dir_mode=0666,noperm       0       0

Fill the /home/user/.smbcredentials file if your CIFS mount requires authentication:

username=WindowsUser
password=########

@coreybrett
Copy link

Why not just use...
post-up mount /mnt/share

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