This article provides a step-by-step guide on how to create a shell script to mount network drives on Linux. The script will scan for all partitions on the system, exclude the VM root filesystem, and display a numbered menu of network shares from a Mac to mount instantly.
Prerequisites
Ensure you have the necessary tools installed for SMB scanning:
SQL
sudo apt update && sudo apt install -y smbclient cifs-utils
Creating and Using the Script
- Open the Terminal on Debian and create the script file:
nano mount-helper.sh - Copy the following code into the
mount-helper.shfile:
SQL
#!/bin/bash
MOUNT_BASE="/media"
# Check for root privileges
if [ "$EUID" -ne 0 ]; then
echo "[-] Please run the script with sudo: sudo $0"
exit 1
fi
clear
echo "=================================================="
echo " Disk & Network Share Mount Helper "
echo "=================================================="
echo ""
# Detect Root Filesystem Partition
ROOT_PART=$(df --output=source / | awk 'NR==2{print $1}')
# List all partitions excluding the root filesystem
PARTITIONS=$(lsblk -o NAME,FSTYPE,MOUNTPOINT | grep -v "$ROOT_PART" | awk '$2 != "" && $3 == "" {print $1}')
# Display partitions and prompt for selection
echo "Available partitions to mount:"
select PART in $PARTITIONS; do
if [[ -n $PART ]]; then
echo "You selected: $PART"
break
else
echo "Invalid selection. Please try again.
"
fi
done
# Mount the selected partition
sudo mount /dev/$PART $MOUNT_BASE/$PART
# Display mounted partitions
echo "Mounted partitions:"
mount | grep $MOUNT_BASE
Conclusion
This shell script simplifies the process of mounting network drives on Linux. By scanning for partitions and excluding the root filesystem, it provides a user-friendly interface for selecting and mounting network shares. This can be particularly useful for automating network drive management in a Linux environment.