NAS Mount Script (mount_nas.sh)#
This script allows you to mount, unmount, and configure automatic mounting of NAS shares on Ubuntu Linux.
π₯ Prerequisites#
- Download script file:
wget https://github.com/pamagister/Digital-Security-Ops-Mastery/blob/main/ubuntu-linux-automations/scripts/mount_nas.sh
- 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
- 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/fstabautomatically. - 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
0664and0775, 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