Mount an SMB Share on Linux with systemd Automount

This guide explains how to mount an SMB share on Linux in a standard, clean way.

The example uses a persistent mount at:

/mnt/mybox

It also uses systemd automount, so the share does not block your system during boot.

What Is SMB?

SMB means Server Message Block.

It is a network file-sharing protocol. In simple words, SMB lets one computer expose a folder over a network so another computer can use that folder almost like a local folder.

You may also see these related terms:

  • SMB: the file-sharing protocol.
  • CIFS: an older name Linux still commonly uses for SMB mounts.
  • Samba: Linux/Unix software that implements SMB.

The important practical distinction:

  • samba is usually for making your Linux machine act as an SMB server.
  • cifs-utils is for mounting an existing SMB share on your Linux machine.
  • smbclient is for testing and browsing SMB shares from the terminal.
  • gvfs-backends lets graphical file managers open smb:// locations.

If you only want to mount a remote SMB share, you usually need cifs-utils, not the full samba server package.

Should You Mount It in /home?

You can mount a network share inside your home directory, for example:

/home/youruser/mybox

But for a standard persistent system mount, this is usually cleaner:

/mnt/mybox

Use /mnt/mybox when you want a stable filesystem path for scripts, terminal work, backups, editors, or programs.

Use your file manager’s smb:// support when you only need occasional GUI browsing.

Why Use systemd Automount?

Network filesystems are different from local disks. They depend on networking, DNS, remote server availability, and credentials.

If you force a network share to mount during early boot, your system may pause or fail if the network is not ready.

That is why this guide uses:

x-systemd.automount

With automount:

  • Linux creates a placeholder at boot.
  • The real SMB connection happens only when you access the folder.
  • Boot stays reliable even if the remote server is temporarily unavailable.

For example, after boot this command triggers the actual mount:

ls /mnt/mybox

Step 1: Install the Correct Packages

On Debian, Ubuntu, Linux Mint, and related systems:

sudo apt update
sudo apt install cifs-utils smbclient

What these packages do:

  • cifs-utils provides mount.cifs, which Linux uses to mount SMB/CIFS shares.
  • smbclient gives you a terminal tool to test SMB login before mounting.

If you accidentally installed the full Samba server and do not need your computer to share folders to other devices, you can remove it:

sudo apt purge samba

Do not remove cifs-utils.

Step 2: Create the Mount Point

Create the local folder where the remote share will appear:

sudo mkdir -p /mnt/mybox

This folder is not the data itself. It is just the local access point.

After the SMB share is mounted, /mnt/mybox will show the remote files.

Step 3: Create a Protected Credentials File

Do not put your SMB password directly in /etc/fstab.

Instead, store it in a root-only credentials file.

Create a credentials directory:

sudo mkdir -p /etc/samba/credentials
sudo chown root:root /etc/samba/credentials
sudo chmod 750 /etc/samba/credentials

Create the credentials file:

sudo nano /etc/samba/credentials/mybox

Put this inside:

username=YOUR_SMB_USERNAME
password=YOUR_SMB_PASSWORD

For a Hetzner Storage Box main account, the username usually looks like:

username=u123456
password=your-password-here

Now secure the file:

sudo chown root:root /etc/samba/credentials/mybox
sudo chmod 600 /etc/samba/credentials/mybox

Why this matters:

  • /etc/fstab is normally readable by all users.
  • The credentials file should be readable only by root.
  • mount.cifs can still use it during the mount.

Step 4: Test SMB Login Before Mounting

Before editing /etc/fstab, test that the server, share name, username, and password are correct.

Use:

smbclient //YOUR_SERVER/YOUR_SHARE -A /etc/samba/credentials/mybox -m SMB3 -c 'pwd'

Example:

smbclient //u123456.your-storagebox.de/backup -A /etc/samba/credentials/mybox -m SMB3 -c 'pwd'

If it works, you should see the current remote SMB directory.

If this fails, fix the SMB login first. Do not continue to /etc/fstab until the login test works.

Step 5: Find Your Linux User ID

Run:

id

Example output:

uid=1000(ashish) gid=1000(ashish) groups=1000(ashish)

In this example:

  • UID is 1000.
  • GID is 1000.

These values are used in the mount options so files appear owned by your normal Linux user.

Step 6: Add the fstab Entry

Open /etc/fstab:

sudo nano /etc/fstab

Add one line at the bottom:

//YOUR_SERVER/YOUR_SHARE /mnt/mybox cifs iocharset=utf8,rw,seal,credentials=/etc/samba/credentials/mybox,uid=1000,gid=1000,file_mode=0660,dir_mode=0770,nosuid,nodev,nofail,x-systemd.automount,_netdev,x-systemd.mount-timeout=30 0 0

Example for Hetzner Storage Box:

//u123456.your-storagebox.de/backup /mnt/mybox cifs iocharset=utf8,rw,seal,credentials=/etc/samba/credentials/mybox,uid=1000,gid=1000,file_mode=0660,dir_mode=0770,nosuid,nodev,nofail,x-systemd.automount,_netdev,x-systemd.mount-timeout=30 0 0

Replace:

  • u123456.your-storagebox.de with your SMB server.
  • /backup with your share name.
  • uid=1000,gid=1000 with your actual UID and GID if different.

What the fstab Options Mean

cifs

Use the Linux CIFS/SMB filesystem driver.

iocharset=utf8

Use UTF-8 for filenames.

rw

Mount read-write.

seal

Use SMB encryption. This is useful when mounting an SMB share over the internet.

credentials=/etc/samba/credentials/mybox

Read the SMB username and password from the protected credentials file.

uid=1000,gid=1000

Show remote files as owned by your normal Linux user.

file_mode=0660,dir_mode=0770

Control the local Linux permission view for files and directories.

nosuid,nodev

Basic security hardening. Do not allow setuid behavior or device files from the remote share.

nofail

Do not fail the boot if the network share is unavailable.

x-systemd.automount

Create an automount unit. The real mount happens only when the path is accessed.

_netdev

Tell systemd this is a network filesystem.

x-systemd.mount-timeout=30

Do not wait forever if the remote server is unreachable.

Step 7: Verify fstab

Run:

sudo findmnt --verify

If there are parse errors, fix /etc/fstab before continuing.

Step 8: Reload systemd

After editing /etc/fstab, reload systemd:

sudo systemctl daemon-reload

Then start the automount unit:

sudo systemctl start mnt-mybox.automount

The unit name comes from the mount path:

/mnt/mybox -> mnt-mybox.automount

Step 9: Trigger the Mount

Access the folder:

ls /mnt/mybox

This triggers the actual SMB mount.

Check the mount:

findmnt /mnt/mybox

You should see something like:

/mnt/mybox //YOUR_SERVER/YOUR_SHARE cifs rw,...

Step 10: Test Write Access

Create and remove a test file:

touch /mnt/mybox/test-write
rm /mnt/mybox/test-write

If both commands work, write access is working.

If touch fails, check:

  • The remote SMB account has write permission.
  • The fstab entry includes rw.
  • uid and gid match your Linux user.
  • file_mode and dir_mode are not too restrictive.

What Happens After Reboot?

At boot, systemd creates an automount placeholder for:

/mnt/mybox

The real SMB connection does not necessarily happen immediately during boot.

It happens when something accesses the path:

ls /mnt/mybox

This is normal.

This is also safer than forcing the network share to mount during early boot.

Useful Commands

Check mount state:

findmnt /mnt/mybox

Check systemd automount status:

systemctl status mnt-mybox.automount

Check actual mount status:

systemctl status mnt-mybox.mount

Unmount manually:

sudo umount /mnt/mybox

Stop the automount:

sudo systemctl stop mnt-mybox.automount

Start it again:

sudo systemctl start mnt-mybox.automount

Reload systemd after changing /etc/fstab:

sudo systemctl daemon-reload

Troubleshooting

Test DNS:

getent hosts YOUR_SERVER

Test SMB port:

nc -vz YOUR_SERVER 445

Test SMB credentials:

smbclient //YOUR_SERVER/YOUR_SHARE -A /etc/samba/credentials/mybox -m SMB3 -c 'pwd'

Check mount logs:

journalctl -xe

Check only the mount unit logs:

journalctl -u mnt-mybox.mount

If boot is slow or fragile, make sure these options are present:

nofail,x-systemd.automount,_netdev

If write access fails, test whether the SMB account itself can write:

smbclient //YOUR_SERVER/YOUR_SHARE -A /etc/samba/credentials/mybox -m SMB3

Inside smbclient, try:

mkdir test-write
rmdir test-write

How to Remove the Mount Later

Stop the automount and mount:

sudo systemctl stop mnt-mybox.mount mnt-mybox.automount

Edit /etc/fstab:

sudo nano /etc/fstab

Remove the line for /mnt/mybox.

Reload systemd:

sudo systemctl daemon-reload

Optionally remove the mount point and credentials:

sudo rmdir /mnt/mybox
sudo rm /etc/samba/credentials/mybox

Summary

The standard setup is:

  • Use cifs-utils, not the full Samba server, for mounting.
  • Mount persistent SMB shares under /mnt.
  • Store credentials in a protected file under /etc.
  • Add a single /etc/fstab entry.
  • Use x-systemd.automount so boot does not depend on the network share.

References