DevOps · K8s · Volleyball · Travel  •  DevOps · K8s · Volleyball · Travel  •  DevOps · K8s · Volleyball · Travel
Explore NY Stream

Making samba permanent and auto-mount in linux

— ny_wk

Making samba permanent and auto-mount in linux

To permanently mount a Samba share in Linux you add a cifs entry to /etc/fstab backed by a protected credentials file; to mount it only on demand you let the autofs daemon attach it the moment you enter the mount point and detach it when idle. This guide walks through both methods end to end, with the correct package, syntax, permissions, and verification steps.

Samba (and its Windows-side counterpart, CIFS/SMB) is the standard way Linux talks to Windows file shares and NAS devices. A one-off mount command works for a quick test, but it disappears on reboot. The two durable patterns below — a static /etc/fstab mount and an autofs on-demand mount — are exactly what you want on a server or workstation that must reconnect reliably.

The problem: a manual Samba mount does not survive a reboot

A typical interactive mount looks like this:

sudo mount -t cifs //192.168.104.92/unixmate /smbc -o username=unixmate

It prompts for a password, mounts the share, and works — until the machine reboots, the network blips, or the session ends. At that point the mount is gone and any service or script that depended on /smbc breaks. You need the mount to be automatic and credential-free at boot, the same way you would make an NFS mount permanent.

There are two clean solutions, and the right one depends on how often you actually use the share:

  • Permanent mount via /etc/fstab — the share is mounted at boot and stays mounted. Best when the share is in constant use (for example, a shared application or backup target).
  • On-demand mount via autofs — nothing is mounted until you access the directory; it unmounts automatically after an idle timeout. Best for occasional access, laptops, or many shares you do not want connected all the time.

Scenario used throughout this guide

SettingValue
Samba server (IP)192.168.104.92
Share nameunixmate
Samba usernameunixmate
Samba passwordred
Local mount point/smbc

Substitute your own server IP or hostname, share, user, and password. Never reuse a weak password like red on a real network — it is used here only to keep the example readable.

Prerequisite: install the CIFS mount helper

The Linux kernel speaks CIFS, but the userspace mount.cifs helper lives in a separate package. Without it you will see mount: wrong fs type, bad option, bad superblock errors. Install it first:

  • RHEL / CentOS / Rocky / AlmaLinux / Fedora: sudo dnf install cifs-utils (older systems: sudo yum install cifs-utils)
  • Debian / Ubuntu: sudo apt update && sudo apt install cifs-utils
  • openSUSE: sudo zypper install cifs-utils

Create the mount point if it does not already exist:

sudo mkdir -p /smbc

Confirm you can reach the server and that the share is visible before automating anything:

smbclient -L //192.168.104.92 -U unixmate

This lists the shares the server is offering. If unixmate appears in the list, you are ready to make the mount permanent.

Method 1: Permanently mount the Samba share with /etc/fstab

This is the most common way to make a Samba mount permanent in Linux. The challenge is that CIFS requires credentials, and you do not want to type them at every boot. There are two ways to supply them: a separate credentials file (recommended) or inline in fstab itself.

Step 1: Create a protected credentials file

Putting the username and password in their own file keeps them out of /etc/fstab, which is world-readable. Create the file (any path works; /etc/samba/ is a tidy home for it):

sudo tee /etc/samba/.smbcreds > /dev/null <<'EOF'
username=unixmate
password=red
EOF

If the share belongs to a domain or workgroup, add a third line such as domain=WORKGROUP.

Step 2: Lock down the credentials file

This step is the one most tutorials skip, and it matters. A credentials file that anyone can read defeats the purpose. Restrict it to root:

sudo chmod 600 /etc/samba/.smbcreds
sudo chown root:root /etc/samba/.smbcreds

Step 3: Add the fstab entry

Open /etc/fstab with a root editor (sudo vi /etc/fstab) and add a single line. The six fields are: device, mount point, filesystem type, options, dump, and pass.

//192.168.104.92/unixmate  /smbc  cifs  credentials=/etc/samba/.smbcreds,uid=1000,gid=1000,iocharset=utf8,vers=3.0,_netdev,nofail  0  0

What each important option does:

  • credentials= — points to the protected file from Step 1.
  • uid= / gid= — the local user/group that should own the mounted files (use id yourname to find your numbers). Without this, files often show up as owned by root.
  • vers=3.0 — the SMB protocol version. Modern servers use 3.0 or higher; only fall back to vers=2.1 or 1.0 for legacy devices. Avoid SMBv1 unless absolutely required — it is deprecated and insecure.
  • _netdev — tells systemd this mount needs the network up first, so boot does not hang or fail trying to mount too early.
  • nofail — lets the system finish booting even if the share is temporarily unreachable, instead of dropping into emergency mode.
  • iocharset=utf8 — correct handling of non-ASCII filenames.

Step 4 (alternative): Put credentials inline instead

If you prefer not to manage a separate file, you can place the credentials directly in the options field. This is simpler but less secure, because /etc/fstab is readable by all users:

//192.168.104.92/unixmate  /smbc  cifs  username=unixmate,password=red,uid=1000,gid=1000,vers=3.0,_netdev,nofail  0  0

For anything beyond a throwaway lab box, use the credentials file approach in Steps 1–3.

Step 5: Mount everything and confirm

Reload systemd's view of fstab and mount all entries that are not yet mounted:

sudo systemctl daemon-reload
sudo mount -a

If mount -a returns no errors, the share is mounted. You do not need to reboot to test it — but a reboot is the real proof that it is permanent. The mount will now reappear automatically on every boot.

Method 2: On-demand auto-mount with autofs

An autofs auto-mount only connects the share when you actually touch the directory, then disconnects it after a short idle period. This reduces standing network connections, handles flaky links gracefully, and scales well when you have many shares. It mirrors the way NFS automounts are configured.

Step 1: Remove the fstab entry first

Do not let both methods fight over the same mount point. If you added the /etc/fstab line from Method 1, delete it (or comment it out) and unmount the share:

sudo umount /smbc
sudo vi /etc/fstab   # remove the //192.168.104.92/unixmate line

Step 2: Install and enable autofs

sudo dnf install autofs        # RHEL/Rocky/Fedora
sudo apt install autofs        # Debian/Ubuntu

Step 3: Edit the master map

The master configuration file is /etc/auto.master. It tells autofs which parent directory it manages and which map file describes the entries under it. Add this line:

/smb  /etc/auto.samba  --timeout=60

Here /smb is the parent directory autofs controls, /etc/auto.samba is the map file you are about to create, and --timeout=60 unmounts the share after 60 seconds of inactivity. (The original write-up mentioned a 10-second idle disconnect — that is simply this timeout value; set it to whatever suits your workflow.) Do not pre-create the /smb directory and do not put it in /etc/fstab; autofs manages it.

Step 4: Create the map file

Create /etc/auto.samba. Each line is: key (the subdirectory created on access), mount options, and the location. Keep using the protected credentials file from Method 1:

unixmate  -fstype=cifs,credentials=/etc/samba/.smbcreds,uid=1000,gid=1000,vers=3.0,iocharset=utf8  ://192.168.104.92/unixmate

Two details that trip people up: the location uses a colon before the double slash (://server/share) in autofs syntax, and the key (unixmate) becomes the directory name under /smb. So the share will appear at /smb/unixmate.

Step 5: Restart autofs and trigger the mount

sudo systemctl restart autofs
sudo systemctl enable autofs

Now simply access the directory — autofs mounts it on the fly:

cd /smb/unixmate
ls

Listing /smb on its own may show nothing until you reference the key directly, because autofs only materializes mounts on access. Leave the directory idle past the timeout and it unmounts itself automatically.

Common pitfalls and how to fix them

  • "mount: wrong fs type, bad option, bad superblock"cifs-utils is not installed, or the vers= you requested is not supported by the server. Install the package and try vers=2.1 or vers=1.0 for old NAS units.
  • "Permission denied" / "NT_STATUS_LOGON_FAILURE" — wrong username, password, or domain. Test the credentials manually with smbclient //192.168.104.92/unixmate -U unixmate before blaming the config.
  • Boot hangs or drops to emergency mode — you forgot _netdev and nofail. The system tried to mount before networking was ready. Add both options.
  • Files owned by root, no write access — add the correct uid= and gid= options (and optionally file_mode=0644,dir_mode=0755).
  • Credentials file ignored — a typo in the path, or the file is not chmod 600/owned by root. Double-check both.
  • autofs shows an empty /smb — this is expected. Access /smb/<key> directly to trigger the mount; do not create the directory manually.
  • SMBv1 warnings — never default to vers=1.0. It is disabled on modern servers for security reasons; only use it as a last resort for legacy hardware that cannot be upgraded.

Verification: confirm the Samba mount is permanent

Use these checks to prove the mount works and persists:

  1. Confirm the share is mounted and shows the cifs type: mount | grep cifs or findmnt /smbc.
  2. Check free space and the server path: df -hT /smbc.
  3. Test read/write as the intended user: touch /smbc/test.txt && rm /smbc/test.txt.
  4. For the fstab method, the definitive test is a reboot: sudo reboot, then re-run findmnt /smbc after login.
  5. For autofs, run cd /smb/unixmate && ls, then watch it disappear from mount output after the idle timeout.

If all five pass, your Samba share is mounted reliably and will reconnect on its own.

Key Takeaways

  • Install cifs-utils first — the kernel speaks CIFS but the mount.cifs helper ships separately.
  • For a constant connection, add a cifs line to /etc/fstab using a credentials= file locked down with chmod 600.
  • Always include _netdev and nofail so a missing network or unreachable server never blocks the boot.
  • For occasional access, use autofs: edit /etc/auto.master, create a map file, and the share mounts on access and unmounts after the idle --timeout.
  • Set uid/gid for correct ownership, prefer vers=3.0, and verify with findmnt, df -hT, and a reboot.

Frequently Asked Questions

What is the difference between SMB, CIFS, and Samba?

SMB is the network file-sharing protocol; CIFS is an older dialect of SMB. Samba is the open-source software that implements SMB/CIFS on Linux. When you mount a Windows or NAS share on Linux, you use the cifs filesystem type, which now negotiates modern SMB versions (2.x/3.x) under the hood.

How do I auto-mount a Samba share at boot without entering a password?

Store the username and password in a separate file (for example /etc/samba/.smbcreds), set it to chmod 600, then reference it from /etc/fstab with credentials=/etc/samba/.smbcreds. Run sudo mount -a to apply it without rebooting.

Should I use fstab or autofs for a Samba mount?

Use fstab when the share is in near-constant use and you want it always available. Use autofs when access is occasional, the network is unreliable, you are on a laptop, or you manage many shares and want them to connect only on demand and release when idle.

Why does my Samba mount fail with a bad superblock or wrong fs type error?

Almost always because cifs-utils is not installed, or the SMB protocol version you requested is not supported. Install cifs-utils, then try vers=3.0; for very old devices fall back to vers=2.1 or, only as a last resort, vers=1.0.

If this helped you mount Samba the right way, subscribe to @explorenystream on YouTube for more Linux and sysadmin walkthroughs.