Skip to content

Instantly share code, notes, and snippets.

@asad-albadi
Last active July 3, 2024 08:53
Show Gist options
  • Save asad-albadi/71f27a053d201035545edb36f4bca5c6 to your computer and use it in GitHub Desktop.
Save asad-albadi/71f27a053d201035545edb36f4bca5c6 to your computer and use it in GitHub Desktop.

Auto-Mounting an SMB Share on Debian

This guide provides steps to auto-mount an SMB share on Debian using a systemd mount unit and NetworkManager-wait-online.service.

Prerequisites

Ensure cifs-utils is installed:

sudo apt update
sudo apt install cifs-utils

Method: Using a Systemd Mount Unit

Step 1: Create Mount Point

Ensure the mount point directory exists:

sudo mkdir -p /mnt/media_storage

Step 2: Create Credentials File

Create a file to store your SMB credentials:

sudo nano /etc/smbcredentials

Add the following lines:

username=asad
password=test

Secure the credentials file:

sudo chmod 600 /etc/smbcredentials

Step 3: Create Systemd Mount Unit File

Create a new systemd mount unit file:

sudo nano /etc/systemd/system/mnt-media_storage.mount

Add the following content to the file:

[Unit]
Description=Mount SMB Share
After=network-online.target
Wants=network-online.target
Requires=network-online.target

[Mount]
What=//10.10.12.74/media_storage
Where=/mnt/media_storage
Type=cifs
Options=credentials=/etc/smbcredentials,uid=1000,gid=1000,iocharset=utf8

[Install]
WantedBy=multi-user.target

Step 4: Reload Systemd

Reload the systemd daemon to recognize the new unit file:

sudo systemctl daemon-reload

Step 5: Enable and Start the Mount Unit

Enable the mount unit to start at boot:

sudo systemctl enable mnt-media_storage.mount

Start the mount unit immediately:

sudo systemctl start mnt-media_storage.mount

Step 6: Verify the Mount

Check if the share is mounted:

df -h

List the contents of the mounted directory to ensure it's working:

ls /mnt/media_storage

Additional Configuration

Ensure that the system waits for the network to be fully up:

Step 1: Enable NetworkManager-wait-online Service

Enable and start the NetworkManager-wait-online.service:

sudo systemctl enable NetworkManager-wait-online.service
sudo systemctl start NetworkManager-wait-online.service

Step 2: Verify Service

Check the status of the service to ensure it's working properly:

sudo systemctl status NetworkManager-wait-online.service

Troubleshooting

  • Check for errors in system logs:
    sudo journalctl -xeu mnt-media_storage.mount
  • Verify network connectivity to the SMB server:
    ping 10.10.12.74

Following these steps should help you auto-mount an SMB share on Debian, ensuring it is available at boot time.

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