Here’s a quick and easy way to get Windows shares up and running in your Debian server:
#! /bin/sh
### BEGIN INIT INFO
# Provides: cifs-share-x
# Required-Start: $local_fs $network $named $time $syslog
# Required-Stop: $local_fs $network $named $time $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: Automatically mount Windows shares in Debian Linux
### END INIT INFO
#
# To install this, run as root:
# export SERVICE_NAME="cifs-share-x"; curl "https://gist.githubusercontent.com/caHarkness/22bb218d016663444b1a64023937530d/raw/cifs-share" > /etc/init.d/$SERVICE_NAME; chmod +x /etc/init.d/$SERVICE_NAME; sed -n "s/cifs-share-x/$SERVICE_NAME/gp" /etc/init.d/$SERVICE_NAME; ln -s /etc/init.d/$SERVICE_NAME /etc/rc2.d/S99$SERVICE_NAME; nano /etc/init.d/$SERVICE_NAME; sleep 1; systemctl daemon-reload
#
WINDOWS_USER="Windows User"
WINDOWS_PASSWORD="password"
WINDOWS_SHARE="//DESKTOP-NAME/Share"
LINUX_USER="root"
LINUX_GROUP="root"
LINUX_DIR="/mnt/Share"
mount_share () {
mkdir -p "$LINUX_DIR"
mount -t cifs -o username="$WINDOWS_USER",password="$WINDOWS_PASSWORD",uid=$LINUX_USER,gid=$LINUX_GROUP "$WINDOWS_SHARE" "$LINUX_DIR"
}
unmount_share () {
umount "$LINUX_DIR"
}
list_share () {
ls -lah "$LINUX_DIR"
}
edit_service () {
SERVICE_NAME=$(basename $0)
nano /etc/init.d/$SERVICE_NAME
sleep 1
systemctl daemon-reload
}
uninstall_service () {
SERVICE_NAME=$(basename $0)
rm /etc/init.d/$SERVICE_NAME
rm /etc/rc2.d/S99$SERVICE_NAME
echo "Service uninstalled."
}
PATH=/sbin:/usr/sbin:/bin:/usr/bin
. /lib/lsb/init-functions
case "$1" in
start)
mount_share
;;
stop)
unmount_share
;;
restart)
unmount_share
mount_share
;;
list)
list_share
;;
edit)
edit_service
;;
uninstall)
uninstall_service
;;
*)
echo "Usage: $0 {start|stop|restart|list|edit|uninstall}"
;;
esac
This /etc/init.d
script is available on my GitHub as a gist, you can see the most up-to-date version of it here. To make things even easier, you can run the following, single-liner to install this automatically:
export SERVICE_NAME="cifs-share-x"; curl "https://gist.githubusercontent.com/caHarkness/22bb218d016663444b1a64023937530d/raw/cifs-share" > /etc/init.d/$SERVICE_NAME; chmod +x /etc/init.d/$SERVICE_NAME; sed -n "s/cifs-share-x/$SERVICE_NAME/gp" /etc/init.d/$SERVICE_NAME; ln -s /etc/init.d/$SERVICE_NAME /etc/rc2.d/S99$SERVICE_NAME; nano /etc/init.d/$SERVICE_NAME; sleep 1; systemctl daemon-reload
(remember that this line may change, refer to the comment in the gist)
Doing so will download the most current version of the script, save it as cifs-share-x by default, and open it for editing in nano automatically. Be sure to configure the script correctly! Upon saving, systemctl daemon-reload
is made aware of the new script. You can call service cifs-share-x start
to start the service, or just reboot instead. If you want to do this for multiple Windows shares, just change cifs-share-x in the export SERVICE_NAME=
statement and repeat the process!
This service will manage the mounting and unmounting of the share located at //DESKTOP-NAME/Share
(forward slashes are used in Linux) to the /mnt/Share
directory in Linux, and allowing for easy directory listing, editing the service script, and uninstalling it altogether!