Skip to content
Secure the credention file for cifs in fstab

Secure the credention file for cifs in fstab

Secure the cifs credentials in a locked down file

there is no way to avoid storing credentials somewhere for CIFS mounts at boot. The protocol itself requires authentication before mounting, and fstab runs before any interactive login. What you can do is move from “password in a world-readable file” to “password in a root-only restricted file” — which is the standard accepted practice for server automation.

Here’s the secure-but-practical pattern used in production

1. Move credentials out of fstab entirely

Create a dedicated credentials file with strict permissions

sudo mkdir -p /etc/samba # normally already excist
sudo nano /etc/samba/credentials

Content in the credentials file:

username=nnnnn
password=ppppp
domain=WORKGROUP

Then lock it down

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

In fstab

//10.xxx.xxx.xxx/share  /mnt/share  cifs  credentials=/etc/samba/credentials,uid=1000,gid=4,vers=3.1.1,_netdev,nofail,x-systemd.automount  0 0

2. Rotate the account, don’t protect it forever

Since the credentials will sit on disk, treat them as service account credentials, not your personal login. Best practice:

Create a dedicated SMB user on the server whose only purpose is this mount
Give it the minimum permissions needed (read-only if possible, or write-only to specific directories)
Rotate the password annually (or when personnel changes)
Lock the account out after 5 failed attempts at the server level

This way, if the credentials file ever leaks, the blast radius is bounded to that share alone.

3. Hardening checklist if staying with CIFS

If you stick with CIFS (e.g., Mac compatibility):

OptionWhy
vers=3.1.1Enforce SMB3 — disable weak SMB1/2 protocols
sec=ntlmsspExplicitly choose NTLMv2 over LM
iocharset=utf8Correct encoding for special characters
file_mode=0644,dir_mode=0755Override server defaults for consistency
nounixDisable Unix extensions if server misbehaves
cache=strictPrevent stale data issues on multi-client writes
noserverinoForce inode regeneration (fixes some sync bugs)

Complete example:

//10.xxx.xxx.xxx/share  /mnt/share  cifs  credentials=/etc/samba/credentials,uid=1000,gid=4,vers=3.1.1,sec=ntlmssp,iocharset=utf8,file_mode=0644,dir_mode=0755,_netdev,nofail,x-systemd.automount  0 0

4. Consider switching to NFS for Linux↔Linux

You mentioned concentrating on Linux servers. For pure Linux-to-Linux links, NFS removes the password problem entirely:

Server exports /etc/exports:

/export/data  10.0.0.0/24(rw,sync,no_subtree_check,all_squash,anonuid=1000,anongid=4)

Client /etc/fstab:

10.xxx.xxx.xxx:/export/data  /mnt/share  nfs  _netdev,nofail,x-systemd.automount,noatime  0 0

Zero credentials. Trust based on network reachability and IP ranges. Combine this with firewall rules that restrict the NAS export to your subnet, and you’ve eliminated the secret altogether.

! note: “NFSv4 only needs port 2049 (no rpcbind chaos). If the server sits on your trusted LAN, this is often the cleanest solution.”