NAS Mount Script (mount_nas.sh)#

This script allows you to mount, unmount, and configure automatic mounting of NAS shares on Ubuntu Linux.


πŸ“₯ Prerequisites#

  1. Download script file:
wget https://github.com/pamagister/Digital-Security-Ops-Mastery/blob/main/ubuntu-linux-automations/scripts/mount_nas.sh
  1. Create a credentials file:
sudo nano /etc/samba/credentials_nas
````

Contents:

username=YOUR_NAS_USERNAME password=YOUR_NAS_PASSWORD

2. Restrict permissions:

```bash
sudo chmod 600 /etc/samba/credentials_nas
  1. Make the script executable:
chmod +x mount_nas.sh

βš™οΈ Configuration#

Edit the script variables as needed:

NAS_HOST="NAS_HOST_NAME.local"
MOUNT_BASE="/mnt/nas"
CREDENTIALS="/etc/samba/credentials_nas"

# Shares to mount
SHARES=("book" "data" "music" "photo" "software" "video" "data_encrypt" "cloud")

πŸš€ Usage#

Run the script with sudo:

sudo ./mount_nas.sh

You will be prompted to select an action:

Option Description
1 Mount all shares (default)
2 Unmount all shares
3 Enable automount on restart (writes to /etc/fstab)

βš™οΈ Automount Details#

When choosing option 3:

  • The script backs up /etc/fstab automatically.
  • Old NAS entries are removed.
  • New entries are added with dynamic UID and GID based on the calling user.
  • Shares will mount automatically at system boot via systemd.

Test the new fstab entries:

sudo mount -a

πŸ“ Notes#

  • The script checks if the NAS host is reachable before mounting.
  • Existing mounts are unmounted first to avoid conflicts.
  • File and directory permissions are set to 0664 and 0775, respectively.
  • Compatible with CIFS/SMBv2.

πŸš€ Example#

Mount all shares manually:

sudo ./mount_nas.sh
# Select 1

Enable automount:

sudo ./mount_nas.sh
# Select 3
sudo mount -a   # optional test